您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 电子设计/PCB > java socket 实现SMTP协议 发送邮件
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)){}else{returnfalse;}//quitStringquit=sendCommand(QUIT+lineFeet);if(isStartWith(quit,221)){}else{returnfalse;}close();returntrue;}privatebooleanisStartWith(Stringres,Stringwith){returnres.startsWith(with);}publicstaticvoidmain(String[]args){SocketMailmail=newSocketMail();mail.setMailServer(smtp.163.com);mail.setFrom(*****@163.com);mail.setTo(*****@163.com);mail.setContent(hello,thisisatestmail!);booleanboo=mail.sendMail();if(boo)System.out.println(邮件发送成功!);else{System.out.println(邮件发送失败!);}}}···························································································SMTP的连接和收发过程:a.建立TCP连接。b.客户端发送HELO命令以标识发件人自己的身份,然后客户端发送MAIL命令服务器端正希望以OK作为响应,表明准备接收。c.客户端发送RCPT命令,以标识该电子邮件的计划接收人,可以有多个RCPT行d.协商结束,发送邮件,用命令DATA发送e.以.表示结束输入内容一起发送出去f.结束此次发送,用QUIT命令退出。SMTP的基本命令集:HELO向服务器标识用户身份MAIL初始化邮件传输mailfrom:xxxRCPT标识单个的邮件接收人;常在MAIL命令后面可有多个rcptto:xxxDATA在单个或多个RCPT命令后,表示所有的邮件接收人已标识,初始化数据传输,以.结束。NOOP无操作,服务器应响应OKRSET重置会话,当前传输被取消QUIT结束会话POP3简介:在POP3协议中有三种状态,认可状态,处理状态,和更新状态。当客户机与服务器建立联系时,一旦客户机提供了自己身份并成功确认,即由认可状态转入处理状态,在完成相应的操作后客户机发出quit命令,则进入更新状态,更新之后最后重返认可状态。POP3基本命令集:USERusernamePASSpasswordSTAT请求服务器发回关于邮箱的统计资料,如邮件总数和总字节数LIST返回邮件数量和每个邮件的大小RETR[Msg#]返回由参数标识的邮件的全部文本DELE[Msg#]服务器将由参数标识的邮件标记为删除,由quit命令执行RSET服务器将重置所有标记为删除的邮件,用于撤消DELE命令NOOP服务器返回一个肯定的响应QUIT更新classPOP3Demo{privatestaticStringPOP3Server=pop.126.com;privatestaticStringUSERNAME=username;//实际应用中改成真实的用户名privatestaticStringPASSWORD=password;//实际应用中改成真实的密码publicstaticvoidmain(String[]args){intPOP3Port=110;Socketclient=null;try{//向POP3服务程序建立一个套接字连接。client=newSocket(POP3Demo.POP3Server,POP3Port);//创建一个BufferedReader对象,以便从套接字读取输出。InputStreamis=client.getInputStream();BufferedReadersockin=newBufferedReader(newInputStreamReader(is));//创建一个PrintWriter对象,以便向套接字写入内容。OutputStreamos=client.getOutputStream();PrintWritersockout=newPrintWriter(os,true);//显示同SMTP服务程序的握手过程。System.ou
本文标题:java socket 实现SMTP协议 发送邮件
链接地址:https://www.777doc.com/doc-51015 .html