java邮件收发程序
更新时间:2024-05-27 07:10:01 阅读量: 综合文库 文档下载
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException;
import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException; import java.util.StringTokenizer;
public class SMTPClient {
private boolean debug = true;
BASE64Encoder encode = new BASE64Encoder();// 自定义BASE64Encoder工具类用于加密字符串
public static void main(String[] args) throws UnknownHostException, IOException {
MailMessage message = new MailMessage();
message.setFrom(\发件人
message.setTo(\多个收件人地址间用逗号隔开 String server = \邮件服务器 message.setSubject(\VA MAIL测试\邮件主题 message.setContent(\你好!这里是系统发出的JAVA MAIL测试,请勿回复。\邮
件内容
message.setDataFrom(\发件人,在邮件的发件人栏目中显示 message.setDataTo(\收件人,在邮件的收件人栏目中显示 message.setUser(\登陆的邮箱账号
message.setPassword(\登陆邮箱的密码,建议自己保密好,公开传播会泄密
SMTPClient smtp = new SMTPClient(server, 25); boolean flag;
flag = smtp.sendMail(message, server);
if (flag) { System.out.println(\邮件发送成功!\ } else { System.out.println(\邮件发送失败!\ } }
private Socket socket;
public SMTPClient(String server, int port) throws UnknownHostException, IOException { try {
socket = new Socket(server, 25); } catch (SocketException e) {
System.out.println(e.getMessage()); } catch (Exception e) { e.printStackTrace();
} finally {
System.out.println(\已经建立连接!\ } }
// 注册到邮件服务器
public void helo(String server, BufferedReader in, BufferedWriter out) throws IOException {
int result;
result = getResult(in);
// 连接上邮件服务后,服务器给出220应答 if (result != 220) {
throw new IOException(\连接服务器失败\ }
result = sendServer(\ // HELO命令成功后返回250
if (result != 250) {
throw new IOException(\注册邮件服务器失败!\ } }
private int sendServer(String str, BufferedReader in, BufferedWriter out) throws IOException { out.write(str); out.newLine(); out.flush();
if (debug) {
System.out.println(\已发送命令:\ }
return getResult(in); }
public int getResult(BufferedReader in) { String line = \ try {
line = in.readLine();
if (debug) {
System.out.println(\服务器返回状态:\ }
} catch (Exception e) {
e.printStackTrace(); }
// 从服务器返回消息中读出状态码,将其转换成整数返回 StringTokenizer st = new StringTokenizer(line, \ return Integer.parseInt(st.nextToken()); }
public void authLogin(MailMessage message, BufferedReader in, BufferedWriter out) throws IOException { int result;
result = sendServer(\ if (result != 334) {
throw new IOException(\用户验证失败!\ }
result = sendServer(encode.encode(message.getUser().getBytes()), in, out); if (result != 334) {
throw new IOException(\错误!\ }
result = sendServer(encode.encode(message.getPassword().getBytes()), in, out); if (result != 235) {
throw new IOException(\验证失败!\ } }
// 开始发送消息,邮件源地址
public void mailFrom(String source, BufferedReader in, BufferedWriter out) throws IOException { int result;
result = sendServer(\ if (result != 250) {
throw new IOException(\指定源地址错误\ } }
// 设置邮件收件人。多邮件发送用\隔开
public void rcpt(String touchman, BufferedReader in, BufferedWriter out) throws IOException {
//String[] mailList = touchman.split(\ /*if (mailList.length > 1) {
for (String touch : mailList) {
connectionServer(touch,in,out); } }else */
connectionServer(touchman,in,out); }
private void connectionServer(String touch, BufferedReader in, BufferedWriter out) throws IOException { int result;
result = sendServer(\ if (result != 250) { throw new IOException(\指定目的地址错误!\ } }
// 邮件体
public void data(String from, String to, String subject, String content, BufferedReader in, BufferedWriter out) throws IOException { int result;
result = sendServer(\TA\ // 输入DATA回车后,若收到354应答后,继续输入邮件内容 if (result != 354) {
throw new IOException(\不能发送数据\ }
out.write(\ out.newLine(); out.write(\ out.newLine();
out.write(\ out.newLine(); out.newLine();
out.write(content); out.newLine();
// 句号加回车结束邮件内容输入 result = sendServer(\
System.out.println(result); if (result != 250) {
throw new IOException(\发送数据错误\ } }
// 退出
public void quit(BufferedReader in, BufferedWriter out) throws IOException { int result;
result = sendServer(\
if (result != 221) {
throw new IOException(\未能正确退出\ } }
// 发送邮件主程序
public boolean sendMail(MailMessage message, String server) {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(socket .getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter( socket.getOutputStream())); helo(server, in, out);// HELO命令
authLogin(message, in, out);// AUTH LOGIN命令 mailFrom(message.getFrom(), in, out);// MAIL FROM rcpt(message.getTo(), in, out);// RCPT
data(message.getDataFrom(), message.getDataTo(), message .getSubject(), message.getContent(), in, out);// DATA quit(in, out);// QUIT } catch (Exception e) { e.printStackTrace(); return false;
}
return true; } }
/**
* BASE64Encoder加密类 */
//package mail; /**
* @author Daniel Cheng * */
public class BASE64Encoder {
private static char[] codec_table = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
public BASE64Encoder() { }
public String encode(byte[] a) { int totalBits = a.length * 8; int nn = totalBits % 6;
int curPos = 0;// process bits
StringBuffer toReturn = new StringBuffer(); while (curPos < totalBits) { int bytePos = curPos / 8; switch (curPos % 8) {
case 0:
toReturn.append(codec_table[(a[bytePos] & 0xfc) >> 2]); break; case 2:
toReturn.append(codec_table[(a[bytePos] & 0x3f)]); break; case 4:
if (bytePos == a.length - 1) {
toReturn
.append(codec_table[((a[bytePos] & 0x0f) << 2) & 0x3f]); } else {
int pos = (((a[bytePos] & 0x0f) << 2) | ((a[bytePos + 1] & 0xc0) >> 6)) & 0x3f;
toReturn.append(codec_table[pos]); } break; case 6:
if (bytePos == a.length - 1) { toReturn
.append(codec_table[((a[bytePos] & 0x03) << 4) & 0x3f]); } else {
int pos = (((a[bytePos] & 0x03) << 4) | ((a[bytePos + 1] & 0xf0) >> 4)) & 0x3f;
toReturn.append(codec_table[pos]); }
break; default:
//never hanppen break; }
curPos+=6; } if(nn==2) {
toReturn.append(\ }
else if(nn==4)
{
toReturn.append(\ }
return toReturn.toString(); } }
/**
* 邮件实体POJO类 */
//package mail; /**
* @author Daniel Cheng * */
public class MailMessage { private String from; private String to;
private String subject; private String content; private String dataFrom; private String dataTo; private String user; private String password; /** * */
public MailMessage() {
super();
// TODO Auto-generated constructor stub } /**
* @param from * @param to * @param subject * @param content * @param dataFrom * @param dataTo * @param user * @param password */
public MailMessage(String from, String to, String subject, String content, String dataFrom, String dataTo, String user, String password) { super();
this.from = from; this.to = to;
this.subject = subject; this.content = content;
this.dataFrom = dataFrom; this.dataTo = dataTo; this.user = user;
this.password = password;
}
public String getFrom() { return from; }
public void setFrom(String from) { this.from = from; }
public String getTo() { return to;
}
public void setTo(String to) { this.to = to;
}
public String getSubject() { return subject; }
public void setSubject(String subject) { this.subject = subject; }
public String getContent() {
return content; }
public void setContent(String content) { this.content = content; }
public String getDataFrom() { return dataFrom; }
public void setDataFrom(String dataFrom) { this.dataFrom = dataFrom; }
public String getDataTo() { return dataTo;
}
public void setDataTo(String dataTo) { this.dataTo = dataTo; }
public String getUser() { return user; }
public void setUser(String user) { this.user = user; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; } }
正在阅读:
java邮件收发程序05-27
2010年大学英语三级(cet3)考试A级全真模拟试卷(7)-中大网校02-29
概括段意专题练习11-19
最新2011年职称英语考试大纲词汇表06-01
基于RFID技术的图书信息采集系统设计_本科毕业论文09-06
贵州大学考研机械设计试题库及答案201401-28
基于matlab的人脸识别系统设计与仿真(含源文件) - 图文12-21
测量学练习题全部07-05
宏观习题第9章10-18
断臂女孩02-19
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 收发
- 邮件
- 程序
- java
- 八年级第一次月考试题
- 这样敷脸缓解皮肤老化
- 班主任技能大赛 案例分析题
- 《全新版大学英语(第二版)快速阅读1》部分原文
- EXCEL中的表格,行变列,列变行
- 基于单片机的步进电机控制系统设计
- 质量管理体系
- 爱的教育试题
- 微机原理习题答案
- 概论-期末复习提纲
- 管理会计习题集1(本科)
- 基础上拔稳定计算公式
- 2012—2013年度北师大版八年级历史上册期末复习题二
- 开关柜无线测温系统说明书
- 药监生产批发企业出入库应用组件用户使用手册V1.1.3.0
- 关于酒店建设、筹建和开业初期营运的建议
- 江苏省苏州市2019届高三调研测试数学试题 Word版含答案
- 历年住院医师规范化培训考试试题
- 2012年最新成都公招教师培训班内部复习资料
- IC design flow& relevant desing tools(集成电路设计流程与EDA