Parsing the basic concepts of e-mail and JavaMail API messaging features using _java

Source: Internet
Author: User
Tags imap message queue mx record

Introduction to Basic concepts of e-mail
e-mail is used for online confidence delivery and communication, and it is one of the most important Internet services. According to statistics, 30% of the business of the Internet is email related. At the same time, we can not deny that it plays a very important role in our daily life, work and office. For example: many office automation projects (OA) with the function of sending mail, if you want to use Outlook and other manual way is not suitable, in this high-speed era, we need to provide efficiency, so that the work can be automated. It also requires the ability to send mail to a number of Web sites: a welcome e-mail with a registered message for the newly registered user, an email to all the registered members of the website's latest activity information, and so on.

Before starting e-mail development: We need to understand a few concepts: e-mail systems, mail servers, e-mail, mail client software, mail transfer protocols, and e-mail transmission processes.

e-mail system
similar to the traditional postal service, e-mail system consists of electronic post office, email sending and receiving system. Senders and receivers send and receive emails via e-mail, which are actually mail client programs running on the computer. E-Post Office plays a role as a bridge, it is actually running the mail server program on the server. The e-mail processing process is similar to the postal service.

Mail Server
A mail server is a device that is used to manage e-mail delivery and dispatch. A large number of e-mail servers are available on the Internet, such as: 126, 163, Hotmail ....
The main features provided by the e-mail server are:
1, receive the user to post the mail.
2, the user posted in the mail forwarded to the target mail server.
3. Receive messages forwarded by other e-mail servers and store them in the user's mail that they manage.
4, to come to read the message to the user to provide read mail services.
The mail server forms the core of the e-mail system. Each recipient has a mailbox (mailbox) located on a mail server. Bob's mailbox is used to manage and maintain mail messages that have been sent to him. A typical journey of a mail message begins with the user agent of the sender, the mail server of the sender of the mail, to the mail server of the addressee, and then to the recipient's mailbox. When Bob wants to view mail messages in his or her mailbox, the mail server that holds the mailbox will authenticate him with the username and password he provided. Alice's mail server also has to deal with the failure of Bob's mail server. If Alice's mail server is unable to deliver mail messages immediately to Bob's mail server, Alice's server stores them in Message Queuing and tries delivery later. This attempt is usually performed every 30 minutes or so: If a few days have passed, the server removes the message from the message queue and notifies the sender (Alice) with another message.

Email
E-mail is a kind of modern communication way that transmits and receives information from each other through network. It is-a means of communication that provides information exchange electronically, Internet is the most widely used service, through the network of e-mail systems, users can at very low prices, in a very fast way, with any corner of the world to contact the network users, these emails can be text, images, Sounds, and all the other ways. At the same time, users can get a lot of free news, special mail, and achieve easy information search.
E-mail consists of two parts of envelopes and content. Envelopes are also called headers, and the e-mail server delivers messages based on the confidence on the envelopes. The content is called the Mail body, and it is used to provide the specific content of the message.
e-mail addresses on the Internet are globally unique in the format of "Mailbox name @ Mail Server domain name." The domain (mail domain) is the basic administrative unit of the e-mail server, and the mail service is based on a domain and each mailbox corresponds to one user. Where the mail server domain name must be a registered DNS domain name and must match the MX (mail switch) record. DNS is used to resolve domain names and host names to IP addresses. The MX record points to the mail server host record for the domain name and is dedicated to the mail service.

mail client Software
The mail client software is responsible for communicating with the mail server, primarily to help users send mail to the SMTP server and the Pop3/imap mail server to read the user's e-mail. mail client software usually sets compose, send, receive mail in one.

e-Mail
Each email server can start a number of e-mail, e-mail is also called e-mail address. It is similar to a real-life mailing address where users receive e-mail from others and send e-mails to others.
e-mail access needs to be applied to the email server, which is actually an account that the user applies to on the mail server. The mail server keeps incoming messages in the mailbox space assigned to an account, and the user logs on to the mail server with the user name and password they have requested to view the e-mail messages that the address has received.

the transmission process of e-mail
The e-mail system uses client/server mode. The following 3 important modules are required for e-mail delivery:
UA (mail user agent): Users interact with e-mail servers through it. MUA is actually the mail client software.
MTA (mail Transfer agent, message transfer agent): It is primarily responsible for handling all incoming and outgoing messages, providing mail delivery services for MUA or MTA, and receiving messages sent by other MTAs.
MDA (mail Delivery agent, Message delivery Agent): It is responsible for mail local delivery. When the MTA determines that a message is sent to a local user, the MTA sends the message to the MDA program for distribution, which means it is posted to the user.
The specific delivery process is as follows:
1. Senders use MUA to send mail to MTA.
2, the MTA received the mail to determine whether the recipient is a local account, if it is a local account, to the MDA delivery to the account of the mailbox, complete the sending process, jump to the 5th step. If not, follow the next step.
3. The MTA determines how to forward messages based on their mail relay forwarding settings.
4, the final purpose of the MTA will be given to his MDA processing, has the MDA to deliver the message to the recipient's mailbox.
5. The recipient uses MUA to connect to the server where the mailbox is located through the POP/IMAP protocol, requests to see if there is a message in his inbox, and if there is a message, it will send a recipient's MUA.
Note: The Message Access Service is the POP or IMAP server software, not the MTA where the message was originally received, and the role of the two is separate.

Message Transfer Protocol
e-mail Service transmission is mainly used in the following 3 network protocols
SMTP (Simple Mail Transfer Protocol)
SMTP is simple Mail Transfer Protocol. The standard TCP port is 25. MUA sends mail to MTA,MTA to send the message to the next MTA, both to use SMTP. The goal of SMTP is to deliver mail reliably and efficiently, independent of the transport subsystem and requiring only one channel that guarantees the order in which the data cells are delivered.
SMTP is a "one-way" protocol that does not send users to collect mail from other mail servers. It is itself a client/server model, and the SMTP process that is responsible for sending the mail is the SMTP client, which is the SMTP server that is responsible for receiving the mail. A complete SMTP communication process consists of three procedures for establishing a connection, sending a message, releasing a connection.
Establish a connection: first sent by the sender to the message cache, the SMTP client periodically scans the message cache, once the message is found, establish a TCP connection with the SMTP server, and then send the Hrllo command to enclose the sender's host name.
Routing Mail: The SMTP client uses the Mail command to start the delivery of the message, which provides the sender's address, then executes the RCPT command, provides the recipient address, and finally executes the data command to deliver the message content.
Release connection: After the message is delivered, the SMTP client sends out the Out command to request the shutdown of the TCP connection.
POP (Post Office Protocol)
POPs are post Office Protocol. The standard TCP port is 110. Mainly used for receiving e-mail. Mua a user's inbox connected to the MTA via the POP protocol to read or download messages from users in the Inbox.
The current use of more pop protocols is POP3. POP3 uses TCP as a transport protocol.
IMAP (Internet Information Access Protocol)
IMAP is an Internet message Access Protocol. The standard TCP port is 143, which also allows MUA to receive mail from the MTA. The target Ball IMAP protocol version is IMAP4.
Both POP and IMAP can be used to collect mail, both in the client/server mode, and the main difference is that they retrieve the message in a different way. When you use POPs, messages reside on the server, and once you receive messages, messages are downloaded from the server to the user's computer. IMAP, however, enables the user to understand that the message is stored on the server, and that the downloaded message is still stranded on the server for the purpose of archiving and sharing messages.

JavaMail
Introduction of JavaMail
JavaMail, as the name suggests, provides a programming interface for developers to work with e-mail. It's a sun-released API for handling emails. It makes it easy to perform some common message transfers. The JavaMail API is a set of standard development packages that Sun provides to enable Java developers to deliver and receive mail in applications, and it supports common mail protocols such as SMTP, POP3, and IMAP. When developers use the JavaMail API to write mail processing software, they do not need to consider the implementation details of the underlying message protocol, just call the corresponding API class in the JavaMail development package.

Second, JavaMail development environment
You must import the appropriate jar package before you make javamail development. The jar pack can be downloaded to Oracle's official web site: http://www.oracle.com/technetwork/java/javamail/index.html.
After the download is complete, extract can be obtained as follows:

Among them Mail.jar: is the development JavaMail program must use the core development package.
LIB subdirectory: This directory contains 5 separate mail development packages: Smtp.jar, Pop3.jar, Imap.jar, and Mailapi.jar, which are the category wrappers for Mail.jar files. The Mailapi.jar encapsulates the API classes that create message content and call mail delivery and reception for ordinary developers, while the other three jar files encapsulate service implementations whose names correspond to the protocol. When compiling a Java mail program, you only need to mailapi.jar the file, but you must have the underlying service implementation of the corresponding mail protocol at run time. If you only need to use the mail delivery feature in your application, You can install only the two files Smtp.jar and Mailapi.jar, and you can install only the two jar files Pop3.jar or Imap.jar and Mailapi.jar without installing the entire Mail.jar file if your application only needs to use the receive function of the message.
The Lib directory is as follows:

III. Introduction to JavaMail API
the JavaMail API is usually divided into three main categories according to its functional partitioning:
1. Create and parse APIs for mail content. The message class is the core API for creating and parsing messages. Its instance object represents an e-mail message.
2, send the Mail API. The transport class is the core API class that sends messages, and its instance object represents a mail delivery object that implements a mail delivery protocol, such as an SMTP protocol.
3, receive the Mail API. The store class is the core API class that receives mail, and its instance object represents a message recipient object that implements a message receipt protocol, such as the POP3 protocol.
The work flow of JavaMail is as follows:

In this workflow, messages, sessions, transport, store four objects are closely connected to the sending and receiving of an e-mail message. We just need to understand the functions and roles of the four core APIs, know their working relationships and email workflow, and it's easy to write JavaMail. For JavaMail auxiliary classes, we can go back and see the documents to solve when we need them. Introduction to the
        
Core APIs:
message
message A class is the core API for creating and parsing messages, and its instance object represents an e-mail message. When a client sends a message, it first wears a mail API, encapsulates the data that is created in the Message object, and then passes the object to the mail delivery API to send it. When a client receives a message, the message receipt API puts the received oil price data in the instance object of the message, and the client resolves the incoming message data from this object using the Mail resolution API. The
        
Session
session class defines the environment information required for the entire application. And the collection of client and mail server to establish a network connection of the session information, such as the host name of the mail server, port number, the use of mail delivery and reception protocols. The session object builds the store and transport objects that are sent and received from the messages, and provides information support for creating a Message object for the client.

The

Transport
Transport class is the core API class that sends messages, its instance object, which represents a message-sending object that implements a mail delivery protocol, such as an SMTP protocol. When a client program creates a good message object, it only needs to use the mail-sending API to get the transport object and then pass the message object to the transport object and invoke its sending method to send it to the specified SMTP server. The
Store
Store class is the core API class that receives mail, and its instance object represents a message recipient object that implements a message receipt protocol, such as the POP3 and IMAP protocols. When a client receives a message, it simply uses the mail-receiving API to get the store object, and then calls the store object's receive method to get the message data from the specified POP3 server and encapsulate the message data into the Message object that represents it.
 
Four, instance

public class Javamail_01 {public 
  static void Main (string[] args) throws Exception { 
    Properties props = new Proper Ties (); 
    Props.setproperty ("Mail.smtp.auth", "true"); 
    Props.setproperty ("Mail.transport.protocol", "SMTP"); 
     
    Session session = Session.getinstance (props); 
    Session.setdebug (true); 
     
    msg = new MimeMessage (session); 
    Msg.setsubject ("This is a test program ..."); 
    Msg.settext ("Hello! This is my first JavaMail program---chen"); 
    Msg.setfrom (New InternetAddress ("******@163.com")); 
 
    Transport transport = Session.gettransport (); 
    Transport.connect ("smtp.163.com", "*******", "********"); 
    Transport.sendmessage (msg,new address[]{new internetaddress ("chenssy995812509@sina.cn")}); 
 
    System.out.println ("Mail sent successfully ..."); 
    Transport.close (); 
  } 
 

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.