<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 56,  comments - 12,  trackbacks - 0
    實 現的原理非常簡單,首先建立和郵件服務器的Socket連接,然后進行和服務器握手,然后發送smtp指令,并封裝郵件體,然后發送即可。大家不妨一試。


    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("root@ehawa.com","???","???");
    smtp.addMailTo("root@ehawa.com");
    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){}
    }
    }
    posted on 2007-01-19 00:07 苦笑枯 閱讀(211) 評論(0)  編輯  收藏 所屬分類: Java
    收藏來自互聯網,僅供學習。若有侵權,請與我聯系!

    <2007年1月>
    31123456
    78910111213
    14151617181920
    21222324252627
    28293031123
    45678910

    常用鏈接

    留言簿(2)

    隨筆分類(56)

    隨筆檔案(56)

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 久久国内免费视频| 99热在线精品免费全部my| 成人免费无码视频在线网站| 亚洲色成人网一二三区| 免费看国产一级特黄aa大片| 久久久高清日本道免费观看| 亚洲综合激情五月色一区| 国产成人A亚洲精V品无码| 女人18毛片a级毛片免费视频| CAOPORN国产精品免费视频| 亚洲av无码专区在线电影天堂| 亚洲福利在线观看| 亚洲AV无码久久| 亚洲无人区一区二区三区| 免费国产成人午夜电影| 日本免费网站在线观看| 久久久久久99av无码免费网站 | 69视频免费在线观看| 中文字幕手机在线免费看电影 | 亚洲大尺度无码专区尤物| 激情综合色五月丁香六月亚洲| 国产99视频免费精品是看6| 永久黄网站色视频免费直播| 无码一区二区三区免费视频| 天天摸天天操免费播放小视频| 91精品视频免费| 国产无遮挡又黄又爽免费视频| 国产又长又粗又爽免费视频| 亚洲精品无码久久久久AV麻豆| 亚洲美女在线国产| 亚洲国产成人精品不卡青青草原| 亚洲av午夜福利精品一区人妖| 亚洲欧洲免费视频| 亚洲一区二区三区高清视频| 亚洲日韩AV一区二区三区四区| 国产精品亚洲色图| a级毛片在线免费| 成人毛片免费观看视频大全| av在线亚洲欧洲日产一区二区| 国产亚洲精品观看91在线| 亚洲AV无码一区二区三区在线|