Common classes based on JavaMail are described in detail

  • 2020-04-01 02:17:32
  • OfStack

Application server
(1) the javax.mail. Mail. The Properties
JavaMail needs Properties to create a session object. It will look for the string "mail.smtp.host", whose property value is the host from which the message was sent.

Usage:


Properties props = new Properties ();   
   props.put("mail.smtp.host", "smtp.163.com");//You can change your SMTP host name.  


Properties props = new Properties ();  
   props.put("mail.smtp.host", "smtp.163.com");//You can change your SMTP host name.

(2) the javax.mail. Mail. The Session classes
This Session class represents an email Session in JavaMail. Every application based on JavaMail has at least one Session but can have as many sessions as you like. In this case, the Session object needs to know which SMTP server is used to process the mail.

Usage:


Session sendMailSession;   
   sendMailSession = Session.getInstance(props, null);   


Session sendMailSession;  
   sendMailSession = Session.getInstance(props, null);  

(3) the javax.mail. Mail. Transport class
Mail can be either sent or received. JavaMail USES two different classes to do both: Transport and Store. Transport is used to send messages, and Store is used to receive messages. For this tutorial we only need Transport objects.

Usage:


Transport transport;   
  transport = sendMailSession.getTransport("smtp");  


Transport transport;
  transport = sendMailSession.getTransport("smtp");

Use the getTransport method of the JavaMail Session object to initialize the Transport. The passed string declares the protocol to be used by the object, such as "SMTP". It will save us a lot of time. Because JavaMail has a lot of protocol implementations in place.

Note: JavaMail does not support every protocol, and currently supports IMAP, SMTP, and POP3.

(4) the javax.mail. Mail. MimeMessage class
The Message object will store the actual E-mail Message we sent, the Message object is created as a MimeMessage object and needs to know which JavaMail session to select.

Usage:


Message newMessage = new MimeMessage(sendMailSession);   


Message newMessage = new MimeMessage(sendMailSession);   
Message newMessage = new MimeMessage(sendMailSession); 

(5) the javax.mail. Mail. InternetAddress class
Once you have created the Session and Message and filled the contents into the Message, you can use Address to determine the letter Address. Like Message, Address is an abstract class. You use the javax.mail. Mail. Internet, InternetAddress class.

Usage:


InternetAddress from=new InternetAddress("xxf@cafe.com");   


InternetAddress from=new InternetAddress("xxf@cafe.com"); 

(6) javax.mail. Mail. Store classes
The Store class implements read, write, monitor, find, and so on on a particular mail protocol. The javax.mail.store class provides access to the javax.mail.folder class.

Usage:


Store store=s.getSorte("pop3");//S is an email session & NBSP;  
store.connect(popserver,username,password);//Login to your email & NBSP; with the pop address, username and password you provide;


Store store=s.getSorte("pop3");//S is an email session & NBSP;  
store.connect(popserver,username,password);//Use the pop address, username and password you provide to log into your email

(7) javax.mail. Mail. Folder
The Folder class is used to organize mail hierarchically and provides the ability to access email in the javax.mail.message format.

Usage:


Folder folder=store.getFolder("INBOX");   
folder.open(Folder.READ_ONLY);   


Folder folder=store.getFolder("INBOX");  
folder.open(Folder.READ_ONLY);   

(8) javax.mail. Mail. Internet, MimeMultpart
Generally save the email content container is a Multipart abstract class, which defines the add and remove different parts and email content method. Because the Multipart is an abstract class, we must use for it a concrete subclass, JavaMail API provides javax.mail. Mail. Internet, MimeMultpart class to use MimeMessage object.

Usage:


MimeMultipart multipart=new MimeMultipart();   


MimeMultipart multipart=new MimeMultipart(); 

Note: one way we use the MimeMultipart object is addBodyPart(), which adds a BodyPart(the BodyPart class is described next) object to our E-mail content.

(9) javax.mail. Mail. Internet, MimeBodyPart class
MimeBodyPart is a subclass of BodyPart specifically for mimeMessage.
The MimeBodyPart object represents part of the contents of a MimeMessage object. Each MimeBodyPart is considered to have two parts:
Even though a MIME type
Even though matches the content of this type

Usage:


MimeBodyPart mdp=new MimeBodyPart();   
String text="Hello JavaMail!";   
mdp.setContent(text,"text/plain");//Define the MIME type as text/plain and set the contents of the MIME bodypart.


    MimeBodyPart mdp=new MimeBodyPart();  
    String text="Hello JavaMail!";  
    mdp.setContent(text,"text/plain");//Define the MIME type as text/plain and set the contents of the MIME bodypart.

(10) javax.mail. Activation. DataHandler class (included in the JAF)
The JavaMail API does not limit information to text only, and any form of information can become part of the MimeMessage itself.

Usage:


    DataHandler dh=new DataHandler(text,type);   
    mdp.setDatahandler(dh);//MDP is a MimeBodyPart object & NBSP;


    DataHandler dh=new DataHandler(text,type);  
    mdp.setDatahandler(dh);//MDP is a MimeBodyPart object & NBSP;

(11) javax.mail. Activation. FileDataSource class (included in the JAF)
A FileDataSource object can represent local files and resources that the server can access directly. A local file can be attached to a mimeMessage object by creating a new MimeBodyPart object.

Usage:


MimeMultipart mm=new MimeMultipart();   
MimeBodyPart mdp=new MimeBodyPart();   
FileDataSource fds=new FileDataSource("c:/exam.txt");   
mdp.setDataHandler(new DataHandler(fds)); //Setting data source & NBSP;  
mm.addBodyPart(mdp); //Adds the mime bodypart & paste to the current message MimeMultipart object;  


MimeMultipart mm=new MimeMultipart();
MimeBodyPart mdp=new MimeBodyPart();
FileDataSource fds=new FileDataSource("c:/exam.txt");
mdp.setDataHandler(new DataHandler(fds)); //Set data source
mm.addBodyPart(mdp); //Adds the MimeBodyPart to the current message MimeMultipart object

(12) javax.mail. Activation. URLDataSource class (included in the JAF)
Remote resources, to which the URL does not point, are represented by a URLDataSource object. A remote resource can be attached to a mimeMessage object by creating a new mimeBodyPart object (similar to a FileDataSource).

Usage:


//The only difference from FileDataSource is the setting of the data source :& NBSP;  
URLDataSource uds=new URLDataSource("/JAVA/UploadFiles_6441/200703/20070320105128501.gif");  


Related articles: