Simple send mail based on Javaemail Point-to-multipoint (picture and attachment)

Source: Internet
Author: User

Use JUNIT4 to run, according to your preference

Not much to say directly on the code

1 @Test2      Public voidsendemailtest () {3         Try {4Properties Properties =NewProperties ();5             //setting up an SMTP host6Properties.put ("Mail.smtp.host", "smtp.exmail.qq.com");7             //Using SMTP authentication8Properties.put ("Mail.smtp.auth", "true");9             /*Ten * In JavaMail, you can override the extends Authenticator abstract class, in subclasses, in the parent class. One * Getpasswordauthentication () method, you can realize the user authentication when the mailbox is logged in different ways. JavaMail A * This design is used in the Strategy mode (strategy -              */ -MimeMessage message =NewMimeMessage (Session.getinstance (Properties,NewAuthenticator () { the @Override -                  Publicpasswordauthentication getpasswordauthentication () { -                     return NewPasswordauthentication (//set Send account password -"Account", "password"); +                 } -             })); +             //set the properties of a message A  at             //set the sender of a message - //Message.setfrom (New internetaddress ("Sender account")); -Message.setfrom (NewInternetAddress ("account"))); -             //to set the recipient cc for a message the CC BCC indicates dark send -Message.setrecipient (Message.RecipientType.TO,NewInternetAddress ("Recipient Mailbox")); -             //set the subject of the message inMessage.setsubject ("I am the subject of the message"); -             //create the body of a message toMimeBodyPart Text =NewMimeBodyPart (); +             //setcontent ("Body content of the message", "Setting the encoding of Message content") -             //content can directly control the size of the image using CSS theString html = "<div><p> content text </p><div>; *Text.setcontent (HTML, "text/html;charset=gb2312")); $ Panax Notoginseng  -             //one-to-many send as long as you change a place as follows: the  + //String tomore[] = {}; A //            // //build a mass address array the //internetaddress[] ADR = new Internetaddress[tomore.length]; + //for (int i = 0; i < tomore.length; i++) { - //Adr[i] = new internetaddress (Tomore[i]); $ //            } $             //the SetRecipients method of message supports mass-mailing. Note: The SetRecipients method is not the same as the plural - //message.setrecipients (Message.RecipientType.TO, ADR); -  the  -             //Create a pictureWuyiMimeBodyPart img =NewMimeBodyPart (); the             /* - * JavaMail API does not restrict information to text only, any form of information may be a part of mimemessage. Wu * In addition to textual information, as a file attachment included in the e-mail message is a part of the common. JavaMail - * API provides an easy way to allow us to include non-text BodyPart objects by using the DataHandler object. About              */ $  - //File File = new file ("Src/main/resources/321.png"); -             //Picture Path - //datahandler dh = new DataHandler (new Filedatasource (file)); A             //Picture Path +DataHandler DH =NewDataHandler (NewFiledatasource ("D://123.jpg")); the Img.setdatahandler (DH); -             //create a representation of a picture to display in a message $             //src= ' cid:a ' in HTML means reference theImg.setcontentid ("a"); the             //the relationship body and picture theMimemultipart mm =NewMimemultipart (); the Mm.addbodypart (text); - Mm.addbodypart (IMG); in             //set the relationship between the body and the picture theMm.setsubtype ("related"); the             //the body of the drawing class and the text AboutMimeBodyPart all =NewMimeBodyPart (); the all.setcontent (mm); the  the             //attachment to the body (text and IMG) +Mimemultipart mm2 =NewMimemultipart (); - Mm2.addbodypart (all); the Bayi  the  the             //Create an attachment -MimeBodyPart Attch =NewMimeBodyPart (); -             //Annex 1 the             //Data Source the  theFile File =NewFile ("D://123.jpg"); theInputStream C =Newfileinputstream (file);; -DataSource Source1 =NewBytearraydatasource (c, "Application/octet-stream"); the             //set the data for the first attachment theAttch.setdatahandler (NewDataHandler (Source1));//new DataHandler (SOURCE1) Data Processor theString fileName = "123.jpg";//Can be jpg,zip,excle (rar etc should also be possible, no specific test)94             //Chinese garbled problem/set the file name of the first attachment the Attch.setfilename (Mimeutility.encodetext (FileName)); the Mm2.addbodypart (attch); the 98  About  - message.setcontent (mm2);101             //Save Changes102 message.savechanges ();103             //Send mail104 transport.send (message); the 106}Catch(Exception e) {107 e.printstacktrace ();108         }109}

Run to view the receiving mailbox, such as:

Simple send mail based on Javaemail Point-to-multipoint (picture and attachment)

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.