PC6下载站

分类分类

JAVA编写ESMTP客户端发送邮件代码

关注+2011-02-11作者:佚名

 由SOHU向网易邮件

1、效果

 2、代码

import java.io.*;
import java.net.*;
public class Smtptest {
public static void main(String[] args) {
  // TODO Auto-generated method stub
  Socket smtpclient=null;
  DataOutputStream os=null;
  BufferedReader is=null;
    String answer=null;
try{       
         smtpclient=new Socket("smtp.sohu.com",25);
         is=new BufferedReader(new InputStreamReader(smtpclient.getInputStream()));
         os=new DataOutputStream(smtpclient.getOutputStream());        
        }
        catch(UnknownHostException ue){
         System.err.println("未知主机");
        }
        catch(IOException io){
         System.err.println("I/O错误");
        }
        try{
         System.out.println("正在登录邮箱服务器....");
         os.writeBytes("EHLO localhost\r\n"); 
            while ((answer=is.readLine())!=null){
             System.out.println("Server:"+answer);
             if (answer.indexOf("220")!=-1){
              break;
             }
            }  
         System.out.println("正在检测邮箱支持的服务....");          
         os.writeBytes("AUTH LOGIN\r\n"); 
            while ((answer=is.readLine())!=null){
             if (answer.indexOf("250")==-1){
              break;
             }            
             System.out.println("Server:"+answer);
            }
         System.out.println("正在验证用户名和密码....");               
         System.out.println("Server:"+answer); 
         os.writeBytes("bmhzb2Z0\r\n");//用户名的BASE64值
         os.writeBytes("********\r\n");//密码的BASE64值,这里用*代替
            while ((answer=is.readLine())!=null){
             System.out.println("Server:"+answer);
             if (answer.indexOf("235")!=-1){
                 System.out.println("验证成功");
                 break;
             }
             else if (answer.indexOf("334")==-1) {
                 System.out.println("验证失败"); 
                    os.close();
                    is.close();
                    smtpclient.close(); 
                    System.exit(0);
             }
            }        
         System.out.println("开始发送邮件....");           
         os.writeBytes("MAIL From: < nhsoft@sohu.com >\r\n");  
         os.writeBytes("RCPT To: < nhsoft@sohu.com >\r\n");
         os.writeBytes("RCPT To: < nhsoft@126.com >\r\n");         
     os.writeBytes("DATA\r\n");
            while ((answer=is.readLine())!=null){
             if (answer.indexOf("354")!=-1){
              break;
             }            
             System.out.println("Server:"+answer);
            }
         System.out.println("Server:"+answer);            
         System.out.println("正在发送邮件内容....");
         os.writeBytes("From: nhsoft@sohu.com\r\n");
            os.writeBytes("To: nhsoft@126.com,nhsoft@sohu.com\r\n");
            os.writeBytes("Subject: test mail-21\r\n");
            os.writeBytes("Content-Type: text/html\r\n");        
            os.writeBytes("This is a test-21 mail, you don't reply it.\r\n\r\n");
         os.writeBytes("\r\n.\r\n");
            while ((answer=is.readLine())!=null){
             System.out.println("Server:"+answer);
             if (answer.indexOf("250")!=-1){
              break;
             }            
            }
            os.writeBytes("QUIT\r\n");
            while ((answer=is.readLine())!=null){
             System.out.println("Server:"+answer);
             if (answer.indexOf("221")!=-1){
                 System.out.println("邮件发送成功,退出邮箱!");              
              break;
             }            
            }
          
            os.close();
            is.close();
            smtpclient.close();          
        }
        catch(UnknownHostException ue){
         System.err.println("无法连接主机");
        }
        catch(IOException io){
         System.err.println("发送I/O错误");
        }

 }

}
3、执行情况:

正在登录邮箱服务器....

 Server:220 smtp.sohu.com ESMTP Postfix

 正在检测邮箱支持的服务....

 Server:250-smtp.sohu.com

 Server:250-PIPELINING

 Server:250-SIZE 10240000

 Server:250-VRFY

 Server:250-ETRN

 Server:250-AUTH LOGIN

 Server:250-ENHANCEDSTATUSCODES

 Server:250-8BITMIME

 server:250 DSN

 正在验证用户名和密码....

 Server:334 VXNlcm5hbWU6

Server:334 UGFzc3dvcmQ6

 Server:235 2.0.0 Authentication successful

 验证成功

 开始发送邮件....

 Server:354 Send from Rising mail proxy

 正在发送邮件内容....

 Server:250

 Server:250 2.1.5 Ok

 Server:354 End data with <CR><LF>.<CR><LF>

 Server:250 2.0.0 Ok: queued as 74169608241

 Server:221 2.0.0 Bye

 邮件发送成功,退出邮箱!

展开全部

相关文章

更多+相同厂商

热门推荐

  • 最新排行
  • 最热排行
  • 评分最高
排行榜

    点击查看更多

      点击查看更多

        点击查看更多

        说两句网友评论

          我要评论...
          取消