package com.syj.mail.tool;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.springframework.mail.javamail.MimeMessagePreparator;
import com.syj.mail.tool.util.MailToolUtil;
import com.syj.util.console.WaitingInfoThread;
/**
* <p>
* Title:
* </p>
*
* <p>
* Description:
* </p>
*
* <p>
* Copyright: Copyright (c) 2007
* </p>
*
* <p>
* Company:
* </p>
*
* @author 孙钰佳
* @date 2007-9-15 上午10:26:14
*/
public class MailProcessor {
private com.syj.mail.tool.sender.MailSender mailSender;
private MailConfig mailConfig;
private MailInfo mailInfo;
/**
* 认证
*
*/
public void authenticator() {
while (true) {
System.out.print("\nUSERNAME: " + mailSender.getUsername());
if (mailSender.getUsername() == null
|| "".equals(mailSender.getUsername().trim()))
mailSender.setUsername(MailToolUtil.readSystemIn());
String password = MailHelper.readPassword();
if (password == null || "".equals(password.trim()))
password = mailSender.getPassword();
boolean isLoginSucc = MailHelper.authenticator(mailSender
.getSession().getProperties(), mailSender.getUsername(),
password);
if (isLoginSucc) {
mailSender.setPassword(password);
break;
}
}
fillNullValue();
}
/**
* 补填默认配置为空的信息
*
*/
public void fillNullValue() {
System.out.println("\nSMTP: " + mailSender.getHost());
System.out.print("\nFROM: ");
if (mailInfo.getFrom() == null || "".equals(mailInfo.getFrom().trim()))
mailInfo.setFrom(MailToolUtil.readSystemIn().trim());
else
System.out.print(mailInfo.getFrom());
System.out.print("\nTO: ");
if (mailInfo.getTo() == null || "".equals(mailInfo.getTo().trim()))
mailInfo.setTo(MailToolUtil.readSystemIn().trim());
else
System.out.print(mailInfo.getTo());
System.out.print("\nSUBJECT: ");
if (mailInfo.getSubject() == null
|| "".equals(mailInfo.getSubject().trim()))
mailInfo.setSubject(MailToolUtil.readSystemIn());
else
System.out.print(mailInfo.getSubject().trim());
System.out.print("\nTEXT: ");
if (mailInfo.getText() == null || "".equals(mailInfo.getText().trim()))
mailInfo.setText(MailToolUtil.readSystemIn());
else
System.out.print(mailInfo.getText().trim());
System.out.print("\nATTACHMENT: ");
if (mailConfig.getShowDirDialogType() == 1
|| mailConfig.getDir() == null
|| "".equals(mailConfig.getDir())) {
String dir = MailToolUtil
.selectDirectoryDialog(mailConfig.getDir());
if (dir != null)
mailConfig.setDir(dir);
}
System.out.println(mailConfig.getDir());
if (mailConfig.getIsClosePc() == null) {
System.out.print("发送完毕后是否自动关闭计算机(Y/N):");
mailConfig.setIsClosePc(MailToolUtil.readSystemIn());
} else {
System.out.println("发送完毕后是否自动关闭计算机:" + mailConfig.getIsClosePc());
}
}
/**
* 核心代码入口
*
*/
public void process() {
String dirStr = mailConfig.getDir();
File target = new File(dirStr);
maxFileHandle(target);
if (!target.isDirectory() && !target.isFile()) {
System.out.println("<-- " + dirStr + " 不是一个目录或文件 -->");
return;
}
sendMailInfoList(target);
}
/**
* 取得要发送的 MailInfo邮件对象 List
*
* @param target
* @return
*/
private List<MailInfo> getMailInfoList(File target) {
File[] files = getFiles(target);
List<MailInfo> list = new ArrayList<MailInfo>(files.length);
for (int i = 0; files != null && i < files.length; i++) {
File file = files[i];
if (!file.isFile() || file.length() > mailConfig.getMaxSize())
continue;
MailInfo curMailInfo = null;
try {
curMailInfo = (MailInfo) mailInfo.clone();
} catch (CloneNotSupportedException e1) {
e1.printStackTrace();
return new ArrayList<MailInfo>(0);
}
curMailInfo.setSubject(curMailInfo.getSubject() + "-"
+ file.getName());
curMailInfo
.setText(curMailInfo.getText() + "\r\n" + file.getName());
curMailInfo.setFileDataSource(new FileDataSource(file));
list.add(curMailInfo);
}
return list;
}
/**
* 发送MailInfo邮件对象 List
*
* @param target
*/
public void sendMailInfoList(File target) {
List<MailInfo> list = getMailInfoList(target);
if (list.size() == 0)
System.out.println("<-- 没有需要发送的邮件! -->");
for (int i = 0; i < list.size();) {
MailInfo curMailInfo = (MailInfo) list.get(i);
if (curMailInfo.getState() == -1 || curMailInfo.getState() == 1) {
i++;
continue;
} else if (curMailInfo.getState() == 0) {
try {
Date sDate = new Date();
String tryAginInfo = "";
if (curMailInfo.getFailedTimes() != 0)
tryAginInfo = " 第" + (curMailInfo.getFailedTimes() + 1)
+ "次发送";
System.out.println("\n<-- "
+ MailToolUtil.fmtDate(sDate)
+ " 正在发送 "
+ curMailInfo.getFileDataSource().getFile()
.getAbsolutePath() + tryAginInfo + " -->");
WaitingInfoThread wt = new WaitingInfoThread("发送中,请稍候 ", "");
wt.setMaxLength(10);
wt.start();
try {
sendMimeMessage(curMailInfo);
wt.stopThread();
} finally {
wt.stopThread();
}
Date eDate = new Date();
long useTime = ((eDate.getTime() - sDate.getTime()) / 1000) + 1;
System.out.println("\n<-- " + MailToolUtil.fmtDate(eDate)
+ " 发送成功 用时 " + useTime + " 秒 -->");
sendSuccHandle(curMailInfo);
} catch (Exception e) {
exHandle(e, curMailInfo);
}
}
report(list);
}
closePc();
}
/**
* 处理发送成功的邮件对象转移附件所在的文件夹
*
* @param mailInfo
*/
private void sendSuccHandle(MailInfo curMailInfo) {
curMailInfo.setState(1);
System.out.println("<-- 正在处理已发送成功的邮件附件! -->");
WaitingInfoThread wt = new WaitingInfoThread("处理中,请稍候 ", "");
wt.setMaxLength(10);
wt.start();
try {
File file = curMailInfo.getFileDataSource().getFile();
File target = new File(file.getParent() + "\\"
+ mailConfig.getSendSuccBakDir());
if (mailConfig.getSendSuccBakDir() == null
|| "".equals(mailConfig.getSendSuccBakDir())) {
file.delete();
System.out.println("\n<-- 删除已发送成功的邮件附件 "
+ curMailInfo.getFileDataSource().getFile().getPath()
+ " -->");
} else
MailToolUtil.moveFile(file, target);
wt.stopThread();
System.out.println("\n<-- 处理已发送成功的邮件附件完毕! -->");
} catch (Exception e) {
System.out.println("\n<-- 处理已发送成功的邮件附件时出错! -->");
e.printStackTrace();
} finally {
wt.stopThread();
}
}
/**
* 每次发送完一封邮件后的报告汇总信息
*
* @param list
*/
private void report(List<MailInfo> list) {
MailReport rt = getReport(list);
if (rt.getReadyTotal() != 0)
System.out.println("<-- 共 " + rt.getTotal() + " 封邮件,还有"
+ rt.getReadyTotal() + "封邮件待发送 -->");
else {
System.out.println("<-- 邮件发送完毕 共 " + rt.getTotal() + " 封邮件 "
+ rt.getSuccTotal() + " 封发送成功 " + rt.getFailTotal()
+ " 封发送失败 -->");
if (rt.getFailList().size() > 0) {
List<MailInfo> failList = rt.getFailList();
System.out.println("\n<-- 发送失败的附件文件列表 -->");
for (int i = 0; i < failLis
小小哭包
- 粉丝: 2089
- 资源: 4286
最新资源
- 基于SSM框架和微信小程序的健身房私教预约管理系统设计源码
- 基于微信小程序的南大学生会管理平台设计源码
- 基于PHP和CSS的幽默在线要饭系统设计源码
- 基于Java和HTML技术的Html转Word富文本转换设计源码
- 基于elementui的Vue TypeScript简洁高效设计源码
- 基于Vue的lojob-nav电子书阅读器网站设计源码
- 基于C++核心的轻量级JS框架arkui_ace_engine_lite设计源码
- 随机孔隙模型与随机小球生成仿真,可以用于光学仿真,微波颗粒小球加热加热仿真 comsol仿真与 comsol with matlab联合仿真
- comsol模拟碳和碳化硅复合材料微观氧化损伤 涉及化学接口与浓物质传递接口
- 基于Vue3+Element-Plus的现代化企业门户管理系统设计源码
- 解决vic水文模型模拟径流过低的问题
- 基于Python核心技术的最新版Python课件设计源码
- 基于PHP语言的canal组件设计源码
- 基于MPC的混合动力汽车能量管理策略 整车构型为P2构型的混合动力汽车
- 基于HarmonyOS平台的面向数字经济农产品的融销App设计源码
- 基于预测区间的配电网降峰储能系统优化运行
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈