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()); } }