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

由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 .

Server:250 2.0.0 Ok: queued as 74169608241

Server:221 2.0.0 Bye

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值