Send mail using Java Mail

Source: Internet
Author: User
Tags email account tomcat server

This note is referenced from: Gao | Coder, original address: http://blog.csdn.net/ghsau/article/details/17839983

JavaMail is a standard development class library provided by Sun for developers to implement mail delivery and reception in their applications, supporting commonly used messaging protocols such as SMTP, POP3, IMAP, and developers using JavaMail to write mail programs without having to consider the underlying communication details ( Socket), JavaMail also provides APIs that create a variety of message content in complex MIME formats. With JavaMail, we can implement software similar to Outlook, Foxmail. Although JavaMail (supported only JDK4 and above) is one of the Java APIs, it is not directly added to the JDK, so we need to download it separately. In addition, JavaMail relies on the JAF (JavaBeans Activation Framework), JAF has been merged into the JDK after Java6, and JDK5 's class libraries need to be downloaded separately.

javamail:http://www.oracle.com/technetwork/java/javasebusiness/downloads/ Java-archive-downloads-eeplat-419426.html#javamail-1.4.5-oth-jpr

jaf:http://www.oracle.com/technetwork/java/javasebusiness/downloads/ Java-archive-downloads-java-plat-419418.html#jaf-1.1.1-fcs-oth-jpr

I test the code

 Packageorg.sunny.shop.utils;Importjava.util.Properties;Importjavax.mail.Address;ImportJavax.mail.Message;Importjavax.mail.MessagingException;Importjavax.mail.Session;ImportJavax.mail.Transport;Importjavax.mail.internet.InternetAddress;ImportJavax.mail.internet.MimeMessage; Public classEmailutil {//Host Address    Private StaticString host = "Smtp.163.com"; //The sender , fill in your email account    Private StaticString from = "[Email protected]"; //The sender password, fill in your own email password    Private StaticString Password = "* * *"; /*** Sender Text * **/    Private StaticString link = "http://192.168.0.119:8080/"; Private Staticstring getcontent (String link) {string content= ";        returncontent; }         Public Static voidSendEmail (string to, string content) {/*** 1. Create Session * **/Properties Props=NewProperties (); //turn on debug modeProps.setproperty ("Mail.debug", "true"); //The sending host requires authenticationProps.setproperty ("Mail.smtp.auth", "true"); //Set host nameProps.setproperty ("Mail.host", host); //Setting the host portProps.setproperty ("Mail.port", "25"); //Send protocol nameProps.setproperty ("Mail.transport.protocol", "SMTP"); Session Session=session.getinstance (props); /*** 2. Create mail Object * **/Message msg=NewMimeMessage (session); Try {            //Set SenderMsg.setfrom (Newinternetaddress (from)); //Set TitleMsg.setsubject ("This is a test e-mail"); //Set ContentMsg.setcontent (Content, "Text/html;charset=utf-8"); /*** 3. Send mail * **/Transport Transport=Session.gettransport ();            Transport.connect (from, password); Transport.sendmessage (MSG,NewAddress[] {Newinternetaddress (To)});        Transport.close (); } Catch(messagingexception e) {e.printstacktrace (); } System.out.println ("Send mail success!"); } Public Static voidMain (string[] args) {
SendEmail ("Recipient account @qq.com", getcontent (link + "Index.action"))); }}

Send mail using Java 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.