/**
*
*/
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.