JavaMail sending mail

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.