Objective
I believe you should all know that the general open API to our call will have a return value or status code, to tell us the implementation of success or not. However, JavaMail does not provide such a return value.
So when calling JavaMail to send a message, we can only determine whether the message was sent successfully by a catch exception. We believe that as long as there is no exception, the message can be sent successfully. So let's analyze why JavaMail did not provide the return value, and whether it was a reliable way to determine the success of a message sent through an exception.
JavaMail Send mail principle
When sending a message using JavaMail, we must provide a mail session. The process of creating a mail session is as follows:
Properties Props = new properties ();
IP and Port props.put of the server sending the message
("Mail.smtp.host", mail_smtp_host);
Props.put ("Mail.smtp.port", mail_smtp_port);
Whether authentication
props.put ("Mail.smtp.auth", "true") is required;
Props.put ("Mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
Session session = Session.getdefaultinstance (props, New Authenticator () {
protected passwordauthentication Getpasswordauthentication () {
//login mail send server username and password return
to new Passwordauthentication (mail_sender_mail, mail _sender_pass);
}
);
Analyze the code.
Before we create a session, we create a properties, which sets a few parameters: mail.smtp.host
, mail.smtp.port
,, mail.smtp.auth
and mail.smtp.socketFactory.class
. When you create the session, you also want to pass in the username and password used to send the message.
Send the message code as follows:
5 Steps to send a message using JavaMail
/1, create session sessions Session
= Session.getinstance (prop);
Turn on debug mode for session so that you can view the running status of the program sending email
session.setdebug (true);
2, through session to get transport object
Transport ts = Session.gettransport ();
3, use the mailbox username and password to connect to the mail server, send a message, the sender needs to submit the user name and password of the mailbox to the SMTP server, the user name and password are authenticated before they can normally send the message to the recipient.
ts.connect ("smtp.sohu.com", "GaCl", "Mailbox Password");
4, create mail message messages
= Createsimplemail (session);
5, Send mail
ts.sendmessage (message, message.getallrecipients ());
Close connection
ts.close ();
There are several main steps to summarizing the process of sending a message:
1. Create a Session object that contains a network link to the mail server
2. Create a Message object that represents the content of your messages
3. Create a Transport object
4. Linked server
5. Send Message
6. Close the link
Since transport is just an abstract class, the method used to invoke the message here ts.sendMessage
is actually the method of calling the transport implementation class SMTPTransport
sendMessage
.
SMTPTransport
the sendMessage
method relies on the SMTP protocol to send mail.
So when JavaMail uses the SMTP service to send mail, when you send a message to an SMTP server, you can only get the status of a queue that has been sent to SMTP, but the mail server can send it successfully, and you won't get it. That is to say, you cannot guarantee that a message will be sent successfully. This depends on the content transfer of the SMTP protocol.
However, if the SMTP protocol fails the transmission, it will be an error. SMTP is a reliable data transfer service provided by TCP that sends 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 a message, if the program does not have an error, it means the message was sent successfully.
SMTP working mechanism
SMTP typically has two modes of operation: sending SMTP and Receiving SMTP.
The way to work is: Send SMTP after receiving the user's mail request, determine whether this mail is local mail, if directly delivered to the user's mailbox, otherwise to DNS query the Remote Mail server MX record, and establish and remote receive SMTP between a two-way transmission channel, The SMTP command is then sent by send SMTP, received by receiving SMTP, and the reply is transmitted in reverse. Once the delivery channel is established, the SMTP sender sends the Mail command to indicate the sender of the message. If the SMTP recipient can receive the message, the OK answer is returned. The SMTP sender then issues the RCPT command to confirm that the message was received. If the SMTP recipient receives it, the OK answer is returned, and if it is not received, a reject receive answer is issued (but the entire message operation is not aborted), and the two sides will repeat so many times. When the recipient receives all the messages will receive a special sequence, if the recipient successfully processed the message, then return OK answer.
SMTP work process
Simple Mail Transfer Protocol (SMTP) is a text-based e-mail transport protocol that is used in the Internet to exchange messages between mail servers. SMTP is an application-tier service that can be adapted to a variety of network systems.
SMTP commands and responses are text-based, in order of behavior units, newline characters are cr/lf. Response information is usually only one line, starting with a 3-digit code, followed by a short text description.
SMTP has 3 stages of connecting, sending mail, and releasing connections. The specific:
(1) Establish a TCP connection.
(2) The client sends the HELO command to the server to identify the sender's identity, and the client sends the Mail command.
(3) The server side takes OK as a response, 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) Negotiate the end, send the mail, use the command data to send the input content.
(7) End this send, with quit command exit.
The SMTP server routes e-mail messages based on mail exchange (MX) records in DNS. The e-mail system locates the mail server based on the recipient's address suffix when it sends a message. SMTP completes the editing, collection, and reading of messages through the user agent (UA), and delivers messages to destinations through the message transfer agent (MTA).
Summarize
The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.