Introduction to the basic concepts of email
Email is used to convey confidence and communicate online, and it is one of the most important Internet services. According to statistics, 30% of Internet services are related to email. At the same time, we cannot deny that it plays an important role in our daily life, work and office. For example: Many office automation projects (OAs) have the function of sending emails. If you also use manual methods such as OutLook, it is not suitable. In this high-speed era, we need to provide work efficiency so that work can be automated. At the same time, many websites also need to send emails: send a welcome E-mail to newly registered users with their registration information, send the latest activity information of the website to all registered members through E-mail, etc.
Before starting email development: we need to understand several concepts: email system, mail server, email, mail client software, mail transmission protocol, and email transmission process.
Email System
Similar to traditional postal services, the email system consists of an email office, an email sending and receiving system. Senders and recipients send and receive emails through email sending and receiving systems, which are actually mail client programs running on computers. The email office plays a bridge, which is actually a mail server program running on the server. The process of email processing is similar to that of postal services.
Mail Server
A mail server is a device used to manage emails. There are a large number of email servers available on the Internet, such as: 126, 163, hotmail...
The main functions provided by the email server:
1. Receive emails sent by users.
2. Forward the email delivered by the user to the target mail server.
3. Receive emails forwarded by other email servers and store the emails in the user's emails they manage.
4. Provide users who come to read emails to read emails.
Mail servers form the core of the email system. Each recipient has a mailbox (mailbox) located on a certain mail server. Bob's mailbox is used to manage and maintain email messages that have been sent to him. A typical journey of an email message starts with the sender's user agent, the sender's email server, is transferred to the recipient's email server, and then delivered to the recipient's email address. When Bob wants to view the email messages in his mailbox, the mail server that stores the mailbox will authenticate him with the username and password he provided. Alice's mail server also has to deal with Bob's mail server failure. If Alice's mail server cannot deliver mail messages to Bob's mail server immediately, Alice's server stores them in the message queue and then attempts to deliver them later. This attempt is usually performed every 30 minutes or so: if the attempt fails to succeed after several days, the server removes the message from the message queue and notifies the sender (i.e. Alice) with another email message.
e-mail
Email is a modern communication method that transmits and receives information from each other through the network. It is a communication method that provides information exchange by electronic means and is the most widely used service on the Internet. Through the network's email system, users can contact Internet users in any corner of the world in a very low price and in a very fast way. These emails can be in various ways such as text, images, sound, etc. At the same time, users can get a large number of free news and special emails, and realize easy information search.
Email consists of two parts: envelope and content. Envelopes are also called mail headers, and the email server delivers mail based on the confidence on the envelope. Content is called a mail body, which is used to provide the specific content of the email.
The email address on the Internet is unique worldwide, and its format is "email name@mail server domain name". Domain (mail domain) is the basic management unit of the email server. The email service is domain-based, and each mailbox corresponds to one user. 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 of the domain name, and is dedicated to mail services.
Email client software
The email client software is responsible for communicating with the email server, which is mainly used to help users send emails to the SMTP server and the POP3/IMAP mail server to read the user's email. Email client software usually integrates writing, sending and receiving emails.
Email
Multiple email addresses can be started on top of each email server, also known as E-Mail addresses. It is similar to a real-life mailing address through which users accept and send emails from others.
Obtaining an email requires applying on the email server. To be precise, the email is actually an account that the user applies on the email server. The mail server keeps the received mail in the mailbox space allocated for an account, and the user logs in to the mail server through the username and password he requests to view the email messages that have been received by the address.
Email transmission process
The email system is in client/server mode. Email delivery requires the following 3 important modules:
UA (Mail User Agent, Mail User Agent): The user deals with the email server through it. MUA is actually the mail client software.
MTA (Mail Transfer Agent, Mail Transfer Agent): It is mainly responsible for processing all received and sent mails, providing mail delivery services to MUA or MTA, and receiving mails sent by other MTAs.
MDA (Mail Delivery Agent, mail delivery agent): It is responsible for local delivery of mail. When the MTA decides to send a certain email to the local user, the MTA handes the email to the MDA program for distribution, that is, it is delivered to the user.
The specific transmission process is as follows:
1. The sender uses MUA to send the email to the MTA.
2. After receiving the email, the MTA determines whether the recipient is a local account. If it is a local account, it will be handed over to the MDA to the email address of the account, complete the sending process and skip to step 5. If not, perform the next step.
3. MTA decides how to forward emails based on its email relay forwarding settings.
4. The MTA of the final purpose will hand over the MDA to his processing, and the MDA will deliver the email to the recipient's email address.
5. The recipient uses MUA to connect to the server where the email address is located through the POP/IMAP protocol, and requests to check whether there are emails in his inbox. If there is email, the recipient's MUA will be transmitted through it.
Note: The email access service is prompted to be POP or IMAP server software, not the MTA that received the email. The roles of the two are separated.
Mail Transfer Protocol
Email service transmission mainly uses the following three network protocols
SMTP (Simple Mail Transfer Protocol)
SMTP is Simple Mail Transfer Protocol. The standard TCP port is 25. MUA sends the email to the MTA, and the MTA sends the email to the next MTA, both of which need to use SMTP. The goal of SMTP is to deliver mail reliably and efficiently, it is independent of the delivery subsystem and requires only one channel that guarantees the order of data units to be transmitted.
SMTP is a "one-way" protocol that cannot be collected by users from other mail servers. It itself adopts the client/server mode. The SMTP process responsible for sending mail is the SMTP client, and the SMTP process responsible for receiving mail is the SMTP server. A complete SMTP communication process mainly includes three processes: establishing a connection, delivering mail, and releasing a connection.
Establish a connection: First, the sender sends the email to the email cache. The SMTP client scans the email cache regularly. Once there is a message, it establishes a TCP connection with the SMTP server, and then sends an HRLLO command to attach the sender's host name.
Delivery email: The SMTP client uses the MAIL command to start sending the email, which provides the sender's address; then executes the RCPT command and provides the recipient's address; finally executes the DATA command to transfer the email content.
Release the connection: After the mail is delivered, the SMTP client sends an OUT command to request to close the TCP connection.
POP (Post Office Agreement)
POP is Post Office Protocol. The standard TCP port is 110. Mainly used for receiving emails. MUA connects to MTA's user inbox via POP protocol to read or download user mail in the inbox.
The POP protocol that is currently used is POP3. POP3 uses TCP as the transmission protocol.
IMAP (Internet Information Access Protocol)
IMAP is an Internet Message Access Protocol. The standard TCP port is 143, which also allows MUA to collect mail from the MTA. The version of the target ball IMAP protocol is IMAP4.
Both POP and IMAP can be used to collect emails, both in the client/server mode. The main difference between the two is that they retrieve emails differently. When using POP, the email resides on the server. Once the email is received, the email is downloaded from the server to the user's computer. IMAP can understand the situation where mail is stored on the server, and the downloaded mail is still stuck in the server to facilitate mail archiving and sharing.
JavaMail
1. Introduction to JavaMail
JavaMail, as the name implies, provides a programming interface for developers to process emails. It is an API released by Sun to handle email. It can conveniently perform some commonly used mail transfers. JavaMail API is a standard development package provided by Sun to facilitate Java developers to implement email sending and receiving functions in applications. It supports some commonly used email protocols, such as SMTP, POP3, and IMAP. When developers use the JavaMail API to write email processing software, they do not need to consider the implementation details of the underlying mail protocol, just call the corresponding API class in the JavaMail development package.
2. JavaMail development environment
Before JavaMail development, you must import the corresponding jar package. The jar package can be downloaded from the official website of oracle: http://www.oracle.com/technetwork/java/javamail/index.html.
After the download is completed, you can get the following directory:
Among them, mail.jar: is the core development package that must be used to develop javamail programs.
lib subdirectory: This directory contains 5 independent email development packages: smtp.jar, pop3.jar, imap.jar and mailapi.jar. These 5 jar files are classified packaging for mail.jar files. Among them, mailapi.jar encapsulates the API class that creates mail content and calls mail sending and receiving to ordinary developers, while the other three jar files encapsulates the service implementation program with their names corresponding to the protocol. . When compiling Java mail programs, you only need the mailapi.jar file, but at runtime, you must have the underlying service implementation program of the corresponding mail protocol. If you only need to use the mail sending function in the application, you can only install the two files smtp.jar and mailapi.jar. If you only need to use the mail receiving function, you can only install the two jar files pop3.jar, imap.jar and mailapi.jar without installing the entire mail.jar file.
The Lib directory is as follows:
3. Introduction to JavaMail API
JavaMail API is usually divided into the following three categories according to its functions:
1. Create and parse email content API. The Message class is the core API for creating and parsing emails. Its instance object represents an email.
2. The API for sending emails. The Transport class is the core API class for sending mails. 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 mail. Its instance object represents the email receiving object that implements a certain email receiving protocol, such as the POP3 protocol.
The workflow of JavaMail is as follows:
In this workflow, the four objects, 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, know their working relationships and email workflow, and we can easily write JavaMail. For JavaMail's auxiliary classes, when we need them, we can follow the clues and check the documents to solve the problem.
Introduction to the core API:
Message
The Message class is the core API for creating and parsing mail, and its instance object represents an email. When the client sends an email, first use the email API, encapsulate the created email data into the Message object, and then pass this object to the email sending API to send it out. When the client receives the email, the email receiving API installs the received oil price data in the instance object of Message, and the client uses the email 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, as well as to collect session information for the client to establish a network connection with the mail server, such as the host name, port number, and the mail sending and receiving protocol used. The Session object builds Store and Transport objects for sending and receiving mails based on these session information, and provides information support for the client to create Message objects.
Transport
The Transport class is the core API class for sending mails. 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 email sending API to get the Transport object, then pass the Message object to the Transport object, and call its sending method to send the email to the specified SMTP server.
Store
The Store class is the core API class for receiving mail. Its instance object represents the mail receiving object that implements a certain mail receiving protocol, such as POP3 and IMAP protocols. When the client program receives mail, it only needs to use the mail receiving API to obtain the Store object, and then call the Store object's receiving method to obtain the mail data from the specified POP3 server and encapsulate the mail data into the Message object representing the mail.
IV. Examples
public class JavaMail_01 { public static void main(String[] args) throws Exception { Properties props = new Properties(); props.setProperty("mail.smtp.auth", "true"); props.setProperty("mail.transport.protocol", "smtp"); Session session = Session.getInstance(props); session.setDebug(true); Message msg = new MimeMessage(session); msg.setSubject("This is a test program..."); msg.setText("Hello! This is my first javamail program----Chen Ming"); msg.setFrom(new InternetAddress("******@163.com")); Transport transport = session.getTransport(); transport.connect("smtp.163.com", 25, "*********", "************"); transport.sendMessage(msg,new Address[]{new InternetAddress("[email protected]")}); System.out.println("Email sent successfully..."); transport.close(); } }