Home > Articles > Programming > Java

Like this article? We recommend

Like this article? We recommend

Sending an Email Message

Internet-based email messages are typically sent to recipients by way of the SMTP network protocol. According to SMTP, when an email program needs to send an email message, that program establishes a two-way transmission channel (by way of sockets) with a primary SMTP server program that is always running on some accessible host, such as your Internet Service Provider's (ISP's) host. The primary SMTP server program might be the email message's ultimate destination, or the primary SMTP server program might be an intermediary to another SMTP server program (on another host) that serves as the ultimate destination. Regardless, once the email and primary SMTP server programs have established a two-way transmission channel, the email program issues a sequence of ASCII-based commands to the primary SMTP server program, and the primary SMTP server program replies to each command with an ASCII-based response message indicating success or failure.

Assuming success, those commands result in an email message being sent from the email program to the primary SMTP server program, which might forward that email message to another SMTP server program (if the email message's address so indicates). If the email message is destined for a mailbox on the primary SMTP server program's host, the primary SMTP server program stores that email message in its associated email database. However, if the email message is destined for a mailbox on another host, the other host's SMTP server program stores that email message in its associated email database. Figure 2 provides an illustration of this SMTP model.

Figure 2 The SMTP model for sending email messages. Sockets appear as green boxes.

SMTP recognizes a variety of commands that email programs can use to communicate with SMTP server programs. Some commands take arguments and other commands take no arguments, but each command must end with a carriage-return character followed by a newline character. Six commonly used commands are HELO, MAIL, RCPT, DATA, RSET, and QUIT.

The previous ordering of the six commonly used SMTP commands is not an accident. Apart from RSET, the commands must be entered in their specified order because an SMTP server program is a state-based program. For each email program that establishes a two-way transmission channel, the SMTP server program maintains state on the current email message transaction with that email program (to keep track of its place in that transaction).

When an email program successfully connects its socket to an SMTP server program's server socket, the SMTP server program sends an initial message to the email program. That message consists of a three-digit reply code and a text message identifying the SMTP server program. The reply code, which is located at the beginning of every SMTP server program response message, identifies success or failure and is used by email programs to automate their email message transactions with SMTP server programs. By contrast, the text message provides meaningful information to a user. An email program must check the reply code to determine its next course of action, but it may ignore the text message if it is not deemed to be relevant (such as the initial message).

After receiving the initial message, an email program begins its email message transaction by sending the HELO command. HELO identifies an email program to an SMTP server program and takes an argument that identifies the SMTP server program's host's domain name. In response, the SMTP server program performs whatever initialization is necessary to set itself in an initial state for taking part in an email message transaction with the email program, and it typically sends a success response message back to the email program. The success response message begins with reply code 250 and continues with a greeting message.

Following HELO, an email program sends the MAIL command. MAIL identifies the sender to the SMTP server program and takes the argument FROM: and one email message address. If the SMTP server program has no trouble parsing the email message address, it typically returns a response message beginning with reply code 250. Otherwise, the response message begins with another code that identifies the reason for the failure.

Following MAIL, an email program sends the RCPT command. RCPT identifies a single recipient to the SMTP server program. That command takes the argument TO: and one email message address. If the SMTP server program has no trouble parsing the address, it typically returns a response message beginning with reply code 250. Otherwise, the response message begins with a different reply code that identifies the reason for the failure. You can specify multiple RCPT commands to address multiple recipients.

At this point, the email program needs to send the email message. It accomplishes that task by sending the DATA command (which takes no arguments), receiving a positive response message, sending each email message line, and sending a line that consists of a single-period character after the last email message line. After that, the email program waits for a response message to find out whether the email message was successfully received by the SMTP server program. After successfully sending the email message, the email program can choose to abort that message and the email transaction by sending the no-argument RSET command.

Finally, when the time comes to disconnect its transmission channel with the SMTP server program, the email program sends the no-argument QUIT command.

NOTE

Although they are shown in uppercase, SMTP (and POP3) commands may be specified in lowercase or uppercase/lowercase. That is not possible with email message addresses.

You might be curious about the format of reply codes (and examples of codes other than 250). The leftmost digit identifies a generic level of success or failure; 1 refers to a positive preliminary reply, 2 refers to a positive completion reply, 3 refers to a positive intermediate reply, 4 refers to a transient negative completion reply (the email program can reissue the command during the current email transaction), and 5 refers to a permanent negative completion reply (the email program cannot reissue the command during the current email transaction). The middle digit identifies response categories; 0 refers to syntax errors, 1 refers to information requests, 2 refers to the transmission channel, 3 and 4 are not specified, and 5 refers to the mail system. The third digit offers more detailed information within the category.

Given that information, 250 can be interpreted as "the requested mail command has successfully completed," 220 (which is typically part of the initial message) can be interpreted as "the SMTP server program is awaiting a HELO command," and 503 can be interpreted as "a bad sequence of commands." Consult RFC 2821 to get more information on reply codes.

To help you understand SMTP-based email transactions, I wrote a command line–based program called SMTPDemo. When run, that program attempts to connect to my ISP's SMTP server program on standard port number 25. Although you should not need to change 25 (because that number is standard for an SMTP server program port), you will need to change mail.gatewest.net to the domain name of your ISP's host (so that the program will work for you) before compiling SMTPDemo's source code. Listing 1 presents that source code.

Listing 1: SMTPDemo.java

// SMTPDemo.java

import java.io.*;
import java.net.*;

class SMTPDemo
{
  public static void main (String [] args)
  {
   String SMTPServer = "mail.gatewest.net";
   int SMTPPort = 25;

   Socket client = null;

   try
   {
     // Attempt to create a client socket connected to the SMTP 
     // server.
     // program.

     client = new Socket (SMTPServer, SMTPPort);

     // Create a buffered reader for line-oriented reading from the
     // standard input device.

     BufferedReader stdin;
     stdin = new BufferedReader (new InputStreamReader (System.in));

     // Create a buffered reader for line-oriented reading from the
     // socket.

     InputStream is = client.getInputStream ();
     BufferedReader sockin;
     sockin = new BufferedReader (new InputStreamReader (is));

     // Create a print writer for line-oriented writing to the 
     // socket.

     OutputStream os = client.getOutputStream ();
     PrintWriter sockout;
     sockout = new PrintWriter (os, true); // true for auto-flush

     // Display SMTP greeting from SMTP server program.

     System.out.println ("S:" + sockin.readLine ());

     while (true)
     {
      // Display a client prompt.

      System.out.print ("C:");

      // Read a command string from the standard input device.

      String cmd = stdin.readLine ();

      // Write the command string to the SMTP server program.

      sockout.println (cmd);

      // Read a reply string from the SMTP server program.

      String reply = sockin.readLine ();

      // Display the first line of this reply string.

      System.out.println ("S:" + reply);

      // If the DATA command was entered and it succeeded, keep
      // writing all lines until a line is detected that begins
      // with a . character. These lines constitute an email
      // message.

      if (cmd.toLowerCase ().startsWith ("data") &&
        reply.substring (0, 3).equals ("354"))
      {
        do
        {
          cmd = stdin.readLine ();

          if (cmd != null && cmd.length () > 1 &&
            cmd.charAt (0) == '.')
            cmd = "."; // Must be no chars after . char.

          sockout.println (cmd);

          if (cmd.equals ("."))
            break;
        }
        while (true);

        // Read a reply string from the SMTP server program.

        reply = sockin.readLine ();

        // Display the first line of this reply string.

        System.out.println ("S:" + reply);

        continue;
      }

      // If the QUIT command was entered, quit.

      if (cmd.toLowerCase ().startsWith ("quit"))
        break;
     }
   }
   catch (IOException e)
   {
     System.out.println (e.toString ());
   }
   finally
   {
     try
     {
      // Attempt to close the client socket.

      if (client != null)
        client.close ();
     }
     catch (IOException e)
     {
     }
   }
  }
}

After you replace mail.gatewest.net with your ISP's hostname and compile SMTPDemo's source code, type java SMTPDemo. Carry out the following exercise, and you will see similar output:

The following exercise's output is from a sample session between myself (C:) and my ISP's SMTP server program (S:).

S:220 kynes.gatewest.net ESMTP Debian SendMail 8.12.1/8.12.0.
Beta19/Debian
8.12.0.Beta19; Sat, 2 Mar 2002 16:38:10 -0600

This initial message was sent from my ISP's Linux-based SendMail SMTP server program to SMTPDemo.

C:helo gatewest.net
S:250 kynes.gatewest.net Hello h139-142-224-80.gtcust.grouptelecom.net 
[139.142.224.80], pleased to meet you

I began the email message transaction by typing helo gatewest.net, where gatewest.net identifies my ISP's domain name. SendMail sent a greeting message, beginning with reply code 250, as its response.

C:mail from: <jeff@javajeff.com>
S:250 2.1.0 <jeff@javajeff.com>... Sender ok

I identified myself as the sender of the email message and was rewarded with a success response. (Angle brackets are optional.)

C:rcpt to: <jeff@javajeff.com>
S:250 2.1.5 <jeff@javajeff.com>... Recipient ok

I also identified myself as the email message's recipient so that I would not bother anyone else during my test. Once again, I received a success response.

C:data
S:354 Enter mail, end with "." on a line by itself
Subject: Test Email
This is my email message.
.
S:250 2.0.0 g22McAxW027265 Message accepted for delivery

I next specified the actual message by issuing the DATA command. After receiving a success response, I specified the message's subject and a single-line message. Note the period character on a line by itself. That character indicates the end of the message and is not part of the message. Also note the response. That response includes a message ID value of g22McAxW027265. SendMail includes that message ID value as part of the stored/forwarded email message, to uniquely identify email messages.

C:quit
S:221 2.0.0 kynes.gatewest.net closing connection

After sending the email message, there was nothing else to do, so I terminated my connection with the SendMail SMTP server program. Note the 221 reply code. The leftmost 2 indicates a positive completion, the middle 2 indicates the transmission channel (or connection), and the rightmost 1 indicates that the connection is closed.

Look carefully at SMTPDemo's output, and you'll find the Subject: header appearing as the first DATA line. That header identifies the subject of the email message, and its value typically appears in the subject portion of a receiving email program's GUI-based message list. If the From:, Sender:, Reply-To:, To:, and Cc: headers were also specified, they would appear as part of the data sent to the SMTP server program via the DATA command. Furthermore, as Subject: shows, each header would appear ahead of any nonheader content. The following paragraphs describe how a receiving email program uses the From:, Sender:, Reply-To:, To:, and Cc: headers' mailbox values.

The receiving email program displays From:'s mailbox values in the from portion of its message list. Furthermore, that program places those values (if no Reply-To: header was passed) in a new To: header when that program's user replies to the email message. If a From: header is not specified, an SMTP server program will probably generate a default From: header, using the email address from the MAIL command, before storing or forwarding that email message.

The receiving email program will probably ignore the Sender: header because replies are not (typically) sent to the sender. To ensure consistency between the email address specified as part of the MAIL command and the Sender: header, a sending email program could retrieve Sender:'s email address from a properties file and pass that value as an argument to MAIL.

The receiving email program places Reply-To:'s mailbox values in a new To: header when the user chooses to reply to an email message.

The receiving email program places To:'s mailbox values (along with Reply-To:'s mailbox values, if Reply-To: is present) in a new To: header when the user selects a reply-all function in the email program. To ensure consistency between the email addresses specified as part of RCPT commands and the To: header, a sending email program could take To:'s email addresses and pass each address as an argument to a RCPT command. If To: is not passed, the receiving email program does not disclose the RCPT addresses.

Finally, the receiving email program places Cc:'s mailbox values in a new Cc: header when the user selects a reply-all function in the email program.

Earlier, I discussed the attachment concept. You might wonder how to use SMTPDemo to send an attachment. To find out, run SMTPDemo and enter the following text (but use your own email address.) Use your favorite email program to retrieve the email message.

helo gatewest.net
mail from: jeff@javajeff.com
rcpt to: jeff@javajeff.com
data
Subject: Attachment Demo
Content-Type: multipart/mixed; boundary="***"

--***
Content-Type: text/plain; charset="iso-8859-1"

This message has an attachment.

--***

Content-Type: text/plain; name="file.txt"

Attachment text.

--***--
quit

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020