It programmer development Essentials-all kinds of resources download list, history of the most IT resources, personal collection summary.
The overall structure of the composite message
the composition relationship in MIME
analysis of hierarchical structure of compound message dividing line
Mail Organization-related APIs
The MimeMessage class represents the entire message.
The MimeBodyPart class represents a MIME message for a message.
The Mimemultipart class represents a combined MIME message that is composed of multiple MIME messages.
JavaMail generate a composite message that contains attachments and nested pictures:
Package edu.mail.util;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.OutputStream;
Import java.util.Properties;
Import Javax.activation.DataHandler;
Import Javax.activation.DataSource;
Import Javax.activation.FileDataSource;
Import Javax.mail.Message;
Import javax.mail.MessagingException;
Import javax.mail.Session;
Import Javax.mail.Message.RecipientType;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeBodyPart;
Import Javax.mail.internet.MimeMessage;
Import Javax.mail.internet.MimeMultipart;
public class SendMultipartMessageDemo3 {public static void main (string[] args) throws Messagingexception, IOException {
Properties Props = new properties ();
Session session = Session.getinstance (props);
Session.setdebug (TRUE);
Message msg = new MimeMessage (session);
Msg.setsubject ("The first multipart javaMail");
Set Sender Msg.setfrom (new internetaddress ("xh216319@163.com"));
Set recipientMsg.setrecipients (recipienttype.to, Internetaddress.parse ("itstar1965@sina.com,xh216319@163.com"));
Set cc person Msg.setrecipient (recipienttype.cc,new internetaddress ("itstar1965@sohu.com"));
Set the Msg.setrecipient (Recipienttype.bcc,new internetaddress ("1277040964@qq.com"));
Mimemultipart Msgmultipart = new Mimemultipart ("mixed");
Msg.setcontent (Msgmultipart);
Create a content component and Attch component for the message object MimeBodyPart content = new MimeBodyPart ();
MimeBodyPart attch1 = new MimeBodyPart ();
MimeBodyPart attch2 = new MimeBodyPart ();
Add two Attachment objects and body objects to the Msgmultipart object Msgmultipart.addbodypart (content);
Msgmultipart.addbodypart (ATTCH1);
Msgmultipart.addbodypart (ATTCH2);
Associate a data source for attachment 1 DataSource ds1 = new Filedatasource ("C:\\Documents and settings\\administrator\\ Desktop \\river.jpg");
DataHandler DH1 = new DataHandler (DS1);
Attch1.setdatahandler (DH1);
Attch1.setfilename ("river.jpg"); Associating data sources for attachment 2 DataSource DS2 = new Filedatasource ("C:\\Documents and settings\\administrator\\ Desktop \\mail.bat");
DataHandler DH2 = new DataHandler (DS2);
Attch2.setdatahandler (DH2);
Attch2.setfilename ("Mail.bat"); Because the body content is also a multipart object, its components are related relationships, so content.setcontent (Contentmultipart) Mimemultipart Contentmultipart
= new Mimemultipart ("related");
Content.setcontent (Contentmultipart);
Create Htmlpart components and Gifpart components for contentmultipart mimebodypart Htmlpart = new MimeBodyPart ();
MimeBodyPart Gifpart = new MimeBodyPart ();
Add the Htmlpart and Gifpart objects to the Contentmultipart object Contentmultipart.addbodypart (Htmlpart);
Contentmultipart.addbodypart (Gifpart);
DataSource DS3 = new Filedatasource ("C:\\Documents and settings\\administrator\\ Desktop \\2012_03130103.JPG");
DataHandler dh3 = new DataHandler (DS3);
Gifpart.setdatahandler (DH3);
Gifpart.setfilename ("Itstar.gif");
Gifpart.setheader ("Content-location", "http://www.csdn.net/itstar.gif"); Here src= "HttP://www.csdn.net/itstar.gif "is actually pointing to its associated Gifpart object Htmlpart.setcontent (" This is the javamail of my first composite structure, my photo "," text/html;charset=utf-8 ");
//Save changes made by the message msg.savechanges (); OutputStream Ops = new FileOutputStream ("C:\\Documents and settings\\administrator\\ desktop \ \
Sendmultipartmessagedemo3.eml ");
//Writes the message object to the file stream msg.writeto (OPS);
ops.close ();
}
}
After the generated SENDMULTIPARTMESSAGEDEMO3.EML message is opened: