application server
(1) javax.mail.Properties class
JavaMail requires Properties to create a session object. It will look for the string "mail.smtp.host", where the property value is the host from which the email was sent.
usage:
Copy the code code as follows:
Properties props = new Properties ();
props.put("mail.smtp.host", "smtp.163.com");//You can replace it with your smtp host name.
Copy the code code as follows:
Properties props = new Properties ();
props.put("mail.smtp.host", "smtp.163.com");//You can replace it with your smtp host name.
(2) javax.mail.Session class
This Session class represents an email session in JavaMail. Every JavaMail-based application has at least one session but can have any number of sessions. In this example, the Session object needs to know the SMTP server used to handle mail.
usage:
Copy the code code as follows:
Session sendMailSession;
sendMailSession = Session.getInstance(props, null);
Copy the code code as follows:
Session sendMailSession;
sendMailSession = Session.getInstance(props, null);
(3) javax.mail.Transport class
Mail can be both sent and received. JavaMail uses two different classes to complete these two functions: Transport and Store. Transport is used to send messages, and Store is used to receive messages. For this tutorial we only need to use the Transport object.
usage:
Copy the code code as follows:
Transport transport;
transport = sendMailSession.getTransport("smtp");
Copy the code code as follows:
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". This will save us a lot of time. Because JavaMail has built-in many protocol implementation methods.
Note: JavaMail does not absolutely support every protocol. It currently supports IMAP, SMTP and POP3.
(4) javax.mail.MimeMessage class
The Message object will store the actual email message we sent. The Message object is created as a MimeMessage object and needs to know which JavaMail session should be selected.
usage:
Copy the code code as follows:
Message newMessage = new MimeMessage(sendMailSession);
Copy the code code as follows:
Message newMessage = new MimeMessage(sendMailSession);
Message newMessage = new MimeMessage(sendMailSession);
(5) javax.mail.InternetAddress class
Once you have created the Session and Message and filled in the content with the message, you can use Address to determine the address of the letter. Like Message, Address is also an abstract class. You are using the Javax.mail.internet.InternetAddress class.
usage:
Copy the code code as follows:
InternetAddress from=new InternetAddress("[email protected]");
Copy the code code as follows:
InternetAddress from=new InternetAddress("[email protected]");
(6) javax.mail.Store class
The Store class implements operations such as reading, writing, monitoring, and searching on specific email protocols. The Javax.mail.Folder class can be accessed through the Javax.mail.Store class.
usage:
Copy the code code as follows:
Store store=s.getSorte("pop3");//s is an email session
store.connect(popserver,username,password);//Log in to your mailbox through the pop address, username and password you provided
Copy the code code as follows:
Store store=s.getSorte("pop3");//s is an email session
store.connect(popserver,username,password);//Log in to your mailbox through the pop address, username and password you provided
(7) javax.mail.Folder class
The Folder class is used to organize emails hierarchically and provides the ability to access emails in the Javax.mail.Message format.
usage:
Copy the code code as follows:
Folder folder=store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Copy the code code as follows:
Folder folder=store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
(8) javax.mail.Internet.MimeMultpart
The container that generally saves email content is the Multipart abstract class, which defines methods for adding, deleting, and obtaining different parts of the email. Since Multipart is an abstract class, we must use a concrete subclass for it, and the JavaMail API provides javax. mail.Internet.MimeMultpart class to use MimeMessage objects.
usage:
Copy the code code as follows:
MimeMultipart multipart=new MimeMultipart();
Copy the code code as follows:
MimeMultipart multipart=new MimeMultipart();
Note: One of the ways we use the MimeMultipart object is addBodyPart(), which adds a BodyPart (BodyPart class will be introduced below) object in our email content. The message can have many parts, and a BodyPart can represent one part.
(9) javax.mail.Internet.MimeBodyPart class
MimeBodyPart is a subclass of BodyPart specifically used for mimeMessage.
A MimeBodyPart object represents a portion of the content of a MimeMessage object. Each MimeBodyPart is considered to have two parts:
⊙A MIME type
⊙Match content of this type
usage:
Copy the code code as follows:
MimeBodyPart mdp=new MimeBodyPart();
String text="Hello JavaMail!";
mdp.setContent(text,"text/plain");//Define the MIME type as text/plain and set the content of MimeBodyPart.
Copy the code code as follows:
MimeBodyPart mdp=new MimeBodyPart();
String text="Hello JavaMail!";
mdp.setContent(text,"text/plain");//Define the MIME type as text/plain and set the content of MimeBodyPart.
(10) javax.activation.DataHandler class (included in JAF)
The JavaMail API does not limit messages to text only; any form of message may be contained as part of a MimeMessage. In addition to text messages, it is very common to include them as file attachments as part of email messages. The JavaMail API provides a way to allow us to Convenient method for containing non-text BodyPart objects.
usage:
Copy the code code as follows:
DataHandler dh=new DataHandler(text,type);
mdp.setDatahandler(dh);//mdp is a MimeBodyPart object
Copy the code code as follows:
DataHandler dh=new DataHandler(text,type);
mdp.setDatahandler(dh);//mdp is a MimeBodyPart object
(11) javax.activation.FileDataSource class (included in JAF)
A FileDataSource object can represent local files and resources that are directly accessible to the server. A local file can be attached to a mimeMessage object by creating a new MimeBodyPart object.
usage:
Copy the code code as follows:
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); //Add MimeBodyPart to the current message MimeMultipart object
Copy the code code as follows:
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); //Add MimeBodyPart to the current message MimeMultipart object
(12) javax.activation.URLDataSource class (included in 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:
Copy the code code as follows:
//The only difference from FileDataSource is the setting of the data source:
URLDataSource uds=new URLDataSource("/JAVA/UploadFiles_6441/200703/20070320105128501.gif");