A short and very practical email sending

Source: Internet
Author: User

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 ();

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.