The Email.properties file is as follows:
Host=smtp.qq.com //This is the QQ mailbox, if it is other mailbox services to be configured to the corresponding hostuser= own mailbox name pwd= mailbox Password subject= topic 1:[email Protected] //Sender 2:[ Email protected]
Import Java.io.bufferedreader;import Java.io.ioexception;import Java.io.inputstream;import Java.io.inputstreamreader;import Java.io.unsupportedencodingexception;import Java.util.Date;import Java.util.enumeration;import Java.util.hashmap;import Java.util.map;import Java.util.properties;import Java.util.vector;import Javax.activation.datahandler;import Javax.activation.filedatasource;import Javax.mail.authenticator;import Javax.mail.message;import Javax.mail.messagingexception;import Javax.mail.multipart;import Javax.mail.passwordauthentication;import Javax.mail.session;import Javax.mail.transport;import Javax.mail.internet.internetaddress;import Javax.mail.internet.mimebodypart;import Javax.mail.internet.mimemessage;import Javax.mail.internet.mimemultipart;import javax.mail.internet.MimeUtility; Import Org.apache.commons.lang.stringutils;import Com.pzoom.dsa.common.util.log;import Com.pzoom.dsa.nerd.model.area;public class SendMail {private static log log = Log.getlogger (Sendmail.class); public static string Tochinese (string text) {try {text = Mimeutility.encodetext (new string (text.ge Tbytes (), "GB2312"), "GB2312", "B"); } catch (Exception e) {e.printstacktrace (); } return text; } static StringBuffer buffer = new StringBuffer (); static map<string,string> Map = new hashmap<string,string> (); static{try {InputStream input=area.class.getclassloader (). getResourceAsStream ("email.properties"); BufferedReader br=new BufferedReader (new InputStreamReader (input)); String Line=br.readline (); while (Line!=null) {if (Line.indexof (":") >=0) {string[] val = Line.split (":"); Buffer.append (val[1]+ ",");} Else{string[] Val=line.split ("="); Map.put (Val[0], val[1]);} Line=br.readline ();}} catch (IOException e) {log.error (e);} /** * Mail Send * @param MB * @return * * public static Boolean sendMail (Mailbean MB) {Strin G from = Map.get ("user");//message sender String Subject = Map. Get ("subject");//message subject String content = Mb.getcontent (); String fileName = Mb.getfilename (); vector<string> file = Mb.getfile (); String type = Mb.gettype (); String Nick = Mb.getnick (); Properties props = System.getproperties (); Props.put ("Mail.smtp.host", Map.get ("host")); Set the SMTP host Props.put ("Mail.smtp.auth", "true"); Need to be verified if (Map.get ("host"). Contains ("Gmail")) {Props.setproperty ("Mail.smtp.socketFactory.class", "JAVAX.N Et.ssl.SSLSocketFactory "); Props.setproperty ("Mail.smtp.socketFactory.fallback", "false"); Props.setproperty ("Mail.smtp.port", "465"); Props.setproperty ("Mail.smtp.socketFactory.port", "465"); } Session session = Session.getinstance (props, new Authenticator () {public Passwordauthenticati On Getpasswordauthentication () {return new passwordauthentication (Map.get ("user"), Map.get ("PWD ")); } }); try {mimemessage msg = new MimeMessage (session); if (Stringutils.isnotblank (Nick)) {Nick = Mimeutility.encodetext (Nick); from = nick+ "<" +from+ ">"; } msg.setfrom (New InternetAddress (from)); New InternetAddress (); internetaddress[] address = Internetaddress.parse (buffer.tostring ()); Msg.setrecipients (Message.RecipientType.TO, address);//Msg.setsubject (Tochinese (subject)); Msg.setsubject (subject); Multipart MP = new Mimemultipart (); MimeBodyPart mbpcontent = new MimeBodyPart (); if ("HTML". Equals (Type)) {mbpcontent.setcontent (content, "text/html;charset=gb2312"); }else {mbpcontent.settext (content);} Mp.addbodypart (mbpcontent); /* Add an attachment to the message */if (null!=file) {enumeration<string> efile = file.elements (); while (Efile.hasmoreelements ()) {MimeBodyPart mbpfile = new MimeBodyPart (); FileName = Efile.nextelement (). toString (); Filedatasource FDS = new Filedatasource (fileName); Mbpfile.setdatahandler (New DataHandler (FDS)); Mbpfile.setfilename (Tochinese (Fds.getname ())); Mp.addbodypart (Mbpfile); }} msg.setcontent (MP); Msg.setsentdate (New Date ()); Transport.send (msg); } catch (Messagingexception me) {me.printstacktrace (); return false; } catch (Unsupportedencodingexception e) {e.printstacktrace (); return false;} return true; } public static class mailbean{private String to; Recipient private String from; Sender private String host; SMTP host private String username; Sender's user name private STring password; Sender's password private String subject; Message subject private String content; message body vector<string> file; Multiple attachments private String filename; The file name of the attachment is private string Type;//type text HTML class private string nick;//nickname public string Getto () {return to; } public void Setto (String to) {this.to = to; } public String Getfrom () {return from; The public void Setfrom (String from) {this.from = from; } public String GetHost () {return host; The public void Sethost (String host) {this.host = host; } public String GetUserName () {return username; } public void Setusername (String username) {this.username = username; } Public String GetPassword () {return password; } public void SetPassword (String password) {this.password = password; } public String Getsubject () {return subject; } public void Setsubject (String subject) {this.subject = subject; } public String GetContent () {return content; The public void SetContent (String content) {this.content = content; } public String GetFileName () {return filename; The public void Setfilename (String filename) {this.filename = filename; } public vector<string> GetFile () {return file; public void Attachfile (String fileName) {if (file = = null) file = new Vector< ; String> (); File.addelement (FileName); } public String GetType () {return type; } public void SetType (STring type) {this.type = type; } public String Getnick () {return nick; } public void Setnick (String nick) {this.nick = Nick; } }}
Use your own mailbox in Java for multi-person mail delivery