Network Programming Email Protocol (NPEP)

Instructional Objectives

After participating in this practicum, students are expected to be able to:

  1. Understanding the email sending process
  2. Create a program to send email.

1. Shipping Process

Email Email application is actually a form of SMTP (Simple Mail Transfer Protocol) service. SMTP is a message protocol for email message delivery services. Sending email messages can be done without having an email account on the SMTP server.

To send an email, the application to be created must contact the mail server that will forward the message to the mail server that manages the destination email account. So the first thing to know is the IP address/name of the mail server and the port it uses. After knowing these two things, it is also necessary to know the message protocol. In general, each message has a destination email address, sender email, sender email that will appear in the message, message title and message content. Sending email can be done using a simple method through the Telnet program. Telnet allows users to access the smtp/mail server via port 25 to send email or port 110 to access Pop3 mail. The commands that can be used are as follows:

telnet servername port

The email sending process can be done through the following steps:

  1. Connecting to the mail server via port 25
  2. Test server response (optional)
  3. Tells the origin of the email
  4. Informs the purpose of the email
  5. Writing data or email content
  6. Closing the connection.

The delivery process is carried out by giving the following commands:

  • Mail from:  <email_pengirim> to show who sent the email.
  • Rcpt to:  <email_tujuan> to show who the email recipient is.
  • Data to write an email message. Data can contain subject, from and to attributes as well as message content. The data content ends with a dot ( “.”).
  • Quit to close the connection with the mail server.


Figure 10.1 Example process

2. Create a Program

Java language has provided a package to access socket through java.net package. The class that will be used to access server socket is Socket class. The usage format is as follows:

Socket(“nama_server/ip_address”, port)

To send commands to the server when a connection occurs, the java.io package is used  DataOutputStream to input commands to the server and BufferedReader to get responses from the server.

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

 public class emailku {
    public static void main(String[] argc) {
        Socket s1;
        DataOutputStream p1=null;
        BufferedReader d1=null;
        String recvreply;
 
    try {
        s1=new Socket("mail.sertifikasi.web.id",25);
        p1=new DataOutputStream(s1.getOutputStream());
        d1=new BufferedReader(new InputStreamReader(s1.getInputStream()));
        recvreply=d1.readLine();
        System.out.println("Server Response : " + recvreply);
        p1.writeBytes("HELO mail.sertifikasi.web.id\r\n");
        recvreply=d1.readLine();
        System.out.println("Server Response : " + recvreply);
        p1.writeBytes("MAIL FROM:<felstly@gmail.com>\r\n");
        recvreply=d1.readLine();
        System.out.println("Server Response : " + recvreply);
        p1.writeBytes("RCPT TO:<felix@sertifikasi.web.id>\r\n");
        recvreply=d1.readLine();
        System.out.println("Server Response : " + recvreply);
        p1.writeBytes("DATA\r\n");
        recvreply=d1.readLine();
        System.out.println("Server Response : " + recvreply);
        p1.writeBytes("Subject:Perkenalan\r\n");
        p1.writeBytes("From:<felstly@gmail.com>\r\n");
        p1.writeBytes("To:<felix@sertifikasi.web.id>\r\n");
        p1.writeBytes("\r\n");
        p1.writeBytes("Hello,\r\n");
        p1.writeBytes("Saya hanya ingin memperkenalkan diri.\r\n");
        p1.writeBytes("Silahkan kunjungi website kami di:\r\n");
        p1.writeBytes("www.sertifikasi.web.id\r\n");
        p1.writeBytes("\r\n");
        p1.writeBytes("Felix\r\n");
        p1.writeBytes("============\r\n");
        p1.writeBytes(".\r\n");
        recvreply=d1.readLine();
        System.out.println("Server Response : " + recvreply);
        p1.writeBytes("QUIT\r\n");
        recvreply=d1.readLine();
        System.out.println("Server Response : " + recvreply);
        s1.close();
        System.out.println("Closed Connection with Server");
    } catch(IOException e) {
        System.out.println("Error in Connecting to Port");
    } 
    } 
}

Email Application Protocol (Electronic Mail)

Electronic-Mail (E-Mail) is the most widely used TCP/IP application. This chapter discusses the protocols that support email applications.

1. Simple Mail Transport Protocol (SMTP)

SMTP is a basic protocol that is responsible for exchanging emails (mail exchange) between TCP/IP-based hosts. There are 3 standards for this protocol, namely: 

  • The standard used for exchanging email between computers (STD 10/RFC 821), is called the SMTP standard.
  • The standard used for message format (STD 11) is described in RFC 822 which contains mail syntax and RFC 1049 which contains the use of files that are not ASCII text (email uses 7bit ASCII) so that they can be used in the email body. This standard is called MAIL
  • The standard used to route email based on the domain name system (DNS), is described in RFC 974 under the name DNS-MX.

The above standards are used for emails that use the English language format, while email usage standards that support the use of other languages ​​include: 

  • Multipurpose Internet Mail Exchange (MIME) is described in RFCs 2045 through 2049.
  • Additional services from SMTP include: service extension notifications on SMTP clients, use of 8bit data format, email size limits.

1.1. How SMTP works

SMTP works based on end-to-end delivery, where the SMTP client will contact the SMTP server to immediately send email. The SMTP server serves users through port 25.

Where each message must have:

  • The header or envelope, which is described in RFC 822.
  • Content, which contains the contents of the letter to be sent.

1.1.1. Mail header format 

Users don't need to be confused about mail headers, because everything is managed by SMTP.

The format of the mail header is 

Bagian-nama : Bagian-isi

Example of using mail headers:

To: Sukaridhoto <dhoto@eepis-its.edu>

Examples of frequently used header sections include:

Table 12.1 SMTP -- Commonly used headers

| Kata kunci  | Nilai                                            |
|-------------|--------------------------------------------------|
| to          | Tujuan dari email                                |
| cc          | Tujuan kedua dari email (carbon-copy)            |
| from        | Pengirim email                                   |
| reply-to    | Alamat pengembalian email                        |
| return-path | Alamat host untuk pengembalian email             |
| Subject     | Subjek tentang email yang diisikan oleh pengguna |

Figure 12.1 Envelope, Header, Body
Figure 12.1 Envelope, Header, Body

1.1.2. Mail Exchange 

The SMTP model can be seen in Figure 12.2. From the results of the user requesting mail. The sender's SMTP makes a 2-way connection with the recipient's SMTP. SMTP can be a final destination or a forwarder (mail gateway). The sender's SMTP will generate a command to do a reply to the recipient's SMTP.

Figure 12.2 SMTP Model
Figure 12.2 SMTP Model

1.2. SMTP mail exchange flow diagram

The email exchange that occurred was as follows: 

  1. The Sender SMTP establishes a TCP/IP connection with the recipient SMTP and waits for the server to send a 220 message indicating that service for the message is ready or a 421 message indicating that service is not ready.
  2. HELO (short for hello) is sent by the server indicating the domain name.
  3. The sender will start giving commands to SMTP, where if SMTP supports the command, it will reply with a 250 OK message.
  4. Provide information to SMTP about the destination of the email with the RCPT TO command followed by the destination email address.
  5. Once the destination is set, continue with the DATA command which indicates that the next line is the body of the email ending with <CRLF>.<CRLF>
  6. The client fills in the data according to the message to be sent until it fills in <CRLF>.<CRLF>
  7. The sender will stop the activity by giving the QUIT command.

Figure 12.3 SMTP Flow
Figure 12.3 SMTP Flow

This can be exemplified by:

Figure 12.4 Example of using SMTP
Figure 12.4 Example of using SMTP

1.3. SMTP and Domain Name System

If the network uses DNS, SMTP cannot simply send an email to TEST.IBM.COM by simply opening a TCP connection to TEST.IBM.COM. The first thing it does is query the name server and get the results for the destination.

SMTP will look for a record in DNS with the MX tag, and will send an email to the host registered on that host.

Figure 12.5 How Email Works
Figure 12.5 How Email Works

2. Multipurpose Internet Mail Extensions (MIME)

MIME is an internet standard that connects email formats to support text formats other than US-ASCII, non-text attachments, multi-part in the message body, and information in the header. All emails written by users will be sent via SMTP with MIME format. In addition to being used in email systems, MIME is also used in other protocols such as HTTP on the world wide web. MIME is described in RFC 2045, RFC 2046 and RFC 2049. The internet's basic email protocol, SMTP, only supports 7bit ASCII, so support with MIME was added to support others.

2.1. The header contained in MIME

Figure 12.6 MIME Example
Figure 12.6 MIME Example

2.1.1. MIME-Version

Version used in MIME 

MIME-Version: 1.0

2.1.2. Content-Type

Type used in messages

Content-Type: text/plain

Table 12.2 Example of Content-type

| Tipe        | Subtipe       | Deskripsi                                  |
|-------------|---------------|--------------------------------------------|
| Text        | Plain         | Unformated text                            |
|             | Enriched      | Text yang memiliki format                  |
| Image       | Gif           | Gambar dengan format GIF                   |
|             | Jpeg          | Gambar dengan format JPEG                  |
| Audio       | Basic         | Suara                                      |
| Video       | Mpeg          | Film dengan format MPEG                    |
| Application | Octet-Stream  | Sequence yang tidak terinterpreted         |
|             | Postscript    | Dokumen for postscript                     |
| Message     | RFC822        | MIME RFC 822                               |
|             | Partial       | Pesan yang dipisah                         |
|             | External-body | Pesan yang ditarik dari jaringan           |
| Multipart   | Mixed         | Independent                                |
|             | Alternative   | Pesan yang sama beda format                |
|             | Parallel      | Bagian yang harus dilihat secara bersamaan |
|             | Digest        | Tiap bagian merupakan bagian RFC 822       |

2.1.3. Content-Transfer-Encoding 

The methods used for sending emails are: 

  • 7bit
  • Quoted printable
  • Base64.

2.1.4. Encoded-Word 

Used when using other characters

2.1.5. Multipart Messages 

Message section separator 

Content-type: multipart/mixed; boundary="frontier"
MIME-version: 1.0

This is a multi-part message in MIME format.
--frontier
Content-type: text/plain

This is the body of the message.
--frontier
Content-type: text/html; encoding=UTF-8
Content-transfer-encoding: base64

PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUg
Ym9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg==
--frontier--

3. Post-Office-Protocol (POP)

Email users will use the POP protocol to retrieve emails that are on the server. The protocol used now is version 3 so it is called POP3.

POP3 evolved from previous protocols called POP (commonly referred to as POP1) and POP2.

The POP3 protocol is designed for users with networks that need to be turned off intermittently. So users can use email without having to be connected continuously. Although POP3 has the option to "leave messages on server", email users will usually connect, retrieve email and store it on the PC, delete email on the server and disconnect.

POP3 server serves users through port 110.

Figure 2.7 Example of using POP3
Figure 2.7 Example of using POP3

4. Internet Message Access Protocol version 4 (IMAP4)

IMAP4 is a protocol that can be used by users to read email on a server. IMAP4 is described in RFC 3501.

Example of using telnet on IMAP

Figure 12.8 Telnet IMAP
Figure 12.8 Telnet IMAP

5. How Email Works

Clients use MUA (Mail User Agent) to read emails via POP3 or IMAP4. And to send emails via SMTP protocol.

Figure 12.9 How EMAIL works
Figure 12.9 How EMAIL works

Mail servers or MTAs (Mail Transfer Agents) exchange emails via the SMTP protocol, and store emails in Mbox or Maildir format. 

Mbox is a type of email storage where emails are stored in 1 file for each user. 

Figure 12.10 Maildir
Figure 12.10 Maildir

Maildir is a type of email storage where emails are stored in 1 folder for each user.

Figure 12.11 Mbox
Figure 12.11 Mbox


Post a Comment

Previous Next

نموذج الاتصال