通过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)); } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyszxs.html