Java email Development (2): Use JMail to send an email with illustrated text and attachments (below)

Source: Internet
Author: User

In the previous article Java email Development (II): Using JMail to send an email with illustrated text and attachments (I), we probably learned about the composition of an email.

The following is an email with the following requirements:

1. The body is a piece of html code

2. An image is embedded in the html code.

3. This email contains two attachments.

First, based on the "mail organization structure API" at the end of the previous article, analyze which MimeMultipart objects and which MimeBodyPart objects are listed below.

1. There is a MimeMultipart object msgPart, which indicates the message body of the entire mail, and the multipart type of the message body is mixed (with attachments ).

2. msgPart should have three MimeBodyPart objects: body (indicating the body), attach1 (indicating attachment 1), and attach2 (indicating attachment 2)

3. There is a MimeMultipart object contentPart, which indicates the message body of the body and the multipart type of the message body is related (with image embedded resources ). The contentPart object is associated with the body object.

4. contentPart should have two MimeBodyPart objects: content (indicating the html code of the body) and img (indicating the embedded resource image)

Analyze the relationship between the MimeMultipart object and the MimeBodyPart object, and easily write the code.

The Code is as follows:

Package com. zyh. demo; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. outputStream; import java. util. properties; import javax. activation. dataHandler; import javax. activation. fileDataSource; import javax. mail. message; import javax. mail. message. recipientType; import javax. mail. multipart; 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; import javax. mail. util. byteArrayDataSource;/*** create a complex email, this email contains a body and two attachments *. The body must contain an image * @ author Administrator **/public class Demo3 {public static void main (String [] args) throws Exception {Properties props = new Properties (); props. setProperty ("mail. smtp. auth "," true "); props. setProperty ("mail. transport. protocol "," smtp "); Session session = Session. getInstance (props); session. setDebug (true); Message msg = new MimeMessage (session);/* set the mail header */msg. setFrom (new InternetAddress (""); msg. setRecipients (RecipientType. TO, InternetAddress. parse (",,,"); msg. setSubject ("From sohu this is a complex email");/* set the message content, including two attachments and a piece of body */Multipart msgPart = new MimeMultipart ("mixed"); msg. setContent (msgPart); MimeBodyPart body = new MimeBodyPart (); // indicates the body MimeBodyPart attach1 = new MimeBodyPart (); // indicates Attachment 1 MimeBodyPart attach2 = new MimeBodyPart (); // indicates the attachment 2msgPart. addBodyPart (body); msgPart. addBodyPart (attach1); msgPart. addBodyPart (attach2);/* set the body * // * the body is a mix of text and image */Multipart contentPart = new MimeMultipart ("related"); body. setContent (contentPart); MimeBodyPart content = new MimeBodyPart (); // text MimeBodyPart img = new MimeBodyPart (); // picture contentPart. addBodyPart (content); contentPart. addBodyPart (img); ByteArrayDataSource fileds = new ByteArrayDataSource (new FileInputStream ("D: \ picture \ jpg \ touxiang.jpg"), "application/octet-stream "); dataHandler imgDataHandler = new DataHandler (fileds); // DataSource imgds = new FileDataSource ("D: \ picture \ jpg \ touxiang.jpg "); // DataHandler imgDataHandler = new DataHandler (imgds); img. setDataHandler (imgDataHandler); // note: the value of the Content-ID attribute must be added <>. It cannot be touxiang.jpg img. setHeader ("Content-ID ","
"); // Set the file name for the image. Some mailboxes use html embedded images as attachment img. setFileName ("touxianga.jpg"); // set the text Content/*** Note: To display the touxiang.jpg in HTML code, you cannot directly write the content-ID value in src. Use cid: This method */Content. setContent ("an email sent from sohu: I have an image here. Is it nice? "," Text/html; charset = UTF-8 ");/* end of body Content Setting * // set the attachment */attach1.setDataHandler (new DataHandler (new FileDataSource (" E: \ others \ firefox.txt "); attach1.setFileName (" file1.txt "); attach2.setDataHandler (new DataHandler (new FileDataSource (" E: \ others \ java.txt "))); attach2.setFileName ("file2.txt"); msg. saveChanges (); // write the email to the disk as a file. OutputStream OS = new FileOutputStream ("E: \ others \ demo. eml "); msg. writeTo (OS); OS. close (); Transport trans = session. getTransport (); trans. connect ("", 25, "zyh5540", "test"); trans. sendMessage (msg, msg. getAllRecipients ());}}


1. JavaMail jar package:

2. References:

Related Article

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: 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.