Javamail sends HTML emails with attachments

Source: Internet
Author: User

/**
*
*/
Package com. tsts. OA. mail;

Import java. util. properties;

Import javax. Activation. datahandler;
Import javax. Activation. filedatasource;
Import javax. Mail. authenticator;
Import javax. Mail. bodypart;
Import javax. Mail. message;
Import javax. Mail. multipart;
Import javax. Mail. passwordauthentication;
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;

/**
* @ Author sunyanan
*
*/
Public class sendhelper {

Public static void send () throws exception {
Properties prop = new properties ();
Prop. Put ("mail. SMTP. Host", "smtp.126.com ");
Prop. Put ("mail. SMTP. Auth", "true ");

Session session = session. getdefaultinstance (prop, (new sendhelper (). New ());

Session. setdebug (true );
Mimemessage message = new mimemessage (session );

Message. setfrom (New internetaddress ("sunyanan8@126.com "));
Message. setrecipient (message. recipienttype. To, new internetaddress ("sunyanan8@126.com "));
Message. setsubject ("topic ");

Bodypart = new mimebodypart ();
Bodypart. setcontent ("<a href =/" #/"> content </a>", "text/html; charset = GBK ");

Bodypart filepart = new mimebodypart ();
Filedatasource DS = new filedatasource ("C: // a.txt"); // a.txt indicates the attachment address, and the attachment and email program are on a server.
Filepart. setdatahandler (New datahandler (DS ));

Multipart part = new mimemultipart ();
Part. addbodypart (bodypart );
Part. addbodypart (filepart );
Message. setcontent (part );

Transport. Send (Message );

}

/**
* @ Param ARGs
*/
Public static void main (string [] ARGs) throws exception {
Send ();

}
 
Class at extends authenticator {

@ Override
Protected passwordauthentication getpasswordauthentication (){
// Todo auto-generated method stub
Return new passwordauthentication ("sunyanan8@126.com", "xxxxxxxxxx ");
}
}

}

 

In this way, you can easily add an upload function to upload local attachments.

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.