欢迎来到天天文库
浏览记录
ID:41566691
大小:65.03 KB
页数:9页
时间:2019-08-27
《javasocket实现SMTP协议发送邮件》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、javasocket实现SMTP协议发送邮件文章分类:Java编程packagecom.socket.test;importjava.io.BufferedReader;importjava.io.DataOutputStream;importjava.io」OException;importjava.ioputStreamReader;importjava.net.Socket;importjava.net.UnknownHostException;importcom.sun.org.apache.xerces.internal・impl・dv.util・Base64;/***通过soc
2、ket向smtp协议服务器发送邮件*@authorfuyanqing♦*/publicclassSocketMail{StringmailServer;Stringfrom;Stringto;Stringcontent;StringlineFeet="r";privateintport=25;Socketdient;BufferedReaderin;DataOutputStreamos;publicStringgetContent(){returncontent;}publicvoidsetContent(Stringcontent){this.content=content;}pu
3、blicStringgetMailServerf){returnmailServer;}publicvoidsetMailServerfStringmailServer){this.mailServer=mailServer;}publicStringgetFrom(){returnfrom;}publicvoidsetFrom(Stringfrom){this.from=from;}publicStringgetTo(){returnto;}publicvoidsetTo(Stringto){this.to=to;}/***初始化连接*(©return*/privatebooleanin
4、it(){booleanboo=true;if(mailServer=二null11,,H.equals(mailServer)){returnfalse;}try{dient=newSocket(mailServer,port);in=newBufferedReader(newInputStreamReaderfclient.getlnputStream()));os=newDataOutputStream(client.getOutputStream());StringisConnect=response();if(isConnect.startsWith("220")){}else{
5、System.out.printing建立连接失败:*'+isConnect);boo=false;}}catch(UnknownHostExceptione){System.out.println(n建立连接失败!“);e.printStackTrace();boo=false;}catch(lOExceptione){System.out.println(”读取流失败!”);e.printStackTrace();boo=false;}returnboo;}/***发送smtp指令*并返回服务器响应信息privateStringsendCommand(Stringmsg){String
6、result=null;try{os.writeBytes(msg);os.flush();result=response();}catch(lOExceptione){e.printStackTrace();}returnresult;}/***读取服务器端响应信息*@return7privateStringresponse(){Stringresult=null;try{result=in.readUne();}catch(lOExceptione){e.printStackTrace();}returnresult;}/***关闭*/privatevoidclose(){try{os
7、.close();in.close();client.close();}catch(lOExceptione){e.printStackTrace();}}/***发送邮件*@returnTpublicbooleansendMail(){〃初始化if(client==null){if(init()){}else{returnfalse;}}〃判断from,toifffrom二二null11from.isEmpty()11
此文档下载收益归作者所有