Java的網絡功能非常強大,開發和使用也非常簡單,難怪microsoft極力要爭回程序語言的霸主地位。筆者根據smtp協議使用java Socket寫了一個發送郵件的程序,將此與各位分享。 實現的原理非常簡單,首先建立和郵件服務器的Socket連接,然後進行和服務器握手,然後發送smtp指令,並封裝郵件體,然後發送即可。大家不妨一試。由什麼問題可以和我聯繫:Email:[email protected];Homepage:http://www.ehawa.com。 import java.net.*; import java.io.*; import java.util.*; public class SMTPSender{ Socket socket=null; PrintWriter outData=null; BufferedReader inData=null; String smtpServer=""; String user=""; String pass=""; String from=""; String LINEFEED="/r/n"; boolean isNeedAuthLogin=false; Vector to=new Vector(); public static void main(String[] args){ SMTPSender smtp=new SMTPSender(); smtp.setMailServer("mail.ehawa.com"); smtp.setMailFrom("[email protected]","???","???"); smtp.addMailTo("[email protected]"); if(smtp.send("hello","這是一個測試!")){ System.out.println("郵件發送成功!"); }else System.out.println("郵件發送失敗!"); } public void setMailServer(String s){ smtpServer=s; } public void setMailFrom(String s,String uid,String pwd){ this.from=s; this.user=uid; this.pass=pwd; this.isNeedAuthLogin=(this.user!=null&&this.pass!=null&&!this.user.equals("")&&!this.pass.equals("")); } public boolean addMailTo(String mailAddr){ to.addElement(mailAddr); return true; } public boolean send(String subject,String content){ try{ if(smtpServer==null||smtpServer.equals(""))return false; if(from==null||from.equals(""))return false; if(to.size()<1)return false; socket=new Socket(smtpServer,25); outData=new PrintWriter(socket.getOutputStream()); inData=new BufferedReader(new InputStreamReader(socket.getInputStream())); //與郵件服務器連接成功 readResponse("220"); //HELO host sendRequest("HELO "+smtpServer+LINEFEED); readResponse("250"); if(isNeedAuthLogin){ //AUTH LOGIN sendRequest("AUTH LOGIN"+LINEFEED); readResponse("334"); //USERNAME: sendRequest(new String(Base64.encodeString(user))+LINEFEED); readResponse("334"); //PASSWORD: sendRequest(new String(Base64.encodeString(pass))+LINEFEED); readResponse("235"); } //MAIL FROM:<..> sendRequest("MAIL FROM:<"+from+">"+LINEFEED); readResponse("250"); //RCPT TO:<..> for(Enumeration enu=to.elements();enu.hasMoreElements();){ String to1=(String)enu.nextElement(); sendRequest("RCPT To:<"+to1+">"+LINEFEED); readResponse("250"); } //DATA sendRequest("DATA"+LINEFEED); readResponse("354"); //郵件內容 StringBuffer s1=new StringBuffer("From: <"+from+">"+LINEFEED); s1.append("To: <"+to+">"+LINEFEED); s1.append("Subject: "+subject+LINEFEED); s1.append("Date: "+new java.util.Date().toLocaleString()+LINEFEED); s1.append("Content-Type: text/plain;charset=/"GB2312/""+LINEFEED); s1.append(LINEFEED); s1.append(content); s1.append(LINEFEED+"."+LINEFEED);//發送 sendRequest(s1.toString()); readResponse("250"); //QUIT退出 sendRequest("QUIT"+LINEFEED); readResponse("221"); try{ inData.close(); inData=null; }catch(Exception ex){} try{ outData.close(); outData=null; }catch(Exception ex){} try{ socket.close(); socket=null; }catch(Exception ex){} }catch(Exception e){ return false; //e.printStackTrace(); } return true; } private void readResponse(String cmd)throws Exception{ String tmp=inData.readLine(); if(tmp.startsWith(cmd));//System.out.println(" [S:]"+tmp); else throw new Exception("##########郵件發送失敗!##########"+tmp); while(tmp.startsWith(cmd+"-"))tmp=inData.readLine(); } private void sendRequest(String msg){ //System.out.print("***[C:]"+msg); outData.write(msg); outData.flush(); } public void close(){ try{ inData.close(); inData=null; }catch(Exception ex){} try{ outData.close(); outData=null; }catch(Exception ex){} try{ socket.close(); socket=null; }catch(Exception ex){} } } |
http://java.e800.com.cn/articles/2007/126/1169755493413851662_1.html