JSP and JavaMail (2)

xiaoxiao2021-03-17  184

Original CN-Java original: javazealot

3. General class introduction of JavaMail

Example:

People who have never used Javamail may not understand these introductions, but don't matter, behind the article has specific instances, and you can follow these classes.

(1) Javax.mail.properties class

JavaMail needs Properties to create a session object. It will look for strings "mail.smtp.host", the attribute value is the host that sends an email.

usage:

Properties PROPS = New Properties ();

Props.Put ("mail.smtp.host", "smtp.163.com"); // can be replaced with your SMTP hostname.

(2) Javax.mail.Session class

This session class represents a mail session in JavaMail. Each JavaMail-based application has at least one session but can have any session. In this example, the Session object needs to know the SMTP server used to process the mail.

usage:

Session SendmailSession;

SendmailSession = session.getInstance (PROPS, NULL);

(3) Javax.mail.Transport class

The message can be sent or can be received. Javamail uses two different classes to complete these two features: Transport and Store. Transport is used to send information, while the Store is used. We only need to use Transport objects for this tutorial.

usage:

Transport Transport;

Transport = sendmailsession.getTransport ("SMTP");

TRANSPORT is initialized with the GetTransport method of the JavaMail Session object. The passed string declares the protocols to be used by the object, such as "SMTP". This will save us a lot of time. Because Javamail has a lot of protocols in JavaMail.

Note: JavaMail is not absolutely supporting each protocol, currently supporting IMAP, SMTP, and POP3.

(4) Javax.mail.mimeMimeMessage class

The Message object stores the email information we actually sent, and the Message object is created as a MIMEMessage object and needs to know which JavaMail Session should be selected.

usage:

Message newMessage = New MimeMailSession;

(5) Javax.mail.InternetDress class

Once you have created session and messages, you can use Address to determine the letters address with address after filling the content. Like Message, Address is also an abstract class. You are using the Javax.mail.Internet.InternetAddress class.

usage:

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

(6) Javax.mail.Store class

The Store class implements read, write, monitor, lookup, etc. on a particular email protocol. You can access the Javax.mail.Folder class through the Javax.mail.Store class.

usage:

Store Store = S.Getsorte ("POP3"); // S is a mail session

Store.Connect (POPSERVER, Username, Password); // Log in to your mailbox by the POP address you provide

(7) Javax.mail.folder class

The Folder class is used to hierarchically messages and provide the ability to access Email in javax.mail.Message format. usage:

Folder folder = store.getfolder ("inbox");

Folder.open (Folder.Read_only);

(8) javax.mail.internet.mimemultpart

The container that typically stores email is a Multipart abstraction class that defines how to increase and delete and obtain different parts of email. Because Multipart is an abstract class, we must use a specific subclass, JavaMail API provides Javax. The mail.internet.mimemultpart class is used to use the MIMEMESSAGE object.

usage:

MimeMultipart Multipart = new mimemultipart ();

Note: One way we use MIMEMULTIPART object is AddBodypart (), which adds Bodypart in our email content (Bodypart class to introduce) object. Message can have a lot of parts, a bodypart can represent a part.

(9) Javax.mail.Internet.mimebodypart class

MIMEBODYPART is a subclass for Bodypart for MIMEMessage.

The MIMEBODYPART object represents a part of a mimeMessage object. Each mimebodypart is considered to have two parts:

⊙ A MIME type

⊙ Match this type of content

usage:

MIMEBODYPART MDP = New MimeBodypart ();

String text = "Hello JavaMail!";

Mdp.setContent (Text, "Text / Plain"); // Defines the MIME type to Text / Plain and set the contents of MIMEBODYPART.

(10) Javax.Activation.DataHandler class (included in JAF)

JavaMail API does not restrict information only for text, any form of information may be part of the MIMEMessage. In addition to text information, as part of the file attachment included in the email information is very popular. JavaMail API provides a allowable us by using the DataHandler object A simplicity of the non-text bodypart object.

usage:

DataHandler DH = New DataHandler (Text, Type);

MDP.SetDataHandler (DH); // MDP is an MIMEBODYPART object

(11) Javax.Activation.fileDataSource class (included in JAF)

A FileDataSource object can represent a local file and a resource that can be accessed 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 the data source

mm.addbodypart (MDP); / / Add MIMEBODYPAR for the current message MIMEMULTIPART object

(12) javax.activation.urldatasource class (included in JAF)

Remote resources, the URL does not point to them, indicated by a URLDataSource object. A remote resource can be attached to a mimeMessage object by creating a new MIMEBODYPART object (similar to FileDataSource is almost).

usage:

The only difference from FileDataSource is the setting of the data source:

转载请注明原文地址:https://www.9cbs.com/read-129493.html

New Post(0)