![](/images/780-200-2.jpg)
用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(); } } }
![](/images/780-200-2.jpg)
最新推荐