Java Send email email related operation code test, generate complex format mail, send mail related operation

Source: Internet
Author: User
Tags auth


Effect Chart:


Related code:

Test1[Java] View plain copy package com.mail;      import java.util.properties;       import javax.mail.address;   import javax.mail.message;   import  javax.mail.session;   import javax.mail.transport;   import  javax.mail.internet.internetaddress;   import javax.mail.internet.mimemessage;  /**    * com.mail.reference below is found on the Internet, not yet to see the level    * Test  is I wrote, and have tested successfully sent       mailboxes and passwords for your own,   *  may encounter a problem,  is to delete your own myeclipse Javaee.jar file in the mail package to erase     *  they will conflict, from the Internet to find a specific operation on it!   *  @author  liangrui   *   */      public class test1 {               public static void main (String[] args)  throws Exception {      &NBSp;     system.out.println ("Hello ...");                       Properties perps=new  Properties ();           //certification             perps.setproperty ("Mail.smtp.auth",  "true");            //Protocol             Perps.setproperty ("Mail.transport.protocol",  "SMTP");                       session session= Session.getinstance (perps);           message mes=new  mimemessage (session);                   //Collection   Content   ,   person            mes.settext (" hello  WORLDDDDDDDDDDDDDDDDDD ");                          //Set Sender address             mes.setfrom (new internetaddress ("liangrui_1988@126.com"));                       //Send information             transport transport=session.gettransport ();            transport.connect ("smtp.126.com", "liangrui_1988", "* * * ");                               //Recipients             transport.seNdmessage (mes,                     new address[]{new internetaddress ("liangrui_1988@sina.com")});                  transport.close ();                     system.out.println (" -----");       }     }  
Test2


[Java] View plain copy package com.mail;      import java.util.properties;       import javax.mail.authenticator;   import javax.mail.message;   import  javax.mail.messagingexception;   import javax.mail.passwordauthentication;   Import  javax.mail.Session;   import javax.mail.transport;   import  javax.mail.internet.internetaddress;   import javax.mail.internet.mimemessage;   Import  javax.mail.internet.MimeMessage.RecipientType;      public class test2  {          /**       *  @param   args       *  @throws  MessagingException         */       public static void main (String[] args)  throws&nbsp exception {                       //Mail Properties Settings ============================================            properties pro=new properties ();            //Certification             Pro.setproperty ("Mail.smtp.auth",  "true");                    //Protocol             pro.setproperty ("Mail.transport.protocol",  "SMTP");            //host   Specify server   port default             Pro.setproperty ("Mail.host",  "smtp.sina.com");                                  //message Session Settings ======== ====================================           Session  Session= session.getinstance (pro,                    new authenticator ()  {                protected passwordauthentication getpasswordauthentication ()  {                    //User name Password                     return new passwordauthentication ("liangrui_1988", "Hu Jintao");                                    }           &NBSP});                       //Mail information Settings ============================================            message ms=new mimemessage (session);            //address   Sender             Ms.setfrom (new internetaddress ("liangrui_1988@sina.com"));            //theme            ms.setsubject ("How are You?"). This is the mail subject ");           //address   recipient             ms.setrecipients (recipienttype.to,               &nbsP;    internetaddress.parse ("liangrui_1988@126.com,liangrui_1988@sohu.com,liangrui_1988@ Sohu.com "));                       //content            ms.setcontent ("< span style= ' color:red ' > Text content   Stay-oh </span> "," TEXT/HTML;CHARSET=GBK ");                   //Launch =================================           transport.send (ms);        System.out.println ("Okkk--------------");       }     }   
Test2222demo
[Java] View plain copy package com.mail;      import java.io.fileinputstream;   import java.util.properties;      import javax.mail.authenticator;   import javax.mail.message;   import javax.mail.messagingexception;   import  javax.mail.passwordauthentication;   import javax.mail.session;   import  javax.mail.transport;   import javax.mail.internet.internetaddress;   import  javax.mail.internet.mimemessage;   import javax.mail.internet.mimemessage.recipienttype;       public class test2demo {              /**       *  @param  args        *  @throws  MessagingException        */       public static void main (String[] args)  throws Exception {                      // Mail Properties Settings ============================================            properties pro=new properties ();            //certification            pro.setproperty ("Mail.smtp.auth", ) True ");                    //protocol            pro.setproperty ("Mail.transport.protocol ", " SMTP ";           //host   Specify server   port default             pro.setproperty ("Mail.host",  "smtp.sina.com");    &NBsp;                              //mail Session Settings ===================================== =======           Session session=  Session.getinstance (pro,                    new authenticator ()  {                protected passwordauthentication getpasswordauthentication ()  {                    // User name Password                     return new passwordauthentication ("liangrui_1988", "Hu Jintao");                                    }          &nbsp});                       //Mail information set = = ======================================           /*message  ms=new mimemessage (session);          //address   Sender           ms.setfrom (new internetaddress ("liangrui_1988@ Sina.com ");          //theme            ms.setsubject ("How are You?"). This is the mail subject ");          //address   recipient            ms.setrecipients (recipienttype.to,  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSp;          internetaddress.parse ("liangrui_1988@126.com, Liangrui_1988@sina.com,liangrui_1988@sohu.com ");                    //content            Ms.setcontent ("<span style= ' color:red ' > Text content   Dull and dull </span>", "TEXT/HTML;CHARSET=GBK");                 //Launch ====================== ===========         transport.send (ms);*/                       //Load generated good messages    Test3  and send            Message me=new  MimeMessage (Session,new fileinputstream ("f:/juxundocument/email/demo3.eml"));          &Nbsp; transport.send (Me,internetaddress.parse ("liangrui_1988@sohu.com")  );                                                      system.out.println ("Okkk--------------");                      }  }  
Test3[Java]View Plain Copy

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.