java编写简单的E-mail发送端程序
本文实例介绍了简单e-mail发送端程序的java实现代码,分享给大家供大家参考,具体内容如下
在这个代码中,有几个注意点强调一下:
1、使用 socket 与 smtp 邮件服务器取得连接,注意 smtp 服务器的主机名;
2、使用 data 命令时,若写了 subject (主题)之后,邮件的正文部分必须与 subject 之间有一个空行,即“回车+换行”,在代码中则是 \r\n ;
3、同样需要将发件人的邮箱用户名、密码进行 base64 编码之后再传给 smtp 服务器;
4、程序在编译时仍然存在警告,这是由于 sun.misc.base64encoder 类是存在于 rt.jar 包中的,由于 jdk 会更新升级,可能会导致该包中的某些类发生变化而不可用,所以编译器会发出警告。
此外,写了这些代码,也发现了一些问题:
1、smtp.qq.com 和 smtp.sina.com 邮件服务器不知道为什么不能用,也就是说,当发件人的邮箱地址是 qq 或者 sina 时,这个程序不管用了,状态应答码也看不懂。在我的测试当中,只有 smtp.163.com 可以用,明明都是在官网查到这几个 smtp 服务器的,怎么不能用呢?真奇怪。有哪位朋友知道的希望能告诉我,谢谢!
2、在下面的 simplemailsender 类中的 sendemail() 方法中,有些重复代码让人感到疑惑,但是没办法,我暂时还弄不懂…
3、重大发现:qq 邮箱接收邮件的速度比 163 邮箱、sina 邮箱要快上可能有数十倍,真让我惊讶。此外,使用 nslookup 命令查询 smtp.qq.com 的主机名时,发现它有好多台 smtp 服务器,至少比 163 的 3 台多出 5 台,腾讯真够强大;
4、虽然说写了这个程序可以恶意地不停给某个邮箱发邮件,但是我发现,当我用一个 sina 邮箱连续发送了几十封邮件给固定的另一个邮箱之后,该 sina 邮箱再想发邮件就会被拒绝,小心哟。
代码如下:
// 邮件 class e_mail { string from; string to; string subject; string content; string username; string pwd; public e_mail(string from, string to, string subject, string content, string username, string pwd) { this.from = from; this.to = to; this.subject = subject; this.content = content; this.username = this.tobase64(username); this.pwd = this.tobase64(pwd); } /** * 在 e_mail 类中进行用户名、密码的转码工作 */ private string tobase64(string str) { return (new sun.misc.base64encoder().encode(str.getbytes())); } } // 简单的邮件发送端类,实现发送功能 public class simplemailsender { private string smtpserver; private int port = 25; private socket socket; bufferedreader br; printwriter pw; /** * 根据发件人的邮箱地址确定smtp邮件服务器 */ private void initserver(string from) { if(from.contains("@163")) { this.smtpserver = "smtp.163.com"; }else if(from.contains("@126")) { this.smtpserver = "smtp.126.com"; }else if(from.contains("@sina")) { this.smtpserver = "smtp.sina.com"; }else if(from.contains("@qq")) { this.smtpserver = "smtp.qq.com"; } } public void sendemail(e_mail email) { try { this.initserver(email.from); this.socket = new socket(smtpserver, port); this.br = this.getreader(socket); this.pw = this.getwriter(socket); // 开始组装发送邮件的命令序列 send_receive(null); // 接收连接smtp服务器成功的信息 send_receive("ehlo hao"); send_receive("auth login"); send_receive(email.username); send_receive(email.pwd); send_receive("mail from:<" + email.from + ">"); send_receive("rcpt to:<" + email.to + ">"); send_receive("data"); // 邮件内容 pw.println("from:" + email.from); pw.println("to:" + email.to); // 主题与正文之间一定要空一行,即加上"\r\n" pw.println("subject:" + email.subject + "\r\n"); // 在控制台打印邮件内容 system.out.println("from:" + email.from); system.out.println("to:" + email.to); system.out.println("subject:" + email.subject + "\r\n"); system.out.println(email.content); // 邮件正文 pw.println(email.content); // 一定记得正文以"."结束 send_receive("."); send_receive("quit"); } catch (ioexception e) { e.printstacktrace(); } finally { try { if (socket != null) socket.close(); } catch (ioexception e) { e.printstacktrace(); } } } /** * 每发送一条命令,必须在命令后面加上"\r\n", * 则同时打印出smtp邮件服务器的相应状态码 * @param command */ private void send_receive(string command) throws ioexception{ if(command != null) { // 向smtp邮件服务器发送命令,一定要记得加上"\r\n" pw.print(command + "\r\n"); pw.flush(); system.out.println("用户 >> " + command); } char [] response = new char[1024]; br.read(response); system.out.println(response); } /** * 获取 socket 的输出流 */ private printwriter getwriter(socket socket) throws ioexception { outputstream socketout = socket.getoutputstream(); return new printwriter(socketout, true); } /** * 获取 socket 的输入流 */ private bufferedreader getreader(socket socket) throws ioexception { inputstream socketin = socket.getinputstream(); return new bufferedreader(new inputstreamreader(socketin)); } // 测试 public static void main(string[] args) { new mailsendergui(); } } // 邮件发送程序界面 class mailsendergui extends jframe implements actionlistener { private jlabel usernamelabel; private jtextfield usernamefield; private jlabel pwdlabel; private jpasswordfield pwdfield; private jlabel fromlabel; private jtextfield fromfield; private jlabel tolabel; private jtextfield tofield; private jlabel subjectlabel; private jtextfield subjectfield; private jlabel contentlabel; private jtextarea contentarea; private jbutton sendbtn; private jbutton cancelbtn; private e_mail email; private simplemailsender mailsender; public mailsendergui() { this.init(); this.mailsender = new simplemailsender(); } private void init() { this.fromlabel = new jlabel("发件人邮箱地址:"); this.fromfield = new jtextfield(25); this.usernamelabel = new jlabel("用户名:"); this.usernamefield = new jtextfield(25); this.pwdlabel = new jlabel("密码:"); this.pwdfield = new jpasswordfield(25); this.tolabel = new jlabel("收件人邮箱地址:"); this.tofield = new jtextfield(25); this.subjectlabel = new jlabel("邮件主题:"); this.subjectfield = new jtextfield(20); this.contentlabel = new jlabel("邮件正文:"); this.contentarea = new jtextarea(15, 20); this.settitle("蚂蚁-->简单邮件发送器"); this.setbounds(200, 30, 500, 500); this.setlayout(new borderlayout()); this.setdefaultcloseoperation(jframe.exit_on_close); this.setvisible(true); this.sendbtn = new jbutton("发送"); this.cancelbtn = new jbutton("重置"); this.sendbtn.addactionlistener(this); this.cancelbtn.addactionlistener(this); jpanel uppanel = new jpanel(new gridlayout(6, 2, 5, 5)); uppanel.add(fromlabel); uppanel.add(fromfield); uppanel.add(usernamelabel); uppanel.add(usernamefield); uppanel.add(pwdlabel); uppanel.add(pwdfield); uppanel.add(tolabel); uppanel.add(tofield); uppanel.add(subjectlabel); uppanel.add(subjectfield); uppanel.add(contentlabel); this.add(uppanel, borderlayout.north); this.add(contentarea, borderlayout.center); jpanel downpanel = new jpanel(new flowlayout(flowlayout.center)); downpanel.add(sendbtn, borderlayout.south); downpanel.add(cancelbtn, borderlayout.south); this.add(downpanel, borderlayout.south); } @override public void actionperformed(actionevent e) { if (e.getsource() == this.sendbtn) { this.email = new e_mail( this.fromfield.gettext(), this.tofield.gettext(), this.subjectfield.gettext(), this.contentarea.gettext(), this.usernamefield.gettext(), new string(this.pwdfield.getpassword()) ); this.mailsender.sendemail(this.email); } else if (e.getsource() == this.cancelbtn) { this.fromfield.settext(null); this.tofield.settext(null); this.subjectfield.settext(null); this.contentarea.settext(null); } } }
以上就是java编写简单e-mail发送端程序的全部代码,希望对大家的学习有所帮助。