Java Send mail

Source: Internet
Author: User

You first need to add a Javax.mail-1.4.5.jar reference.

Then use the following main code to do it.

 packageorg.lyk.main;Importjava.util.Properties;Importjavax.mail.Authenticator;Importjavax.mail.Message.RecipientType;Importjavax.mail.MessagingException;Importjavax.mail.PasswordAuthentication;Importjavax.mail.Session;Importjavax.mail.Transport;Importjavax.mail.internet.AddressException;Importjavax.mail.internet.InternetAddress;Importjavax.mail.internet.MimeMessage; public classmain{ public Static voidMain (string[] Args)throwsException {Try        {            //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", "[email protected]"); //password to provide when accessing the SMTP serviceProps.put ("mail.password", "XXXX"); //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); //Set Senderinternetaddress form =NewInternetAddress (props.getproperty ("mail.user"));            Message.setfrom (form); //Set RecipientInternetAddress to =NewInternetAddress ("[email protected]");            Message.setrecipient (recipienttype.to, to); //set up ccinternetaddress cc =NewInternetAddress ("[email protected]");            Message.setrecipient (recipienttype.cc, CC); //set Secret send, Other recipients cannot see the BCC email address//internetaddress bcc = new internetaddress ("[email protected]"); //message.setrecipient (recipienttype.cc, bcc); //set the message headerMessage.setsubject ("test message"); //set the content body of a messageMessage.setcontent ("); //Send mailTransport.send (message); } Catch(Exception E) {e.printstacktrace (); } System.out.println ("///~ Main done"); }}

Java Send mail

Related Article

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.