SMTP protocol using java language based on handwritten mail client

SMTP protocol using java language based on handwritten mail client

1. Description

E-mail is a common application on the Internet, he is the product of the early Internet, until today is still subject to the user's favorite (probably because of different cultural backgrounds, e-mail only when used in an office in China).

E-mail system consists of the following components:

  • User Agent
  • Mail Server
  • Mail Transfer Protocol

As we know, the current market popular e-mail has qq-mail, 163 mailboxes and so on, we can go to apply for a qq mailbox or mailbox 163, the reason is because Tencent and NetEase mail server.

We also know that we not only can, but also can log qq mailbox by other clients through the official qq-mail clients send and receive messages, such NetEase mailbox and other masters. This shows that the mail service provider mail server, but the user agent is not confined to the vendor, the user agent is communicating through mail transfer protocol with the mail server, which means that as long as we understand the message transport protocols to understand a network programming language, you can begin to realize our own mail client up.

Then we began to realize it;

2. SMTP protocol

SMTP stands for Simple Mail Transfer Protocol, Simple Mail Transfer Protocol. As the name suggests, this agreement is very simple, by reading the protocol RFC documents, we can grasp the basic elements of the agreement, understand the communication rules from the user agent and the mail server.

3 Preparations

  1. SMTP protocol read RFC documents
  2. Maven build environment
  3. Write code

4. SMTP protocol Essentials

RFC documents are in English, of course, would have been very frightened, but he found the contents fact very small, so watching and checking dictionary document read it twice (do not worry), the following are the main contents.

SMTP protocol into the standard SMTP protocol extensions and SMTP protocols, standard SMTP protocol is defined in the 1982 RFC821 document, and extended in 1995, the SMTP protocol is defined in RFC1869 document. Extended SMTP protocol changes in the standard SMTP protocol on the basis of very little, mainly to increase the security of e-mail authentication, and now we are basically talking about SMTP protocol SMTP protocol extensions.

  • introduction introduction:

    It introduces the Extended SMTP protocol provides a stable foundation for efficient message transfer agent (that is to say the SMTP protocol can be used to implement the message transfer agent client, also fast mail client). Then explain the expansion content.

  • SMTP extension of the framework agreement

    SMTP transmission is the message object, mail object including the cover and contents

    • The cover comprises a sender's address, recipient's address and a plurality of delivery model, using a series of unit transmission protocol.
    • Includes a head portion and two topics, sent using the SMTP protocol data unit header includes a series of key-value pairs, the head always using ASCII encoding
  • SMTP protocol contains much instruction, but only need to use the following instructions to complete the simple mailing

    • ehlo <domain>

      As ehlo zeng

      Establishing a first command to be sent after the connection with the SMTP protocol.

    • auth para

      Set the authentication mode, such asauth login

    • mail from: <发送者邮箱>

      Setting sender mail, such asmail from:<[email protected]>

    • rcpt to:<收件者邮箱>

      Set recipient mailbox, such asrcpt to:<[email protected]>

    • data

      Said it would be sent contents of the message, the command is sent back all message content

    • quit

      End of message sent

All commands are line breaks at the end of

5. Source

import com.sun.xml.internal.messaging.saaj.util.Base64;
import lombok.Data;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * @author zeng
 */
public class MyEmailClient {
    public static void main(String[] args) throws IOException {
        //敏感信息。。
        Token token=new Token("",25,"","");
        Socket socket=null;
        PrintWriter printWriter=null;
        BufferedReader br=null;
        
        try {
            //1. 连接smtp邮箱服务器
            socket=new Socket(token.getAddress(),token.getPort());
            printWriter=new PrintWriter(socket.getOutputStream(),true);
            br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
            //2. 第一条命令 ehlo
            printWriter.println("ehlo zeng");
            System.out.println(br.readLine());
            //3. 发送,auth
            printWriter.println("auth login");
            System.out.println(br.readLine());
            //4. 用户名和密码
            printWriter.println(token.getUserName());
            printWriter.println(token.getPassWord());
            //会有一大串信息返回,如果最后返回235 Authentication successful则成功
            String temp=null;
            while ((temp=br.readLine())!=null){
                System.out.println(temp);
                if ("235 Authentication successful".equals(temp)){
                    break;
                }
            }
            System.out.println("认证成功");

            //设置发件人和收件人,敏感信息
            String sentUser="";
            String recUser="";
            printWriter.println("mail from:<"+sentUser+">");
            System.out.println(br.readLine());
            printWriter.println("rcpt to:<"+recUser+">");
            System.out.println(br.readLine());

            //设置data
            printWriter.println("data");
            System.out.println(br.readLine());

            //设置邮件主题
            printWriter.println("subject:test");
            printWriter.println("from:"+sentUser);
            printWriter.println("to:"+recUser);
            //设置邮件格式
            printWriter.println("Content-Type: text/plain;charset=\"utf8\"");
            printWriter.println();
            //邮件正文
            printWriter.println("来自java手写smtp邮件客户端");
            printWriter.println(".");
            printWriter.print("");
            System.out.println(br.readLine());

            //退出
            printWriter.println("rset");
            System.out.println(br.readLine());
            printWriter.println("quit");
            System.out.println(br.readLine());

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //释放连接
            socket.close();
            printWriter.close();
            br.close();
        }
    }
}
@Data
class Token{
    String address;
    Integer port;
    String userName;
    String passWord;

   Token(String address, Integer port, String userName, String passWord) {
        this.address = address;
        this.port = port;
        this.userName = new String(Base64.encode(userName.getBytes()));
        this.passWord = new String(Base64.encode(passWord.getBytes()));
    }
}

6. digression

The reason for writing this is his reason to read the meter network "computer network top-down approach" This book, which application layer protocol homework problems have a mail client to achieve after the exam, then see this title, it felt weird, because before the classroom when the meter network are some theoretical knowledge, but really never thought to write the code yourself. Spring before when writing code is also used mail-related classes, so he also decided to implement its own mail client by reading RFC documents, to help me find more fun.

Guess you like

Origin www.cnblogs.com/zeng-xian-hui/p/11221519.html