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.