Java email development-javaMail (1)

Source: Internet
Author: User
Tags imap

The previous section introduced the basic knowledge of the email system. The blog briefly introduced the basic theoretical knowledge, which does not prevent us from developing emails. Now we start Java mail development.

 

1. Introduction to JavaMail

JavaMail, as its name implies, provides developers with programming interfaces related to email processing. It is an API released by Sun to process emails. It can easily perform some common mail Transmission. JavaMail API is a set of standard development kits provided by Sun to facilitate Java developers to send and receive emails in applications. It supports common mail protocols, such: SMTP, POP3, and IMAP. When developers use the JavaMail API to write mail processing software, they do not need to consider the implementation details at the bottom layer of the mail protocol. They only need to call the corresponding API classes in the JavaMail development kit.

 

Ii. JavaMail Development Environment

Before developing JavaMail, you must import the corresponding jar package. You can download the jar package at http://www.oracle.com/technetwork/java/javamail/index.html.

After the download is complete, decompress the package to obtain the following directory:

 

Among them, mail. jar is the core development package necessary to develop the javamail program.

Demo subdirectory: Sample program.

Demo subdirectory: Sample program.

Lib subdirectory: This directory contains five independent email Development Kits: smtp. jar, pop3.jar, imap. jar and mailapi. jar, these five jar files are for mail. jar file classification packaging. Mailapi. jar encapsulates the API class for creating mail content and calling mail sending and receiving for common developers. The other three jar files encapsulate the service implementation programs whose names correspond to the Protocol .. When compiling a Java mail program, you only need the mailapi. jar file. However, you must have the underlying service implementation program of the corresponding mail protocol at runtime. If you only need to use the mail sending function in the application, you can only install smtp. jar and mailapi. jar files. If the application only needs to use the mail receiving function, you can only install pop3.jar or imap. jar and mailapi. jar. jar file.

The Lib directory is as follows:

Iii. Introduction to JavaMail APIs

JavaMail APIs are generally divided into the following three categories by function:

1. Create and parse the mail content API. The Message class is the core API for creating and parsing emails. Its instance object represents an email.

2. API for sending emails. The Transport class is the core API class for sending mail. Its instance object represents the mail sending object that implements a certain mail sending protocol, such as the SMTP protocol.

3. API for receiving emails. The Store class is the core API class for receiving mails. Its instance object represents the mail receiving object that implements a Certain Mail receiving protocol, such as POP3 protocol.

The workflow of JavaMail is as follows:

In this workflow, four objects, namely Message, Session, Transport, and Store, are closely connected to send and receive an email. We only need to understand the functions and functions of these four core APIs, and understand their working relationships and email workflow, so that we can easily compile JavaMail. For the auxiliary classes of JavaMail, when we need them, we can look at the document to solve the problem.

Core API introduction:

Message

The Message class is the core API for creating and parsing emails. Its instance object represents an email. When the client sends an email, it first uses the mail API to encapsulate the data of the created email into the Message object, and then transmits the object to the mail sending API. When the client receives an email, the mail receiving API stores the received oil price data in the Message instance object. The client uses the mail parsing API to parse the received email data from this object.

Session

The Session class is used to define the environment information required by the entire application and collect the Session information for establishing a network connection between the client and the mail server, for example, the host name and port number of the email server, and the mail sending and receiving protocols used. The Session Object constructs the Store and Transport objects for sending and receiving emails Based on the Session information, and provides information support for creating Message objects for the client.

Transport
The Transport class is the core API class for sending mail. Its instance object represents the mail sending object that implements a certain mail sending protocol, such as the SMTP protocol. After the client program creates a Message object, it only needs to use the mail sending API to obtain the Transport object, then pass the Message object to the Transport object, and call its sending method, you can send an email to the specified SMTP server.
Store
The Store class is the core API class for receiving mails. Its instance object represents the mail receiving objects that implement a certain mail receiving protocol, such as POP3 and IMAP protocols. When receiving emails, the client only needs to use the mail receiving API to obtain the Store object, and then call the Receiving Method of the Store object to obtain the mail data from the specified POP3 server, and encapsulate the mail data into the Message object that represents the mail.

 

Iv. Instances

1 public class JavaMail_01 {2 public static void main (String [] args) throws Exception {3 Properties props = new Properties (); 4 props. setProperty ("mail. smtp. auth "," true "); 5 props. setProperty ("mail. transport. protocol "," smtp "); 6 7 Session session = Session. getInstance (props); 8 session. setDebug (true); 9 10 Message msg = new MimeMessage (session); 11 msg. setSubject ("this is a test program .... "); 12 msg. setText (" Hello! This is my first javamail program --- Chen Ming "); 13 msg. setFrom (new InternetAddress ("****** @ 163.com"); 14 15 Transport transport = session. getTransport (); 16 transport. connect ("smtp.163.com", 25, "********", "*********"); 17 transport. sendMessage (msg, new Address [] {new InternetAddress ("chenssy995812509@sina.cn")}); 18 19 System. out. println ("email sent successfully... "); 20 transport. close (); 21} 22}

 

 

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.