JavaMail Getting Started: Creating plain Text, HTML-formatted messages

Source: Internet
Author: User
Tags base64 telnet program

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:

  1. Import Java.io.FileOutputStream;
  2. Import Java.util.Date;
  3. Import java.util.Properties;
  4. Import Javax.mail.Message;
  5. Import javax.mail.Session;
  6. Import javax.mail.internet.InternetAddress;
  7. Import Javax.mail.internet.MimeMessage;
  8. /**
  9. * Create messages in plain text format, save As Outlook ". EML" message Format
  10. * @author Haolloyin
  11. */
  12. Public class TextMessage {
  13. public static void Main (string[] args) throws exception{
  14. String from = "[email protected]";
  15. String to = "[email protected]";
  16. String subject = "Create a plain text message!"  ";
  17. String BODY = "plain text mail test!!!"  ";
  18. //Create environment information and session information required for the mail application
  19. Session session = Session.getdefaultinstance (new Properties ());
  20. //Create a MimeMessage instance based on the Session instance above, i.e. a message
  21. MimeMessage msg = New MimeMessage (session);
  22. //Set Sender address
  23. Msg.setfrom (new InternetAddress (from));
  24. //Set recipient address
  25. Msg.setrecipients (Message.RecipientType.TO, Internetaddress.parse (to));
  26. //Set e-mail theme
  27. Msg.setsubject (subject);
  28. //Set Send time
  29. Msg.setsentdate (new Date ());
  30. //Set e-mail body part
  31. Msg.settext (body);
  32. //must save changes to the MimeMessage instance
  33. Msg.savechanges ();
  34. //write the contents of the Msg object into the textmail.eml file of the current file
  35. Msg.writeto (new FileOutputStream ("textmail.eml"));
  36. }
  37. }

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:

  1. Import Java.io.FileOutputStream;
  2. Import Java.util.Date;
  3. Import java.util.Properties;
  4. Import Javax.mail.Message;
  5. Import javax.mail.Session;
  6. Import javax.mail.internet.InternetAddress;
  7. Import Javax.mail.internet.MimeMessage;
  8. /**
  9. * Create an HTML-formatted message saved as an ". eml" file for Outlook
  10. * @author Haolloyin
  11. */
  12. Public class Htmlmessage {
  13. public static void Main (string[] args) throws exception{
  14. String from = "[email protected]";
  15. String to = "[email protected]";
  16. String subject = "Create an HTML-formatted message!"  ";
  17. String BODY = "mail test in
  18. "<a href = http://haolloyin.blog.51cto.com/> ant </a>";
  19. //Create environment information and session information required for the mail application
  20. Session session = Session.getdefaultinstance (new Properties ());
  21. //Create a MimeMessage instance based on the Session instance above, i.e. a message
  22. MimeMessage msg = New MimeMessage (session);
  23. //Set Sender address
  24. Msg.setfrom (new InternetAddress (from));
  25. //Set recipient address
  26. Msg.setrecipients (Message.RecipientType.TO, Internetaddress.parse (to));
  27. //Set e-mail theme
  28. Msg.setsubject (subject);
  29. //Set Send time
  30. Msg.setsentdate (new Date ());
  31. //Set e-mail body part
  32. Msg.settext (body);
  33. msg.setcontent (Body, "text/html;charset = GBK");
  34. //Save changes to the MimeMessage instance
  35. Msg.savechanges ();
  36. //write the contents of the Msg object to the file
  37. Msg.writeto (new FileOutputStream ("htmlmail.eml"));
  38. }
  39. }

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

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.