JSP and JavaMail 2 (commonly used class introduction)

Source: Internet
Author: User
Tags abstract include mail resource access
The common class introduction of JS 3.JavaMail

Prior description:

People who have never used javamail may not be able to understand these introductions, but it does not matter, the following articles have specific examples, and then can be inverted back to see the use of these classes.


(1) Javax.mail.Properties class
JavaMail requires properties to create a Session object. It will look for the string "Mail.smtp.host", the property value being the host that sent the message.

Usage:
Properties Props = new properties ();
Props.put ("Mail.smtp.host", "smtp.163.com");//Can be replaced with your SMTP host name.


(2) Javax.mail.Session class
This session class represents a message session in JavaMail. Each JavaMail 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 that is used to process the message.

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


(3) Javax.mail.Transport class
Messages are either sent or can be received. JavaMail uses two different classes to accomplish these two functions: transport and store. Transport is used to send information, and the store is used to collect letters. For this tutorial we only need to use the transport object.

Usage:
Transport transport;
Transport = Sendmailsession.gettransport ("SMTP");

Initializes the transport with the Gettransport method of the JavaMail session object. A string that passes past declares the protocol to use for the object, such as "SMTP." This will save us a lot of time. Because JavaMail has a lot of protocols implemented in the Territory.

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


(4) Javax.mail.MimeMessage class
The Message object stores the e-mail messages we actually send, and the messages object is created as a MimeMessage object and needs to know which JavaMail session should be chosen.

Usage:
Message Newmessage = new MimeMessage (sendmailsession);


(5) Javax.mail.InternetAddress class
Once you have created the session and message and filled it in with messages, you can use address to determine the mailing addresses. As with the message, the address is 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, and find operations on specific mail protocols. The Javax.mail.Folder class can be accessed through the Javax.mail.Store class.

Usage:
Store Store=s.getsorte ("POP3");//s for a mail session
Store.connect (Popserver,username,password)//through the pop address, username and password you provide to login to your mailbox


(7) Javax.mail.Folder class
The folder class is used to organize messages hierarchically and to 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
A container that typically holds e-mail content is a multipart abstract class that defines ways to add and remove and get different portions of e-mail. Since multipart is an abstract class, we have to use a specific subclass for it, JavaMail The API provides the Javax.mail.Internet.MimeMultpart class to use the MimeMessage object.

Usage:
Mimemultipart multipart=new Mimemultipart ();

Note: One way we use the Mimemultipart object is Addbodypart (), which adds bodypart to our email content (BodyPart class is described below). Messages can have many parts, A bodypart can represent a part.


(9) Javax.mail.Internet.MimeBodyPart class

MimeBodyPart is a subclass of BodyPart specifically used for 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 this type of content

Usage:
MimeBodyPart mdp=new MimeBodyPart ();
String text= "Hello javamail!";
Mdp.setcontent (text, "Text/plain");//define MIME type as Text/plain and set MimeBodyPart content.


(a) Javax.activation.DataHandler class (included in JAF)
JavaMail APIs do not restrict information to text only, and any form of information can be a part of the mimemessage. In addition to textual information, it is common to include file attachments as part of the e-mail message. The JavaMail API provides an easy way to allow us to include text-BodyPart objects by using the DataHandler object.

Usage:
DataHandler dh=new DataHandler (text,type);
Mdp.setdatahandler (DH);//MDP is a MimeBodyPart object


(one) Javax.activation.FileDataSource class (included in JAF)
A Filedatasource object can represent a resource that local files and servers 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 up a data source
Mm.addbodypart (MDP); Mimemultipart object for current message MimeBodyPart


(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 (similar to Filedatasource) by creating a new MimeBodyPart object.

Related Article

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.