美文网首页
使用Socket发送邮件

使用Socket发送邮件

作者: 垃圾简书_吃枣药丸 | 来源:发表于2021-09-16 17:12 被阅读0次
    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());
    
        }
    }
    
    

    相关文章

      网友评论

          本文标题:使用Socket发送邮件

          本文链接:https://www.haomeiwen.com/subject/kttdgltx.html