fork me on github

通过163smtp服务器向各大邮箱发送邮件(SOCKET编程)

package server;

import java.io.*;
import java.net.*;
import java.sql.Time;
import java.util.Scanner;
import java.util.Timer;

public class client {
    
    /*通过163smtp服务器向其他邮箱发送数据
     * 返回码
     * 0:未知原因出错
     * 1:发送成功
     * 2:被判定为垃圾邮件
    */
    public static int send(Mail mail){
        int state=0;
        Socket socket=null;
        String userName="xxxxxxxxx";//经过base64编码的163邮箱用户名
        String password="xxxxxxxxxxxxxx";//经过base64编码的163邮箱密码
        BufferedReader reader=null;
        PrintWriter writer=null;
        try {
            // 连接到服务器
            socket = new Socket("SMTP.163.com", 25);
            reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            writer = new PrintWriter(socket.getOutputStream(), true);
            System.out.println(reader.readLine());
            writer.println("HELO 163");
            System.out.println(reader.readLine());
            writer.println("auth login");   
            System.out.println(reader.readLine());   
            writer.println(userName);   
            System.out.println(reader.readLine());   
            writer.println(password);   
            System.out.println(reader.readLine()); 
            writer.println("mail from:<mailsys163@163.com>");
            System.out.println(reader.readLine());   
            writer.println("rcpt to:<" +mail.getReceiver()+">");   
            System.out.println(reader.readLine()); 
            //开始发送邮件
            writer.println("data");
            System.out.println(reader.readLine());
            writer.println("subject:"+mail.getSubject());
            writer.println("from:"+mail.getSender());
            writer.println("to:"+mail.getReceiver());
            writer.println("Content-Type: text/plain;charset=\"gb2312\"");   
            writer.println("");   
            writer.println(mail.getContent());   
            writer.println(".");   
            writer.println("");   
            String sum=reader.readLine();
            System.out.println(sum);
            if(sum.indexOf("250")==0){
                System.out.println("发送成功");
                state=1;
            }else if(sum.indexOf("554")==0){
                System.out.println("被判定为垃圾邮件");
                state=2;
            }
            writer.println("rset");   
            System.out.println(reader.readLine());   
            writer.println("quit");   
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close();
                reader.close();
                writer.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return state;
    }
    
    public static void main(String[] args){
        Mail mail=new Mail("xxxxxxx@163.com","xxxxx@qq.com","你好","吃饭了没");
        System.out.println(send(mail));
        
    }
}

注意如果是163向163发送邮件,邮件的发送者(mail.sender,不是真实发送者这个必须和认证的用户名相同)可以设置为任意的,这样接收者收到邮件时会看到该邮件由认证的用户代发

但是如果是163向其他邮箱发送邮件,这两个from必须相同,否则大部分情况下都会被163当作是垃圾邮件返回554错误,无发发送

 

posted @ 2017-05-02 19:38  烦嚣的人  阅读(2343)  评论(0编辑  收藏  举报