JavaMail API Detailed decomposition

Source: Internet
Author: User
Tags imap rfc save file all mail

One of the advantages of using the spring framework is that it integrates with other technologies such as JavaMail, task scheduling, and caching strategies. This is explained in detail today in Java Mail. The JavaMail API is designed to be protocol agnostic, but for now we are not able to overcome the constraints of these protocols. Before you know javamail, you need to know more about several mail protocols, SMTP POP IMAP MIME, and these protocols are analyzed in detail below:


1.SMTP protocol: The Simple Mail Transfer Protocol defines the mechanism for delivering mail. The SMTP server forwards the message to the recipient's SMTP server until it is finally acquired by the recipient via a POP or IMAP protocol. delivery message mechanism. (such as the software on the ipad QQ mailbox if you want to receive mail, you must set up the SMTP service in our QQ mailbox to open)

2.pop:pop is a Post Office protocol, currently the 3rd version, known as POP3. Pop defines a mechanism for how a user obtains a message. It specifies that each user uses a separate mailbox. gets the message mechanism.

3.imap:imap uses the advanced protocol in receiving information, the current version is the 4th edition, so it is also known as IMAP4. It is important to note that the mail server must support this protocol when using IMAP. In this respect, we can't use IMAP as a substitute for pop, and we can't expect IMAP to be supported anywhere. If the mail server supports IMAP, then our mail program will be able to have the following features that are supported by IMAP: Each user can have multiple directories on the server, which can be shared among multiple users.
It's obviously more advanced than pop, but when trying to take IMAP, we realize that it's not perfect: because IMAP needs to receive new information from other servers, deliver that information to users, and maintain multiple directories for each user, which brings a high load on the mail server. And one of the differences between IMAP and POP is that pop users download messages from the mail server when they receive mail, and IMAP allows users to access the mail directory directly, so when the mail server makes a backup job, the mail directory for each user who uses this messaging system for a long time occupies a lot of space. This will directly cause a spike in disk space on the mail server.

4.mime:mime is not a protocol for sending messages, it defines the format of the message content as an extension of a multipurpose message: The format of the message, the format of the attachment, and so on. Some RFC standards involve MIME:RFC 822, RFC 2045, RFC 2046, RFC 2047, and interested matrixer can read about it. As developers of the JavaMail API, we don't have to care about these format definitions, but these formats are used in programs.

Second, installation environment:

1. Install JavaMail: Download JavaMail from http://java.sun.com/products/javamail/downloads/index.html and add Mail.jar file to Classpath.

2. Install the JavaBeans Activation Framework and add the Activation.jar file to the CLA.

Third, use:

1. Some core classes of Javamail.jar: Session, Message, Address, Authenticator, Transport, Store, Folder. And some of the other commonly used classes contained in the Javax.mail.internet package.

2. Session mail reply, send and receive email is based on this reply. The session object takes advantage of the Javax.util.properties object to obtain the mail server, user name, password information, and the shared information that the entire application uses.

// How to use  Newnull new= Session.getinstance ();

3. Message, after the session is established, the message body can be constructed. Use the Javax.mail.internet.Message abstract class. Use the following methods:

New Message (session);
Message.setcontent ("HelloWorld", "Text/plain"); Set non-textual information content
Message.settext ("Hello"); Set message plain text information content
Message.setsubject ("Subjecthelloworld"); Set Message subject
Message.setsentdate (date); Set the date the message was sent

4. Address class, after the session and message message body is established, use the e-mail address of the abstract class. Use the Javax.mail.internet.InternetAddress class to pass in a string of incoming mail addresses.

New InternetAddress ("[Email protected]");    New InternetAddress ("[Email protected]", "Sucre");    Set the name of the e-mail address message.setfrom;    Set the sender Message.serreplyto ();   Set Sender
Message.addfrom (address[] Address); Add multiple Senders

Message.setrecipient (type,address); Set up the recipient.
Type has three types {Message.RecipientType.To recipient Message.RecipientType.CC cc person Message.Recipienttype.BCC secret cc}

5. Authenticator the grantee class. Using the authenticator abstract class must implement the Getpasswordauthentication () method for storing the user name and password used for authentication. and to register in the session, the session will know which class to use for authentication.

Newnew myauthenticator ();
Session session = Session.getdefaultinstance (Properties,authenticator);

/*
*
* Throes IOException
*/
public class Myauthenticator extends authenticator{
Public passwordauthentication getpasswordauthentication (String param) {
String username;
String password;
StringTokenizer st = new StringTokenizer (param, ",");
Username = St.nexttoken ();
Password = st.nexttoken ();
return new Passwordauthentication (Username,password);
}
}

6. Transport abstract class implements the SMTP

Transport.send (MimeMessage message);

You can also get the transport instance corresponding to the corresponding protocol through the session and establish a connection with the mail server by passing parameters such as user name, password, and mail server hostname. and use the SendMessage () method to send the information. Finally, close the connection.
Session.setdebug (true); Monitoring the message delivery process
Message.savechanges (); Save File
Transport Transport = Session.gettransport ("SMTP"); Gets the transport class that implements the SMTP protocol
Transport.connect (Host,username,password); Connecting to a server
Transport.sendmessage (Message,message.getallrecipients ()); Send mail to all mail recipients
Transport.close (); Close the connection

A comprehensive comb of the javamail process, as follows: To obtain session session, fill message information, before Sentmessage (), you need to set the SMTP server related properties.

JavaMail API Detailed decomposition

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.