Java sends HTML emails.

Source: Internet
Author: User

 

Package com. awu;

Import java. util. date;
Import java. util. properties;

Import javax. Mail. bodypart;
Import javax. Mail. message;
Import javax. Mail. multipart;
Import javax. Mail. Session;
Import javax. Mail. Transport;
Import javax. Mail. Internet. internetaddress;
Import javax. Mail. Internet. mimebodypart;
Import javax. Mail. Internet. mimemessage;
Import javax. Mail. Internet. mimemultipart;

Public class sendhtmlmail ...{

/***//**
* @ Param ARGs
*/
Public static void main (string [] ARGs )...{
String inline = "love_wuzhou520@163.com ";
String ttitle = "sending HTML emails ";
String tcontent = "<Table border = '1' bgcolor = 'red'> <tr> <TD> send a table </TD> </tr> </table> "; // html code
Properties props = new properties ();
Props. Put ("mail. SMTP. Host", "smtp.163.com ");
Props. Put ("mail. SMTP. Auth", "true ");
Session S = session. getinstance (props );
S. setdebug (true );

Mimemessage message = new mimemessage (s );

// Set the sender/recipient/topic/sending time for the message object
Try ...{
Internetaddress from = new internetaddress ("love_wuzhou520@163.com ");
Message. setfrom (from );
Internetaddress to = new internetaddress (region );
Message. setrecipient (message. recipienttype. To, );
Message. setsubject (ttitle );
Message. setsentdate (new date ());


// Set content for the message object
Bodypart MDP = new mimebodypart (); // creates a bodypart object that stores the content of a letter.
MDP. setcontent (tcontent, "text/html; charset = gb2312"); // set the content and format/encoding method for the bodypart object
Multipart Mm = new mimemultipart (); // create a new mimemultipart object to store the bodypart
// Image (in fact, multiple objects can be stored)
Mm. addbodypart (MDP); // Add the bodypart to the mimemultipart object (multiple bodyparts can be added)
Message. setcontent (mm); // use mm as the content of the message object.

Message. savechanges ();
Transport transport = S. gettransport ("SMTP ");
Transport. Connect ("smtp.163.com", "love_wuzhou520", "email password ");
Transport. sendmessage (message, message. getallrecipients ());
Transport. Close ();
System. Out. println ("sent successfully! ");
} Catch (exception e )...{
E. printstacktrace ();
// Todo: handle exception
}


}

}
 

 

 

The mail. jar package in J2EE (1.4) is required.

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.