Transferred from: http://haolloyin.blog.51cto.com/1177454/353849/
The JavaMail 1.4.3 version was downloaded in http://java.sun.com/products/javamail/, and if the JDK used is 6.0 then the activation is not downloaded. Download the javamail-1.4.3.zip of the compressed package, extract the Mail.jar package, configure the environment variables can be used. From the unpacked folder there is javamail-1.4.pdf this JavaMail design specification document. Although it is English, but a little look at me to cut off 4 more useful pictures, I think it is necessary to understand, as follows:
1, JavaMail architecture level diagram:
2, JavaMail class level diagram:
3. The class diagram of some of the most important classes in JavaMail:
4, MimeMessage class of the specific class diagram:
JavaMail documents have time to understand, using JavaMail development mail application can refer to the JavaMail development document after decompression, the following code implementation to create a plain text message:
Messages in plain text format:
- Import Java.io.FileOutputStream;
- Import Java.util.Date;
- Import java.util.Properties;
- Import Javax.mail.Message;
- Import javax.mail.Session;
- Import javax.mail.internet.InternetAddress;
- Import Javax.mail.internet.MimeMessage;
- /**
- * Create messages in plain text format, save As Outlook ". EML" message Format
- * @author Haolloyin
- */
- Public class TextMessage {
- public static void Main (string[] args) throws exception{
- String from = "[email protected]";
- String to = "[email protected]";
- String subject = "Create a plain text message!" ";
- String BODY = "plain text mail test!!!" ";
- //Create environment information and session information required for the mail application
- Session session = Session.getdefaultinstance (new Properties ());
- //Create a MimeMessage instance based on the Session instance above, i.e. a message
- MimeMessage msg = New MimeMessage (session);
- //Set Sender address
- Msg.setfrom (new InternetAddress (from));
- //Set recipient address
- Msg.setrecipients (Message.RecipientType.TO, Internetaddress.parse (to));
- //Set e-mail theme
- Msg.setsubject (subject);
- //Set Send time
- Msg.setsentdate (new Date ());
- //Set e-mail body part
- Msg.settext (body);
- //must save changes to the MimeMessage instance
- Msg.savechanges ();
- //write the contents of the Msg object into the textmail.eml file of the current file
- Msg.writeto (new FileOutputStream ("textmail.eml"));
- }
- }
Compile run, get textmail.eml file, double click to automatically open with Outlook, such as:
Click File in Outlook-> "Properties"-> "Details" to see the message header, and then click "Mail Source" to see the source file content of the message, such as:
Note that the above selected content, Content-transfer-encoding:base64 description is converted with BASE64 encoding, so the body part of the message such as:
Because the subject and body in the message are used in Chinese, the character set encoding is CHARSET=GBK.
HTML-formatted messages:
In the same way to create an HTML-formatted message, the above code slightly modified, the code is as follows:
- Import Java.io.FileOutputStream;
- Import Java.util.Date;
- Import java.util.Properties;
- Import Javax.mail.Message;
- Import javax.mail.Session;
- Import javax.mail.internet.InternetAddress;
- Import Javax.mail.internet.MimeMessage;
- /**
- * Create an HTML-formatted message saved as an ". eml" file for Outlook
- * @author Haolloyin
- */
- Public class Htmlmessage {
- public static void Main (string[] args) throws exception{
- String from = "[email protected]";
- String to = "[email protected]";
- String subject = "Create an HTML-formatted message!" ";
- String BODY = "mail test in
- "<a href = http://haolloyin.blog.51cto.com/> ant </a>";
- //Create environment information and session information required for the mail application
- Session session = Session.getdefaultinstance (new Properties ());
- //Create a MimeMessage instance based on the Session instance above, i.e. a message
- MimeMessage msg = New MimeMessage (session);
- //Set Sender address
- Msg.setfrom (new InternetAddress (from));
- //Set recipient address
- Msg.setrecipients (Message.RecipientType.TO, Internetaddress.parse (to));
- //Set e-mail theme
- Msg.setsubject (subject);
- //Set Send time
- Msg.setsentdate (new Date ());
- //Set e-mail body part
- Msg.settext (body);
- msg.setcontent (Body, "text/html;charset = GBK");
- //Save changes to the MimeMessage instance
- Msg.savechanges ();
- //write the contents of the Msg object to the file
- Msg.writeto (new FileOutputStream ("htmlmail.eml"));
- }
- }
Note the msg.setcontent (body, "text/html;charset = GBK") in the above code; Statement, double-clicking the generated file is automatically opened with Outlook, such as:
We notice that the encoding has become quoted-printable, which, like the BASE64 encoding, converts pure binary data into printable ASCII characters, and the specific differences and uses refer to the information.
Currently, it is not possible to send mail to the specified mailbox, just to create a message.
Summary:
1. Understanding the MIME protocol and the organizational structure of the MIME message is helpful for understanding the class diagram given above;
2, if the mail subject and body are not involved in Chinese, then the entire message will not be transcoded, but with the most original 7bit encoding format, you can try a look at the effect.
My related articles:
Plot to send messages manually using the Telnet program
Java implementation of a simple e-mail Sender program
Diagram using the Telnet program to receive messages and their processes manually
JavaMail Getting Started: Creating plain Text, HTML-formatted messages