第1页共10页编号:时间:2021年x月x日书山有路勤为径,学海无涯苦作舟页码:第1页共10页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(){第2页共10页第1页共10页编号:时间:2021年x月x日书山有路勤为径,学海无涯苦作舟页码:第2页共10页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;}/**第3页共10页第2页共10页编号:时间:2021年x月x日书山有路勤为径,学海无涯苦作舟页码:第3页共10页*发送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第4页共10页第3页共10页编号:时间:2021年x月x日书山有路勤为径,学海无涯苦作舟页码:第4页共10页*/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{第5页共10页第4页共10页编号:时间:2021年x月x日书山有路勤为径,学海无涯苦作舟页码:第5页共10页returnfalse;}StringtoStr=sendCommand("RCPTTO:<"+to+">"+lineFeet);if(isStartWith(toStr,"250")){}else{returnfalse;}Stringdata=sendCommand("DA...