JavaMail How to ensure successful mail delivery

Source: Internet
Author: User
Tags how to send mail mail exchange mx record

The person who sent the message using the JavaMail API might wonder: How do I know if the message I sent to the API was successful? The general Open API gives us a call with a return value or status code to tell us whether the execution was successful or not. But JavaMail does not provide such a return value.

So when we call JavaMail to send a message, we can only tell if the message was sent successfully by means of catch exception. We think that as long as no exception occurs, the message can be sent successfully. So let's analyze why JavaMail does not provide a return value, and by exception, determine whether the message sent successfully status is reliable.

JavaMail How to send mail

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

Properties props =NewProperties (); //IP and port of the server sending the messageProps.put ("Mail.smtp.host", Mail_smtp_host); Props.put ("Mail.smtp.port", Mail_smtp_port); //whether authentication is requiredProps.put ("Mail.smtp.auth", "true"); Props.put ("Mail.smtp.socketFactory.class", "Javax.net.ssl.SSLSocketFactory"); Session Session= Session.getdefaultinstance (Props,NewAuthenticator () {protectedpasswordauthentication getpasswordauthentication () {//user name and password of the login mail sending server                return Newpasswordauthentication (Mail_sender_mail, Mail_sender_pass); }        });

Analyze the code. Sessionbefore we create it, we create one Properties , which Properties sets up a few parameters: mail.smtp.host , mail.smtp.port ,, mail.smtp.auth and mail.smtp.socketFactory.class . The user name and password used to send the message are also passed in when the session is created.

Send the message code as follows:

//5 Steps to send a message using JavaMail//1. Create SessionSession session =session.getinstance (prop); //turn on the session debug mode so you can see the status of the program sending emailSession.setdebug (true); //2. Get transport object by sessionTransport ts =Session.gettransport (); //3, the user name and password to use the mailbox to connect to the mail server, when sending a message, the sender needs to submit the mailbox user name and password to the SMTP server, the user name and password are verified before you can send the message to the recipient normally. Ts.connect ("smtp.sohu.com", "GaCl", "Email password"); //4. Create a messageMessage message =Createsimplemail (session); //5. Send mailts.sendmessage (Message, message.getallrecipients ()); //Close ConnectionTs.close ();

There are several main steps to summarize the sending process:

1. Create a Session object that contains the network link for the mail server
2. Create a Message object that represents the content of the messages
3. Create a Transport object
4. Linked Servers
5. Send a message
6. Close the link

Since transport is just an abstract class, the method Message used to invoke the ts.sendMessage Transport implementation class is actually called when it is sent SMTPTransport . sendMessage
SMTPTransportthe sendMessage method relies on the SMTP protocol for sending messages.

So, when JavaMail uses the SMTP service to send mail, when you send mail to the SMTP server, you can only get the status of the queue that has been sent to SMTP, but whether the mail server can be sent successfully, you can't get it. That is, you cannot guarantee that e-mail will succeed. This depends on the content transfer of the SMTP protocol.

However, if the SMTP protocol fails, an error will be sent. SMTP is a reliable data transfer service provided by TCP that 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 a message, if the program does not have an error, the message is sent successfully.

SMTP working mechanism

SMTP typically has two modes of operation: sending SMTP and Receiving SMTP.
The specific mode of work is: Send SMTP after receiving the user's mail request, determine whether this message is a local mail, if directly delivered to the user's mailbox, or to DNS query the Remote Mail server's MX record, and establish with the remote receive SMTP between a two-way transmission channel, The SMTP command is then sent by SMTP, received by the receiving SMTP, and the reply is reversed. Once the routing channel is established, the SMTP sender sends the Mail command to indicate the sender of the message. Returns an OK answer if the SMTP recipient can receive mail. The SMTP sender then issues the RCPT command to confirm that the message was received. If the SMTP receiver receives, an OK response is returned, and if it cannot be received, a reject receive reply is issued (but the entire message operation is not aborted), and the parties are repeated so many times. A special sequence is received when the recipient receives all the messages, and if the recipient successfully processes the message, an OK response is returned.

SMTP work process

Simple Mail Transfer Protocol (SMTP) is a text-based e-mail Transfer 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, with command-line units and newline characters cr/lf. The response information is usually only one line, starting with a 3-digit code, followed by a brief textual description.
SMTP has 3 stages of establishing connections, sending messages, 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 own identity, and then the client sends the Mail command.
(3) The server side 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) End of negotiation, send mail, send input with command data.
(7) End this send, quit with quit command.

The SMTP server routes e-mail messages based on mail exchange (MX) records in DNS. The e-mail system locates mail servers based on the address suffix of the recipient. SMTP completes the editing, receiving, and reading of messages through the user agent (UA) and delivers messages to the destination through the Mail Transfer agent (MTA).

JavaMail How to ensure successful mail delivery

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.