使用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());
    }
}
相关文章
|
2月前
使用的是.NET Framework 4.0,并且需要使用SMTP协议发送电子邮件
使用的是.NET Framework 4.0,并且需要使用SMTP协议发送电子邮件
56 1
|
5月前
|
监控 数据安全/隐私保护
Smtp邮件发送失败情况汇总
Smtp邮件发送失败情况汇总
293 0
|
C#
jmail + c#实现邮件的接收
jmail + c#实现邮件的接收
191 0
|
移动开发 C# 数据安全/隐私保护

热门文章

最新文章