JSP and JavaMail (2)

Source: Internet
Author: User
3. Introduction to common classes of JavaMail
Instructions in advance:
Users who have never used JavaMail can understand these introductions, but it doesn't matter. There are specific examples in the subsequent articles. You can also come back to view the usage of these classes.
(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.
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.