JavaMail (ii): Using JavaMail to send complex messages

Source: Internet
Author: User
Tags mail code mailmessage

Previous article we learned to use JavaMail to send Simple mail, this article we use JavaMail send a slightly more complex message (including text, pictures, attachments). Only the core code is posted here, the rest of the code can refer to JavaMail (i): send Simple Mail with JavaMail

Encapsulate sending mail code

/*** Package Send mail code *@authorFly * @ Time 2017-05-09 **/ Public classEmail {/*** Create and send a complex message containing text, pictures, attachments *@throwsException*/     Public Static voidSendcomplexmail (Mailsenderinfo mailinfo)throwsexception{//Determine if identity authentication is requiredMyauthenticator Authenticator =NULL; Properties Pro=mailinfo.getproperties (); if(Mailinfo.isvalidate ()) {//If authentication is required, create a password validatorAuthenticator =NewMyauthenticator (Mailinfo.getusername (), Mailinfo.getpassword ()); }        //constructs a session that sends messages based on mail session properties and password validatorsSession sendmailsession =session.getdefaultinstance (pro, authenticator); //Create a mail message based on sessionMessage MailMessage =Newmimemessage (sendmailsession); //Create a Mail sender addressAddress from =Newinternetaddress (Mailinfo.getfromaddress ()); //set the sender of a mail messageMailmessage.setfrom (from); //Create the recipient address of the message and set it to the mail messageAddress to =Newinternetaddress (Mailinfo.gettoaddress ()); //The Message.RecipientType.TO property indicates that the recipient's type is tomailmessage.setrecipient (Message.RecipientType.TO, to); //set the subject of a mail messageMailmessage.setsubject (Mailinfo.getsubject ()); //set when mail messages are sentMailmessage.setsentdate (NewDate ()); MimeBodyPart Image=NewMimeBodyPart (); //DataHandler for picture Filedatasource as a picture data source (similar to processing attachments: The difference is that you must tell by setting a subtype in the Mimemultipart constructor (or by using Setsubtype ()) Mimemultipart each relevant part and sets the content-id of the image to be used as the src of the image in the IMG tag)DataHandler DH =NewDataHandler (NewFiledatasource ("Stock-photo-189028703.jpg"));        Image.setdatahandler (DH); //reference the image with a given CID URL, where the CID is a reference to the image attachment Content-id header setcontent When using this CIDImage.setcontentid ("Tupian"); BodyPart text=NewMimeBodyPart (); Text.setcontent ("Beautiful picture <br/>); Mimemultipart textimagerelated=NewMimemultipart ();        Textimagerelated.addbodypart (text);        Textimagerelated.addbodypart (image); Textimagerelated.setsubtype ("Related"); BodyPart Textimage=NewMimeBodyPart ();                Textimage.setcontent (textimagerelated); BodyPart Attachment=NewMimeBodyPart (); //DataHandler for Attachment Filedatasource as an attachment data source if you read from the URL, the data source of the attachment is UrldatasourceDataHandler DH2 =NewDataHandler (NewFiledatasource (Mailinfo.getattachfilenames () [0]));        Attachment.setdatahandler (DH2);                Attachment.setfilename (Mimeutility.encodetext (Dh2.getname ())); Mimemultipart Mainpart=NewMimemultipart ();        Mainpart.addbodypart (Textimage);        Mainpart.addbodypart (attachment); Mainpart.setsubtype ("Mixed");        Mailmessage.setcontent (Mainpart); //connect to a mail server, send a message, close a connectiontransport.send (MailMessage); }}

Test code:

 Public classJavamailtest { Public Static voidMain (string[] args) {
Set up message-related information mailsenderinfo mailinfo=NewMailsenderinfo (); Mailinfo.setmailserverhost ("Smtp.163.com"); Mailinfo.setmailserverport ("25"); Mailinfo.setvalidate (true); Mailinfo.setusername ("[Email protected]"); Mailinfo.setpassword (""); Your email password, if your mailbox opens the client authorization password, here is your client authorization password mailinfo.setfromaddress ("[Email protected]"); Mailinfo.settoaddress ("[Email protected]"); Mailinfo.setsubject ("This is a test e-mail"); Mailinfo.setcontent (Hello This is a test e-mail "); String[] FileName= {"Craftsman. docx"}; Mailinfo.setattachfilenames (FileName); Try { //Email.sendtextmail (Mailinfo); //Email.sendhtmlmail (mailinfo);Email.sendcomplexmail (Mailinfo); } Catch(Exception e) {e.printstacktrace (); } }}

Send success:

JavaMail (ii): Using JavaMail to send complex messages

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.