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 emails, we can only use the catch exception to determine whether the email is sent successfully. We believe that as long as no exception occurs, the email will be sent successfully. Then let’s analyze why JavaMail does not provide a return value, and whether the successful email sending status is reliable through exceptions.
JavaMail sending mail principle
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(); // The IP and port of the server that sends the mail 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() { // Log in to the user name and password of the email sending server return new PasswordAuthentication(MAIL_SENDER_MAIL, MAIL_SENDER_PASS); } });Analyze the code.
Before creating the Session, we first create a Properties. This Properties sets the following parameters: mail.smtp.host , mail.smtp.port , mail.smtp.auth and mail.smtp.socketFactory.class . When creating a Session, you must also pass in the username and password to send the email.
The sending email code is as follows:
//5 steps to send emails using JavaMail//1. Create session Session session session = Session.getInstance(prop); //Enable Session debug mode, so that you can view the running status of the email sent by the program session.setDebug(true); //2. Get the transport object through session Transport ts = session.getTransport(); //3. Use the user name and password of the email to connect to the mail server. When sending email, the sender needs to submit the user name and password of the email to the smtp server. The user name and password can only be verified before the email can be sent to the recipient normally. ts.connect("smtp.sohu.com", "gacl", "email password"); //4. Create a message Message message = createSimpleMail(session); //5. Send mail ts.sendMessage(message, message.getAllRecipients()); //Close the connection ts.close();Summary of the process of sending emails has the following steps:
1. Create a Session object containing a network link to the mail server
2. Create a Message object representing the content of the email
3. Create a Transport object
4. Link to the server
5. Send Message
6. Close the link
Since Transport is just an abstract class, the method ts.sendMessage is actually the sendMessage method of the SMTPTransport implementation class SMTPTransport .
The sendMessage method of SMTPTransport relies on the SMTP protocol to send emails.
Therefore, when Javamail uses the smtp service to send emails, when you send the 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 whether the mail server can send it successfully. That is to say, you cannot guarantee that the email will be sent successfully. This depends on the content transmission of the SMTP protocol.
However, if the SMTP protocol fails, an error will be reported. SMTP's reliable data transmission service provided by TCP transmits 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 emails, if the program does not report an error, it means that the email is sent successfully.
SMTP working mechanism
SMTP usually has two working modes: sending SMTP and receiving SMTP.
The specific working method is: after receiving the user's email request, it determines whether the email is local email. If it is sent directly to the user's email, otherwise, query the MX record of the remote mail server from the dns, and establish a two-way transmission channel with the remote receiving SMTP. After that, the SMTP command is sent by the sending SMTP, received by the receiving SMTP, and the response is transmitted in the opposite direction. Once the transmission channel is established, the SMTP sender sends a MAIL command to indicate the sender of the mail. If the SMTP recipient can receive the mail, the OK answer will be returned. The SMTP sender then issues an RCPT command to confirm whether the email has been received. If the SMTP recipient receives it, it returns an OK answer; if it cannot be received, it sends a rejection response (but does not abort the entire email operation), and both parties will repeat this many times. When the recipient receives all the emails, a special sequence will be received. If the recipient successfully processes the emails, it will return an OK reply.
SMTP working process
Simple Mail Transfer Protocol (SMTP) is a text-based email transmission protocol that is used on the Internet to exchange mail between mail servers. SMTP is an application-level service that can be adapted to various network systems.
The commands and responses of SMTP are all based on text, with command behavior units, and the newline is CR/LF. The response information generally has only one line, starting with a 3-digit code, and a very short text description can be attached later.
SMTP needs to go through three stages: establishing a connection, delivering mail and releasing a connection. Specifically:
(1) Establish a TCP connection.
(2) The client sends a HELO command to the server to identify the sender's own identity, and the client then sends a MAIL command.
(3) The server responds with OK, indicating that it is ready to receive.
(4) The client sends RCPT command.
(5) The server side indicates whether it is willing to receive emails for the recipient.
(6) End of negotiation, send email, and use the command DATA to send the input content.
(7) End this sending and exit with the QUIT command.
The SMTP server routes emails based on Mail Exchange (MX) in DNS. When sending emails, the email server is located according to the address suffix of the recipient. SMTP completes the functions of editing, collecting and reading emails through the User Agent (UA); and transmits the emails to the destination through the Mail Transfer Agent (MTA).
Summarize
The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate.