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
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
内容概要:MailTool源代码工程。 作用:指定目标邮箱,目标目录,文件大小限制,目录下超过指定大小的文件会被自动分卷压缩批量发送。 能学到什么:1,邮件收发;2,文件切分,3,文件压缩分卷 适合人群:有一点Java基础。
资源推荐
资源详情
资源评论
收起资源包目录
MailTool邮件附件发生器(自动分卷压缩).zip (51个子文件)
MailTool邮件附件发生器(自动分卷压缩)
MailTool
MailTool
bin
run.cmd 188B
xercesImpl.jar 1.17MB
MailTool.jar 20KB
xml-apis.jar 190KB
spring.jar 2.54MB
mail.jar 320KB
activation.jar 53KB
SyjPasswordDLL.dll 9KB
swt.jar 1.38MB
Rar.exe 324KB
commons-logging.jar 52KB
appContext.properties 906B
startup.cmd 21B
lib
xercesImpl.jar 1.17MB
xml-apis.jar 190KB
spring.jar 2.54MB
mail.jar 320KB
activation.jar 53KB
swt.jar 1.38MB
commons-logging.jar 52KB
.classpath 959B
src
resources
run.cmd 188B
swt-xulrunner-win32-3422.dll 48KB
swt-gdip-win32-3422.dll 76KB
swt-wgl-win32-3422.dll 40KB
swt-awt-win32-3422.dll 32KB
swt-win32-3422.dll 316KB
appContext.xml 2KB
SyjPasswordDLL.dll 9KB
startup.cmd 21B
Rar.exe 324KB
appContext.properties 907B
java
com
syj
jni
PasswordReader
JNIPasswordReader.java 171B
mail
tool
MailToolRunner.java 528B
MailReport.java 1KB
sender
MailSenderImpl.java 411B
MailSender.java 4KB
MailProcessor.java 15KB
MailHelper.java 3KB
MailInfo.java 2KB
util
MailToolUtil.java 4KB
SpringUtil.java 908B
MailConfig.java 2KB
util
console
WaitingInfoThread.java 748B
pwd
PasswordUtil.java 2KB
MaskingThread.java 925B
RefreshConsoleThread.java 3KB
build.properties 366B
.project 384B
.fatjar 654B
build.xml 2KB
共 51 条
- 1
资源评论
小小哭包
- 粉丝: 2050
- 资源: 4206
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功