使用Socket发送邮件

简介: 使用Socket发送邮件
package com.futao.learn.imooc.book.s2;
import sun.misc.BASE64Encoder;
import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
/**
 * @author
 * @date 2021/9/16
 */
public class SmtpDemo {
    private static final String S = "\r\n";
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket();
        socket.setTcpNoDelay(true);
        socket.connect(new InetSocketAddress("smtp.exmail.qq.com", 25), 5000);
        PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        System.out.println("=:" + reader.readLine());
        writer.println("HELO " + InetAddress.getLocalHost().getHostName());
        System.out.println("===:" + reader.readLine());
        // writer.println("STARTTLS");
        // System.out.println("===:" + reader.readLine());
        String userName = new BASE64Encoder().encodeBuffer("<futao@mysteel.com>".getBytes(StandardCharsets.UTF_8));
        writer.println("AUTH LOGIN");
        String authCode = reader.readLine();
        System.out.println("===:" + authCode);
        writer.println(userName + "\r\n" + authCode.split(" ")[1]);
        System.out.println("===:" + reader.readLine());
        writer.println("MAIL FROM: <futao@mysteel.com>");
        System.out.println("===:" + reader.readLine());
        writer.println("RCPT TO: <futao@mysteel.com>");
        System.out.println("===:" + reader.readLine());
        writer.println("DATA");
        System.out.println("===:" + reader.readLine());
        writer.println("这是通过Socket编程发出的邮件");
        System.out.println("===:" + reader.readLine());
        writer.println(".");
        System.out.println("===:" + reader.readLine());
        writer.println("QUIT");
        System.out.println("===:" + reader.readLine());
    }
}
相关文章
|
4月前
|
监控 数据安全/隐私保护
Smtp邮件发送失败情况汇总
Smtp邮件发送失败情况汇总
209 0
|
网络协议
socket聊天室--socket的建立
socket聊天室--socket的建立
95 0
|
移动开发 C# 数据安全/隐私保护