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. addressexception;
Import javax. Mail. Internet. internetaddress;
Import javax. Mail. Internet. mimemessage;
Public class mysendmessag2 {
/**
* Email sending
*/
Public static void send ()
{
// Session attribute settings
Properties Properties = new properties ();
// Authentication Protocol Settings
Properties. setproperty ("mail. SMTP. Auth", "true ");
// Sending protocol settings
Properties. setproperty ("mail. Transport. Protocol", "SMTP ");
// Set the host server
Properties. setproperty ("mail. Host", "smtp.qq.com ");
// Establish a session mechanism and inject attributes
Session session = session. getinstance (properties, new authenticator (){
@ Override
Protected passwordauthentication getpasswordauthentication (){
// Set the email password
Return new passwordauthentication ("username", "password ");
// Information creation
Message message = new mimemessage (session );
// Sender's email settings
Try {http://www.huiyi8.com/font/
Message. setfrom (New internetaddress ("sender address "));
} Catch (addressexception e ){
System. Out. println ("failed to set the sender's email address ");
E. printstacktrace ();
} Catch (messagingexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
// Set the topic
Try {
Message. setsubject ("beautiful theme ");
} Catch (messagingexception E1 ){
System. Out. println ("Incorrect topic Settings ");
E1.printstacktrace ();
}
Try {// set the sent content to HTML or text format, encoded as UTF-8
Message. setcontent ("<a color = 'red' href = 'www .baidu.com '> You </a>", "text/html; charset = UTF-8 ");
} Catch (messagingexception E1 ){
// Todo auto-generated Catch Block
E1.printstacktrace ();
// Set the recipient's email address. You can have multiple recipients at the same time, including CC and BCC, and recipients.
Try {// send to receiver 1 and receiver 2 by recipient
Message. setrecipients (message. recipienttype. To, internetaddress. parse (
"Receiver 1," receiver 2 ""));
} Catch (addressexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (messagingexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
// Track sent information
Session. setdebug (true );
// Start sending information
Try {
Transport. Send (Message );
} Catch (messagingexception e ){
System. Out. println ("failed to send ");
E. printstacktrace ();
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
Send ();