电脑桌面
添加小米粒文库到电脑桌面
安装后可以在桌面快捷访问

java socket 实现SMTP协议 发送邮件VIP免费

java socket 实现SMTP协议 发送邮件_第1页
1/9
java socket 实现SMTP协议 发送邮件_第2页
2/9
java socket 实现SMTP协议 发送邮件_第3页
3/9
javasocket实现SMTP协议发送邮件文章分类:Java编程packagecom.socket.test;importjava.io.BufferedReader;importjava.io.DataOutputStream;importjava.io.IOException;importjava.io.InputStreamReader;importjava.net.Socket;importjava.net.UnknownHostException;importcom.sun.org.apache.xerces.internal.impl.dv.util.Base64;/***通过socket向smtp协议服务器发送邮件*@authorfuyanqing**/publicclassSocketMail{StringmailServer;Stringfrom;Stringto;Stringcontent;StringlineFeet="\r\n";privateintport=25;Socketclient;BufferedReaderin;DataOutputStreamos;publicStringgetContent(){returncontent;}publicvoidsetContent(Stringcontent){this.content=content;}publicStringgetMailServer(){returnmailServer;}publicvoidsetMailServer(StringmailServer){this.mailServer=mailServer;}publicStringgetFrom(){returnfrom;}publicvoidsetFrom(Stringfrom){this.from=from;}publicStringgetTo(){returnto;}publicvoidsetTo(Stringto){this.to=to;}/***初始化连接*@return*/privatebooleaninit(){booleanboo=true;if(mailServer==null||"".equals(mailServer)){returnfalse;}try{client=newSocket(mailServer,port);in=newBufferedReader(newInputStreamReader(client.getInputStream()));os=newDataOutputStream(client.getOutputStream());StringisConnect=response();if(isConnect.startsWith("220")){}else{System.out.println("建立连接失败:"+isConnect);boo=false;}}catch(UnknownHostExceptione){System.out.println("建立连接失败!");e.printStackTrace();boo=false;}catch(IOExceptione){System.out.println("读取流失败!");e.printStackTrace();boo=false;}returnboo;}/***发送smtp指令*并返回服务器响应信息*/privateStringsendCommand(Stringmsg){Stringresult=null;try{os.writeBytes(msg);os.flush();result=response();}catch(IOExceptione){e.printStackTrace();}returnresult;}/***读取服务器端响应信息*@return*/privateStringresponse(){Stringresult=null;try{result=in.readLine();}catch(IOExceptione){e.printStackTrace();}returnresult;}/***关闭*/privatevoidclose(){try{os.close();in.close();client.close();}catch(IOExceptione){e.printStackTrace();}}/***发送邮件*@return*/publicbooleansendMail(){//初始化if(client==null){if(init()){}else{returnfalse;}}//判断from,toif(from==null||from.isEmpty()||to==null||to.isEmpty()){returnfalse;}//进行握手Stringresult=sendCommand("HELO"+mailServer+lineFeet);if(isStartWith(result,"250")){}else{System.out.println("握手失败:"+result);returnfalse;}//验证发信人信息Stringauth=sendCommand("AUTHLOGIN"+lineFeet);if(isStartWith(auth,"334")){}else{returnfalse;}Stringuser=sendCommand(newString(Base64.encode("*****".getBytes()))+lineFeet);if(isStartWith(user,"334")){}else{returnfalse;}Stringpass=sendCommand(newString(Base64.encode("*****".getBytes()))+lineFeet);if(isStartWith(pass,"235")){}else{returnfalse;}//发送指令Stringf=sendCommand("MailFrom:<"+from+">"+lineFeet);if(isStartWith(f,"250")){}else{returnfalse;}StringtoStr=sendCommand("RCPTTO:<"+to+">"+lineFeet);if(isStartWith(toStr,"250")){}else{returnfalse;}Stringdata=sendCommand("DATA"+lineFeet);if(isStartWith(data,"354")){}else{returnfalse;}StringBuildersb=newStringBuilder();sb.append("From:<"+from+">"+lineFeet);sb.append("To:<"+to+">"+lineFeet);sb.append("Subject:test"+lineFeet);sb.append("Date:2010/10/2717:30"+lineFeet);sb.append("Content-Type:text/plain;charset=\"GB2312\""+lineFeet);sb.append(lineFeet);sb.append(content);sb.append(lineFeet+"."+lineFeet);StringconStr=sendCommand(sb.toString());if(isStartWith(conStr,"250")){}...

1、当您付费下载文档后,您只拥有了使用权限,并不意味着购买了版权,文档只能用于自身使用,不得用于其他商业用途(如 [转卖]进行直接盈利或[编辑后售卖]进行间接盈利)。
2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。
3、如文档内容存在违规,或者侵犯商业秘密、侵犯著作权等,请点击“违规举报”。

碎片内容

java socket 实现SMTP协议 发送邮件

确认删除?
VIP
微信客服
  • 扫码咨询
会员Q群
  • 会员专属群点击这里加入QQ群
客服邮箱
回到顶部