How to send mail using JavaMail to ensure success

preface

I believe everyone should know that the general open API calls us will have a return value or status code to tell us whether the execution is successful or not. However, JavaMail does not provide such a return value.

Therefore, when calling JavaMail to send e-mail, we can only judge whether the e-mail is sent successfully by means of catch exception. We believe that as long as there are no exceptions, the email can be sent successfully. Then let's analyze why JavaMail does not provide a return value, and whether it is reliable to judge the mail sending success status through exceptions.

How JavaMail sends mail

When using JavaMail to send mail, we must provide a mail session. The process of creating a mail session is as follows:

Analyze the code.

Before creating a session, we first create a property that sets the following parameters: mail smtp. host、mail. smtp. port、mail. smtp. Auth and mail smtp. socketFactory. class。 When creating a session, you should also pass in the user name and password used to send mail.

The sending mail code is as follows:

To summarize the process of sending e-mail, there are the following steps:

1. Create a session object containing the network link of the mail server

2. Create a message object representing the message content

3. Create a transport object

4. Linked server

5. Send message

6. Close the link

Since transport is only an abstract class, the method ts.sendmessage called when sending message here is actually the SendMessage method of the implementation class smtptransport of the called transport.

The SendMessage method of smtptransport relies on SMTP protocol to send mail.

Therefore, when JavaMail uses the SMTP service to send mail, when you send mail to the SMTP server, you can only get the status in the queue that has been sent to SMTP, but you can't get whether the mail server can send successfully. In other words, you can't guarantee that the email will be sent successfully. This depends on the content transmission of SMTP protocol.

However, if the SMTP protocol fails to transmit, an error will be reported. SMTP is a reliable data transmission service provided by TCP to transfer mail messages from the sender's mail server to the recipient's mail server.

So we can think that when we call JavaMail to send mail, if the program does not report an error, it means that the mail is sent successfully.

SMTP working mechanism

SMTP usually has two working modes: send SMTP and receive SMTP.

The specific working method is as follows: send SMTP. After receiving the user's mail request, judge whether the mail is a local mail. If it is delivered directly to the user's mailbox, otherwise, query the MX record of the remote mail server from DNS and establish a two-way transmission channel with the remote receiving SMTP. After that, the SMTP command is sent by the sending SMTP and received by the receiving SMTP, The reply is transmitted in the opposite way. Once the delivery channel is established, the SMTP sender sends a mail command to indicate the mail sender. If the SMTP recipient can receive mail, an OK response is returned. The SMTP sender then sends the RCPT command to confirm whether the mail has been received. If it is received by the SMTP receiver, an OK response is returned; If it cannot be received, a rejection response will be sent (but the whole mail operation will not be stopped), and both parties will repeat it many times. When the receiver receives all the mail, it will receive a special sequence. If the receiver successfully processes the mail, it will return an OK response.

SMTP workflow

Simple Mail Transfer Protocol (SMTP) is a text-based E-mail transfer protocol, which is used to exchange mail between mail servers in the Internet. SMTP is an application layer service, which can be adapted to various network systems.

SMTP commands and responses are text-based, in command behavior units, and line breaks are Cr / LF. The response message is generally only one line, starting with a 3-digit code, followed by a very short text description.

SMTP goes through three stages: establishing connection, sending mail and releasing connection. Specifically:

(1) Establish a TCP connection.

(2) The client sends the helo command to the server to identify the sender's own identity, and then the client sends the mail command.

(3) The server responds with OK, indicating that it is ready to receive.

(4) The client sends the RCPT command.

(5) The server side indicates whether it is willing to receive mail for the recipient.

(6) After negotiation, send an email and send the input content with the command data.

(7) End the sending and exit with quit command.

The SMTP server routes e-mail based on the mail exchange (MX) records in DNS. When the e-mail system sends e-mail, it locates the mail server according to the address suffix of the recipient. SMTP completes the functions of editing, receiving and reading e-mail through the user agent (UA), and transmits e-mail to the destination through the mail transfer agent (MTA).

summary

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. If you have any questions, you can leave a message.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>