Use JavaMail to send messages to ensure success

  • 2020-05-17 05:37:40
  • OfStack

preface

As you probably know, the open api of 1 has a return value or a status code that tells us whether or not the execution was successful. However, JavaMail does not provide such a return value.

Therefore, when we call JavaMail to send the mail, we can only judge whether the mail is sent successfully by catch exception. We think that as long as there are no exceptions, then the email will be sent successfully. Then we will analyze 1 why JavaMail does not provide the return value, and judge whether the successful status of the email is reliable through the exception.

JavaMail sends messages

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


Properties props = new Properties();
  //  Of the server that sent the mail IP And port  
  props.put("mail.smtp.host", MAIL_SMTP_HOST);
  props.put("mail.smtp.port", MAIL_SMTP_PORT);
  //  Whether authentication is required 
  props.put("mail.smtp.auth", "true");

  props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  Session session = Session.getDefaultInstance(props, new Authenticator() {
   protected PasswordAuthentication getPasswordAuthentication() {
   //  Login the username and password of the mail sending server  
    return new PasswordAuthentication(MAIL_SENDER_MAIL, MAIL_SENDER_PASS);
   }
  });

Analyze 1 for the code.

Before creating Session, we first create an Properties, which sets the following parameters: mail.smtp.host , mail.smtp.port , mail.smtp.auth and mail.smtp.socketFactory.class . When you create Session, you also pass in the username and password you used to send the email.

The sending code is as follows:


// use JavaMail emailing 5 A step 
  //1 , create, session
   Session session = Session.getInstance(prop);
   // open Session the debug Mode so that you can view the program send Email Operating state 
   session.setDebug(true);
   //2 And through session get transport object 
   Transport ts = session.getTransport();
   //3 , use the mailbox user name and password to connect to the mail server. When sending mail, the sender needs to submit the mailbox user name and password to smtp The server, username and password must be verified before the email can be sent to the recipient. 
   ts.connect("smtp.sohu.com", "gacl", " Your password ");
   //4 , create mail 
   Message message = createSimpleMail(session);
   //5 , send email 
   ts.sendMessage(message, message.getAllRecipients());
   // Close the connection 
   ts.close();

There are mainly the following steps to summarize the process of sending emails:

1. Create an Session object that contains a network link to the mail server

2. Create an Message object that represents the content of the message

3. Create Transport objects

4. Link to a server

5. Send Message

6. Close the link

Since Transport is just an abstract class, the method is called when Message is sent ts.sendMessage Is actually the implementation class of the Transport call SMTPTransport the sendMessage Methods.

while SMTPTransport the sendMessage The method relies on the SMTP protocol to send the message.

So, when javamail sends an email using smtp, when you send an email to the smtp server, you can only get the status that has been sent to the smtp queue, but you can't get it if the email server sends it successfully. That is, you can't guarantee a successful email. This depends on the content transfer of the SMTP protocol.

But the SMTP protocol will report an error if the transmission fails. SMTP a reliable data transfer service provided by TCP delivers mail messages from the sender's mail server to the recipient's mail server.

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

SMTP working mechanism

SMTP usually has two modes of operation: send SMTP and receive SMTP.

Specific work is: send SMTP upon receiving the user's mail request, whether this email as the local mail, if direct delivery to the user's mailbox, or contact the dns MX records of the remote mail server, and the establishment and the remote receiving SMTP between a two-way transmission channel, then SMTP command issued by sending SMTP, received by receiving SMTP, while answering the transfer. Once a transport channel is established, the SMTP sender sends the MAIL command indicating the sender of the message. If the SMTP receiver can receive the message, an OK reply is returned. The SMTP sender then issues the RCPT command to confirm that the message was received. If the SMTP receiver receives, OK reply is returned. If it is not received, a reject response is issued (but the entire mail operation is not aborted), which is repeated many times by both parties. When the recipient receives the entire message, a special sequence is received, and if the recipient successfully processes the message, an OK reply is returned.

SMTP working process

The simple mail transfer protocol (SMTP) is a text-based E-mail transfer protocol used in the Internet to exchange messages between mail servers. SMTP is an application layer service that can be adapted to various network systems.

SMTP's commands and responses are text-based, in command behavior units, with the newline character CR/LF. Response message 1 is usually just one line, starting with a three-digit code, followed by a very short text description.

SMTP goes through three stages: establishing the connection, sending the message, and releasing the connection. Specific as follows:

(1) establish TCP connection.

(2) the client sends the HELO command to the server to identify the sender, and the client sends the MAIL command.

(3) the server side takes OK as the response, indicating that it is ready to receive.

(4) the client sends RCPT command.

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

(6) after the negotiation, send the email, and use the command DATA to send the input content.

(7) end the transmission and exit with the QUIT command.

The SMTP server logs the routing of e-mails based on the mail exchange (MX) in DNS. The E-mail system addresses the mail server based on the address suffix of the recipient. SMTP can edit, receive and read emails through the user agent program (UA). Mail is sent to its destination through the mail transfer agent (MTA).

conclusion

The above is the whole content of this article, I hope the content of this article to your study or work can bring 1 definite help, if you have questions you can leave a message to communicate.


Related articles: