Java code for sending mail

Source: Internet
Author: User

 PackageCom.dong.mail;Importjava.util.Properties;ImportJavax.mail.Authenticator;ImportJavax.mail.Message;Importjavax.mail.MessagingException;Importjavax.mail.PasswordAuthentication;Importjavax.mail.Session;ImportJavax.mail.Transport;Importjavax.mail.internet.AddressException;Importjavax.mail.internet.InternetAddress;ImportJavax.mail.internet.MimeMessage;ImportJavax.mail.internet.MimeMessage.RecipientType; Public classMailutils {//Email : Email to who subject: Subject emailmsg: The content of the message     Public Static voidsendMail (string email, string subject, String emailmsg)throwsaddressexception, messagingexception {//1. Create a program and mail server session object sessionsProperties props =NewProperties (); Props.setproperty ("Mail.transport.protocol", "SMTP");//e -Mail protocolProps.setproperty ("Mail.host", "smtp.163.com");//163 server address for sending mailProps.setproperty ("Mail.smtp.auth", "true");//Specifies that validation is true//creating validatorsAuthenticator auth =NewAuthenticator () { Publicpasswordauthentication getpasswordauthentication () {return NewPasswordauthentication ("xxx", "Mima");//verification of e-mail account            }        }; Session Session=session.getinstance (props, auth); //2. Create a message that corresponds to the content of the messageMessage message =NewMimeMessage (session); Message.setfrom (NewInternetAddress ("[email protected]");//Set Sendermessage.setrecipient (recipienttype.to,NewInternetAddress (email));//set the sending method and receiverMessage.setsubject (subject);//subject of the messagemessage.setcontent (emailmsg,"Text/html;charset=utf-8"); //3. Create transport to send messagestransport.send (message); }}

Java code for 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.