Java network programming: realize QQ mail sending client

table of Contents

1. Goal introduction

1. Know SMTP (Mail Transfer Protocol)

 2. POP3 (mail receiving protocol)

2. Base64-encoded mailbox and authorization code

1. Open QQ mailbox SMTP/POP3 service

2. Write BASE64 encoding program in Java

Three, telnet command to send mail

Four, Java graphical interface for SMTP interactive mailing

1. Write the client class

2. Create a graphical interface for sending QQ mail 

V. Summary and next preview


1. Goal introduction

This blog post records learning to use Java to implement a client similar to QQ to send mail. It is planned to be divided into two parts. The first part is to learn from scratch, to understand the computer network mail transfer protocol (SMTP, POP3), and to open the smtp/ of the QQ mailbox. The pop3 service is to prepare for the subsequent program design, implement a simple Java GUI by itself, and send emails through commands; the next part is based on the previous foundation, and realizes basic and complete functions, similar to our usual QQ email sending client, with a simple graphical interface In addition, the sending operation is added, and the function of receiving server feedback information is added. In the future, you can use the mail sending program you wrote to send mail to other people, which is faster and more convenient. ๑乛◡乛๑

1. Know SMTP ( Mail Transfer Protocol )

SMTP (Simple Mail Transfer Protocol, RFC821) is a reliable and effective email transfer protocol . SMTP is an e-mail service based on FTP file transfer service. It is mainly used to transfer e-mail information between systems and provide notifications about incoming letters .

 2. POP3 (mail receiving protocol)

POP3, the full name is Post Office Protocol-Version 3, that is, Post Office Protocol version 3 . It is a member of the TCP/IP protocol suite, defined by RFC1939. This protocol is mainly used to support the use of the client to remotely manage emails on the server.

2. Base64-encoded mailbox and authorization code

The port of the server sending service mail is 25, and the port of the server receiving service mail is 110.

Email settings enable smtp/pop3 service. At present, when opening the service for most emails, you need to set the authorization code used by the third-party client. The authorization code is used instead of the password to prevent the password from leaking. The operation is as follows:

1. Open QQ mailbox SMTP/POP3 service

Log in to the mailbox and click Settings----Account.

Scroll down, find the POP3/SMTP service, click Open, click to generate authorization code after completing the verification, remember to copy and save, and use it later

2. Write BASE64 encoding program in Java

To successfully send and receive emails, the user name and password (email address and authorization code) need to be BASE64 encoded before they can be effectively transmitted.

/*
 * BASE64.java
 * Copyright (c) 2020-12-09
 * author : Charzous
 * All right reserved.
 */

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.IOException;

public class BASE64 {
    public static void main(String[] args) throws IOException {
        String userName="你的邮箱";
        String authCode="生成的授权码";
        //显示邮箱名的base64编码结果
        System.out.println(encode(userName));
        //显示授权码的base64编码结果
        System.out.println(encode(authCode));

    }

    public static String encode(String str){
        return new BASE64Encoder().encode(str.getBytes());
    }
}

Finally, a similar string is generated:

Y3poX2NoYXJ6ZXVzQHFxLmNvbQ==

Three, telnet command to send mail

1. Open the command prompt and enter

telnet smtp.qq.com 25

 2. Input in order, for example as follows:

HELO hostname   //回车,hostname可以是IP或其他随意别名
AUTH LOGIN //回车后,先粘贴base64程序编码的完整邮箱名并回车;再粘贴base64编码的授权码并回车


MAIL FROM:<[email protected]>   //在这里填写自己的邮箱地址,用于发送邮件(注意冒号后面别有空格)
RCPT TO:<[email protected]>  //接收方的邮箱,在这里暂时填写和上面一样的邮箱地址,即自己发送邮件给自己,验证是否成功
DATA       //回车,接下来开始发送邮件头相关内容
Subject: the simple mail     // 邮件的标题,回车  
//在这里再多发送一行空行,来分隔邮件内容,下面就是邮件正文内容
Hello,the mail content!
测试发送邮件!   //控制台中中文会变成?的乱码,但不影响接受者接收到中文内容
.   //在邮件正文发送完毕后,单独用一行输入一个小圆点,作为结束标志,然后回车
QUIT   //结束通信(含4次握手断开)

 Successfully sent results:

Four, Java graphical interface for SMTP interactive mailing

When using the terminal to send, there are many inconveniences, such as input errors, you cannot go back and modify, and you can only enter by pressing Enter. Therefore, a simple Java interface is written to avoid the trouble caused by these misoperations. Use your own program instead of telnet to verify the process of smtp sending to mail.

1. Write the client class

/*
 * TCPMailClient.java
 * Copyright (c) 2020-12-09
 * author : Charzous
 * All right reserved.
 */

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

public class TCPMailClient {
    private Socket socket;

    private PrintWriter pw;
    private BufferedReader br;

    /**
     * @param ip
     * @param port
     * @return
     * @author Charzous
     * @date 2020/12/9 11:19
     *
     */
    public TCPMailClient(String ip, String port) throws IOException{
        //主动向服务器发起连接,实现TCP三次握手
        //不成功则抛出错误,由调用者处理错误
        socket =new Socket(ip,Integer.parseInt(port));

        //得到网络流输出字节流地址,并封装成网络输出字符流
        OutputStream socketOut=socket.getOutputStream();
        //参数true表示自动flush数据
        pw=new PrintWriter(new OutputStreamWriter(socketOut,"utf-8"),true);

        //得到网络输入字节流地址,并封装成网络输入字符流
        InputStream socketIn=socket.getInputStream();
        br=new BufferedReader(new InputStreamReader(socketIn,"utf-8"));

    }

    public void send(String msg){
        //输出字符流,由socket调用系统底层函数,经网卡发送字节流
        pw.println(msg);
        try {
            //进行邮件交互,发送smtp指令之间应该暂停一段时间
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public String receive(){
        String msg=null;
        try {
            //从网络输入字符流中读取信息,每次只能接受一行信息
            //不够一行时(无行结束符),该语句阻塞
            //直到条件满足,程序往下运行
            msg=br.readLine();
        }catch (IOException e){
            e.printStackTrace();
        }
        return msg;
    }

    public void close(){
        try {
            if (socket!=null)
                socket.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

2. Create a graphical interface for sending QQ mail 

 It is easy to find here that similar to the previous TCP communication, only need to connect to the sending server smtp.qq.com of QQ mail.

The creation of the interface uses JavaFx, you can refer to my article to implement it yourself, which is relatively simple.

Reference article:

1. " Network socket programming based on TCP protocol (java realizes C/S communication) "

2. " Java advanced: TCP-based network real-time chat room (socket communication case) "

V. Summary and next preview

This article records in detail the technical ideas of using Java network programming, and initially realizes the QQ mail sending client. Learn from scratch, understand the computer network mail transfer protocol (SMTP, POP3), and open the smtp/pop3 service of QQ mailbox, prepare for the subsequent program design, implement a simple Java GUI by yourself, and send mail through commands.

It can be found that it is still not convenient to send requests through commands to send information to the server , so the following article will implement non-command mode to send emails, better user experience, avoid cumbersome command input, wait for the next article The content is updated!

If you think it’s good, welcome to "one-click, three-link", like, bookmark, follow, comment directly if you have any questions, and exchange and learn!

Java implementation of socket communication network programming series of articles:

  1. Network Socket programming based on UDP protocol (C/S communication case implemented by java) [ https://blog.csdn.net/Charzous/article/details/109016215 ]
  2. Network socket programming based on TCP protocol (java realizes C/S communication) [ https://blog.csdn.net/Charzous/article/details/109016215 ]
  3. Java multithreading to realize TCP network Socket programming (C/S communication) [ https://blog.csdn.net/Charzous/article/details/109283697 ]
  4. Java multithreading realizes multi-user and server-side Socket communication [ https://blog.csdn.net/Charzous/article/details/109440277 ]
  5. Advanced Java: TCP-based network real-time chat room (socket communication case) [ https://blog.csdn.net/Charzous/article/details/109540279 ]

My CSDN blog: https://blog.csdn.net/Charzous/article/details/110913974

Guess you like

Origin blog.csdn.net/Charzous/article/details/110913974