In the previous period of time in the work of the mail to send monitoring alarm information, today in this record a JavaMail mail tool class.
Below is the pom dependency of the jar package used for the JavaMail. Here is the version of JavaMail's 1.4.6.
<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> < Version>1.4.6</version></dependency>
Here is the entity class for some parameter objects of the message used in the tool class.
Packagecom.juihai.entity;Importjava.util.Arrays; Public classEmailto {PrivateString title;//Message subjectPrivateString content;//Message contentPrivateString sender;//Sender AccountPrivateString password;//Sender PasswordPrivateString host;//Outgoing Mailbox serverPrivatestring[] toadress;//recipient address PublicString GetTitle () {returntitle; } Public voidSettitle (String title) { This. title =title; } PublicString getcontent () {returncontent; } Public voidsetcontent (String content) { This. Content =content; } PublicString Getsender () {returnSender; } Public voidSetsender (String sender) { This. Sender =Sender; } PublicString GetPassword () {returnpassword; } Public voidSetPassword (String password) { This. Password =password; } PublicString gethost () {returnhost; } Public voidSethost (String host) { This. Host =host; } Publicstring[] Gettoadress () {returntoadress; } Public voidsettoadress (string[] toadress) { This. toadress =toadress; } @Override PublicString toString () {return"Emailto [title=" + title + ", content=" + content + ", sender=" + Sender + ", password=" + password + ", h Ost= "+Host+ ", toadress=" + arrays.tostring (toadress) + "]"; } }
Here is the tool class for sending email. A comment in the specific description code.
Importjava.io.UnsupportedEncodingException;Importjava.security.GeneralSecurityException;Importjava.util.Properties;ImportJavax.mail.Authenticator;ImportJavax.mail.Message;Importjavax.mail.MessagingException;Importjavax.mail.PasswordAuthentication;Importjavax.mail.Session;ImportJavax.mail.Transport;Importjavax.mail.internet.InternetAddress;ImportJavax.mail.internet.MimeMessage;ImportOrg.apache.log4j.Logger;ImportCom.juihai.entity.EmailTO; Public classSendemailutils {/**Log Output*/ Private StaticLogger log = Logger.getlogger (sendemailutils.class); Public Static BooleanSend (Emailto to)throwsgeneralsecurityexception{FinalString user =To.getsender (); FinalString Password =To.getpassword (); Log.info ("Sendemailutils >>>> Start Send Mail"); Properties Properties= System.getproperties ();//Get System PropertiesProperties.setproperty ("Mail.smtp.host", To.gethost ());//Set the mail server "Smtp.163.com", which is the sender's mailbox serverProperties.put ("Mail.smtp.auth", "true"); String Sendernick=NULL;
//Some mailbox servers need to turn on SSL authentication to send mail, if needed, turn on comments /*mailsslsocketfactory SF = new Mailsslsocketfactory ();//SSL authentication begin Sf.settrustallhosts (TRUE); Properties.put ("Mail.smtp.ssl.enable", "true"); Properties.put ("Mail.smtp.ssl.socketFactory", SF);//SSL Authentication End*/Session Session=Session.getdefaultinstance (Properties,NewAuthenticator () { Publicpasswordauthentication getpasswordauthentication () {return NewPasswordauthentication (user, password);//Sender Mail user name, password } }); Try{mimemessage message=NewMimeMessage (session);//to create a default MimeMessage objectSendernick = Javax.mail.internet.MimeUtility.encodeText ("Juihai"); //Set sender nickname Message.setfrom (NewInternetAddress (sendernick+ "<" +user+ ">")); //message.addrecipient (message.recipienttype.to,new internetaddress ());//Create a single recipientstring[] tos = to.gettoadress ();//Create multiple Recipients if(TOS! =NULL&& Tos.length! = 0) {internetaddress[] toaddress=NewInternetaddress[tos.length]; for(inti = 0; i < tos.length; i++) {Toaddress[i]=Newinternetaddress (Tos[i]); } message.setrecipients (Message.RecipientType.TO, toaddress); } message.setsubject (To.gettitle ());//Set Message subject//Message.settext (content);//Send plain text contentMessage.setcontent (To.getcontent (), "text/html;charset=utf-8");//Send HTML message contenttransport.send (message);//Send emailLog.info ("Sendemailutils Mail sent successfully"); return true; }Catch(Messagingexception Mex) {mex.printstacktrace (); Log.info (">>>>sendemailutils message Send failed" +Mex); } Catch(unsupportedencodingexception e) {log.info (">>>>sendemailutils message send failed-set sender nickname error" +e); E.printstacktrace (); } return false; }}
Tool Class-Send mail (sent via JavaMail)