用Java编写邮件发送程序

发布时间:2024-02-25 13:53:54
importjava.net.*; importjava.io.*; importjava.net.*; publicclasssendElvisMail{ publicstaticvoidmain(Strings[]){ // //SendfakemailfromElvisPresley // //sendElvisMail[mailserver][recipientaddress] //mailservercanbehostnameorIPaddress // //ex.sendElvisMailmail.company.com[email protected] // sendElvisMailt=newsendElvisMail(); t.sendMail(s[0],s[1]); } publicvoidsendMail(StringmailServer,Stringrecipient){ try{ Sockets=newSocket(mailServer,25); BufferedReaderin=newBufferedReader (newInputStreamReader(s.getInputStream(),"8859_1")); BufferedWriterout=newBufferedWriter (newOutputStreamWriter(s.getOutputStream(),8859_1); send(in,out,"HELOtheWorld"); //warning:somemailservervalidatethesenderaddress //intheMAILFROmcommand,putyourrealaddresshere send(in,out,"MAILFROM:<[email protected]>"); send(in,out,"RCPTTO:"+recipient); send(in,out,"DATA"); send(out,"Subject:Intheghetto"); send(out,"From:ElvisPresley<[email protected]>"); send(out,"\n"); //messagebody send(out,"I'malive.Helpme!"); send(out,"\n.\n"); send(in,out,"QUIT"); s.close(); } catch(Exceptione){ e.printStackTrace(); } } publicvoidsend(BufferedReaderin,BufferedWriterout,Strings){ try{ out.write(s+"\n"); out.flush(); System.out.println(s); s=in.readLine(); System.out.println(s); } catch(Exceptione){ e.printStackTrace(); } } publicvoidsend(BufferedWriterout,Strings){ try{ out.write(s+"\n"); out.flush(); System.out.println(s); } catch(Exceptione){ e.printStackTrace(); } } }
上一篇 利用Java实现网络通信
下一篇 使用Java制作多点发送程序

文章素材均来源于网络,如有侵权,请联系管理员删除。

标签: Java教程Java基础Java编程技巧面试题Java面试题