这是一个简单的邮件发送(只含文本)
通过前一篇博文的实验,我想读者应该大致了解邮件的收发过程,在每次输入一条指令后,都会回显1~n条信息给你。
如果你要写一个发邮件的小程序,你得读取它,要不然会堵塞。
package com.lxw.javamail;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class Demo3 {
/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException, IOException {
Socket socket = new Socket("smtp.qq.com",25);
OutputStream out = socket.getOutputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println(br.readLine());
out.write("ehlo flx\r\n".getBytes());
System.out.println(br.readLine());
out.write("auth login\r\n".getBytes());
for(int i=0;i<7;i++){
System.out.println(br.readLine());
}
out.write("MzI4MjQwNzg0QHFxLmNvbQ==\r\n".getBytes());
System.out.println(br.readLine());
out.write("MzcwNTM1NDQxYg==\r\n".getBytes());
System.out.println(br.readLine());
out.write("mail from:<328240784@qq.com>\r\n".getBytes());
System.out.println(br.readLine());
out.write("rcpt to:<370535441@qq.com>\r\n".getBytes());
System.out.println(br.readLine());
out.write("data\r\n".getBytes());
System.out.println(br.readLine());
//随意填写,可以假冒别人
out.write("from:328240784@qq.com\r\n".getBytes());
//随意填写,但别人收到后会显示不是发给他的,但他却收到了
out.write("to:370535441@qq.com\r\n".getBytes());
//内容正文
out.write("subject:test\r\n".getBytes());
out.write("\r\n".getBytes());
out.write("xxxxxxxxxxxxxxxxxxx\r\n".getBytes());
//.作为结束
out.write(".\r\n".getBytes());
System.out.println(br.readLine());
out.write("quit\r\n".getBytes());
System.out.println("hhih");
}
}
本人测试了几个,可以,但超过五次之后就报错了。。。腾讯不给我发,以为是垃圾邮件。另外相关jar包在这个地址可以下载,已经打包了。
ps:原来不是检测到我乱发邮件,是随便填写那个地址必须是发邮件的那个地址。。。看来腾讯也做了相关检查
http://download.youkuaiyun.com/detail/a328240784/3820318