JavaMail Mail Sending
Referencing maven jar Packages
1 <Dependency>2 <groupId>Javax.mail</groupId>3 <Artifactid>Mail</Artifactid>4 <version>1.4.5</version>5 </Dependency>
Send mail function
Private Static voidSendMail () {//Configure environment properties for sending messages FinalProperties props =NewProperties (); /** Available Properties: mail.store.protocol/mail.transport.protocol/mail.host/* Mail.user/mail.from */ //indicates that SMTP sends a message and authentication is requiredProps.put ("Mail.smtp.auth", "true"); Props.put ("Mail.smtp.host", "smtp.163.com"); //Sender's account numberProps.put ("Mail.user", "*****@163.com"); //password to provide when accessing the SMTP serviceProps.put ("Mail.password", "******"); //Building authorization information for SMTP authenticationAuthenticator Authenticator =NewAuthenticator () {@Overrideprotectedpasswordauthentication getpasswordauthentication () {//user name, passwordString userName = Props.getproperty ("Mail.user"); String Password= Props.getproperty ("Mail.password"); return Newpasswordauthentication (userName, password); } }; //Create a mail session using environment properties and authorization informationSession mailsession =session.getinstance (props, authenticator); //Create a mail messageMimeMessage message =Newmimemessage (mailsession); Try { //Set Senderinternetaddress form =NULL; Form=Newinternetaddress (Props.getproperty ("Mail.user")); Message.setfrom (form); //Set RecipientInternetAddress to =Newinternetaddress (****@163.com); Message.setrecipient (Message.RecipientType.TO, to); //set the message headerMessage.setsubject ("test Message"); //set the content body of a messageMessage.setcontent ("<a href= ' http://www.cnblogs.com/dawnheaven/' > Test HTML mail </a>", "text/html;charset= UTF-8 "); //Send mailtransport.send (message); } Catch(addressexception e) {e.printstacktrace (); } Catch(messagingexception e) {e.printstacktrace (); } }
JavaMail sending mail