Javamail supplementary class

Source: Internet
Author: User

(1) javax. Mail. properties class
Javamail needs properties to create a session object. It will look for the string "mail. SMTP. Host". The attribute value is the host that sent the mail.

Usage:
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. Each 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 process the mail.

Usage:
Session sendmailsession;
Sendmailsession = session. getinstance (props, null );

(3) javax. Mail. Transport class
An email can be sent or received. Javamail uses two different classes to complete these two functions: Transport and store. transport are used to send information, while store is used to receive mail. For this tutorial, we only need to use the transport object.

Usage:
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 used by the object, such as "SMTP ". This saves us a lot of time. Because javamail has many built-in protocol implementation methods.

Note: javamail does not absolutely support every protocol. Currently, it supports IMAP, SMTP, and POP3.

(4) javax. Mail. mimemessage class
The message object stores the actually sent email information. The message object is created as a mimemessage object and needs to know which javamail session should be selected.

Usage:
Message newmessage = new mimemessage (sendmailsession );

(5) javax. Mail. internetaddress class
Once you have created a session and message, and entered the content into the message, you can use address to determine the mail address. 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 reading, writing, monitoring, and searching operations on specific mail protocols. You can use the javax. Mail. Store class to access the javax. Mail. Folder class.

Usage:
Store store = S. getsorte ("POP3"); // s is an email session
Store. Connect (popserver, username, password); // use the pop address, user name, and password you provided to log on to your mailbox

(7) javax. Mail. Folder class
The folder class is used to organize mails hierarchically and provide the ability to access emails in javax. Mail. Message format.

Usage:
Folder folder = store. getfolder ("inbox ");
Folder. Open (Folder. read_only );

(8) javax. Mail. Internet. mimemultpart
Generally, the container that saves the email content is a multipart abstract class, which defines how to add and delete and obtain different parts of the email content. because multipart is an abstract class, we must use a specific subclass for it. The javamail API provides javax. mail. internet. the mimemultpart class is used to use the mimemessage object.

Usage:
Mimemultipart multipart = new mimemultipart ();

Note: One method of using the mimemultipart object is addbodypart (), which adds the bodypart (bodypart class will be introduced below) object in our email content. messages can have many parts. A bodypart can represent a part.

(9) javax. Mail. Internet. mimebodypart class

The mimebodypart is a subclass of the bodypart used in mimemessage.
The mimebodypart object represents a part of the content of a mimemessage object. Each mimebodypart is considered to have two parts:
⊙ A MIME type
⊙ Match content of this type

Usage:
Mimebodypart MDP = new mimebodypart ();
String text = "Hello javamail! ";
MDP. setcontent (text, "text/plain"); // defines the MIME type as text/plain and sets the mimebodypart content.

(10) javax. Activation. datahandler class (included in JAF)
The javamail API does not limit that information is only text. Any form of information may be part of mimemessage. in addition to text information, it is common to include attachments in emails. by using the datahandler object, javamail API provides a simple method that allows us to include non-text bodypart objects.

Usage:
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 resources that can be directly accessed by local files and servers. 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); // set the data source
Mm. addbodypart (MDP); // Add mimebodypart to the current message mimemultipart object.

(12) javax. Activation. urldatasource class (included in JAF)
Remote resources, URLs do not point to them and are represented by a urldatasource object. A remote resource can be attached to a mimemessage object by creating a new mimebodypart object (similar to filedatasource ).

Usage:
The only difference from filedatasource is the data source settings:
Urldatasource UDS = new urldatasource ("http://www.cnjsp.com/logo.gif ")

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.