Use JavaMail in Java programs to send messages with pictures and attachments _java

Source: Internet
Author: User
Tags base64 mixed

We often add attachments to the message to achieve the goal of transferring larger files. And the previous article just will be a picture of this machine embedded in the text of the HTML format of the message, such a message is not rich and colorful. We want a text that is illustrated with pictures and visitors with a number of attachments attached to the mail.
The following are 3 important methods in the code structure:

    1. The MimeBodyPart createattachment (String fileName) method is used to create an attachment and return;
    2. The MimeBodyPart createcontent (string body, String FileName) method is used to create the body part and return it;
    3. The MimeMessage CreateMessage method is used to invoke the two methods above to generate a message.

In fact, creating a message with an attachment is the principle of creating a message in the previous article that contains pictures in the body, and even the code is almost identical. That is, using the Filedatasource class and the DataHandler class in the JAF framework to obtain a resource file for a given path, the two classes in the JAF framework collaborate to know which MIME types the file belongs to, and they are added correctly to the corresponding message headers in the mail organization structure.

The difference is:
The inclusion of a picture in the body of the HTML format is a unique identifier for the corresponding resource file using the Mimebody.setcontentid () method, which is the Content-id header field in the structure organization format of the message in the MIME protocol;
Adding an attachment in a MIME message uses the Mimebodypart.setfilename () method to associate the resource file that the Filesourcedata object points to.
Program code:
This difference is highlighted in the following code with the orange background, and the code is as follows:

Import Java.io.FileOutputStream; 
 
Import java.util.Properties; 
Import Javax.activation.DataHandler; 
Import Javax.activation.FileDataSource; 
Import Javax.mail.Message; 
Import javax.mail.Session; 
Import javax.mail.internet.InternetAddress; 
Import Javax.mail.internet.MimeBodyPart; 
Import Javax.mail.internet.MimeMessage; 
 
Import Javax.mail.internet.MimeMultipart; 
   /** * Create an enclosed, illustrated message * @author Haolloyin/public class Withattachmentmessage {/** * Creates an attachment based on an incoming file path and returns */Public MimeBodyPart createattachment (String fileName) throws Exception {MimeBodyPart Attachmentpart = new Mi 
    Mebodypart (); 
    Filedatasource FDS = new Filedatasource (fileName); 
    Attachmentpart.setdatahandler (New DataHandler (FDS)); 
    Attachmentpart.setfilename (Fds.getname ()); 
  return attachmentpart; 
      /** * Create an illustrated body part based on the incoming message body and file path * * Public MimeBodyPart Createcontent (string, String fileName) Throws Exception {//For saving final body part MimEbodypart contentbody = new MimeBodyPart (); 
 
    Used to combine text and pictures, the "related" type of Mimemultipart object Mimemultipart contentmulti = new Mimemultipart ("related"); 
    The text part of the body mimebodypart TextBody = new MimeBodyPart (); 
    Textbody.setcontent (Body, "TEXT/HTML;CHARSET=GBK"); 
 
    Contentmulti.addbodypart (TextBody); 
    The picture part of the body mimebodypart jpgbody = new MimeBodyPart (); 
    Filedatasource FDS = new Filedatasource (fileName); 
    Jpgbody.setdatahandler (New DataHandler (FDS)); 
    Jpgbody.setcontentid ("logo_jpg"); 
 
    Contentmulti.addbodypart (Jpgbody); 
    The Mimemultipart object of the "related" type above is used as the body of the message contentbody.setcontent (CONTENTMULTI); 
  return contentbody; /** * Creates a mixed MIME message based on the incoming Seesion object */Public MimeMessage CreateMessage (sessions session) throws Exceptio 
    n {String from = test_hao@163.com; 
    String to = "test_hao@sina.cn"; String Subject = "Create an enclosed, illustrated message!" 
    "; String BODY = " 

Compile, run generate withattachmentmail.eml file, double-click Open, the following figure:

Look at the file you opened with Outlook, and there's a. Java source file and a MP3 file in the attachment bar.
To open the original contents of a message:
To view the original content of the generated message, click on "File", "Properties", "mail Source", or use the EditPlus, UltraEdit, and other text editors to open the line directly, the following is the original:

from:test_hao@163.com to:test_hao@sina.cn Message-id: <56667.2.1279677956578.javamail.administrator@ Www-1477ff1578b> Subject: =? GBK?  
  b?tls9qmtauqy4vbz+oalnvm7esqldr7xe08q8/qoh?= mime-version:1.0 content-type:multipart/mixed; boundary= "----=_part_1_13249998.1279677956546"------=_part_1_13249998.1279677956546 content-type:application/ Octet-stream; Name=snake.java content-transfer-encoding:base64 content-disposition:attachment; Filename=snake.java//Omitting the contents of the Snake.java attachment after BASE64 transcoding------=_part_1_13249998.1279677956546 Content-type:applicat Ion/octet-stream; Name=meng.mp3 content-transfer-encoding:base64 content-disposition:attachment; Filename=meng.mp3//Omitting the contents of the Meng.java attachment after BASE64 transcoding------=_part_1_13249998.1279677956546 content-type:multipart/r  
  elated; boundary= "----=_part_0_3373112.1279677956546"------=_part_0_3373112.1279677956546 content-type:text/html; CHARSET=GBK content-transfer-encoding:quoted-printable  

The important part has been pointed out with the background color, interested in the in-depth understanding of the MIME protocol to the message structure of the organization of the canonical format.

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