业务背景及需求:
公司需要统一发送同类型的邮件给一批人时,比如发送员工个人薪资表分别至个人邮箱。
此类邮件标题,内容,发送人等基本一致,且需要带附件,而其附件是收件人的隐私性个人信息。
方案:
可以这样处理:统一将要发送的附件放在确定路径下(支持远程路径),附件名称统一以收件人的id命名,访问远程路径权限及路径地址作为参数配置到系统里。例如:由操作发送邮件人名称命名的路径。
此时便可以:操作人单击receiver 按钮,系统根据配置参数到对应路径下遍历所有id命名的附件,并将其员工信息查询(邮箱地址)出来赋值,单击send email 按钮,发送时,遍历收件人列表,逐个发送
如果receiver list里带有附件,也一并发出带附件的收件人。
访问远程路径:获得访问权限并登录
其中 sysParam从数据库中获取(系统参数已配置并保存在数据库里)
Table:SysParam | ||
Field Name | Field Type | Null(Y/N) |
id_ | nvarchar(32) | not null |
category_ | nvarchar(128) | null |
type_ | nvarchar(128) | null |
code_ | nvarchar(128) | not null |
data1_ | nvarchar(128) | not null |
data2_ | nvarchar(128) | null |
public class Constants {
// system parameter code constants
public static final String SYS_PARAM_CODE_REMOTEHOST = "remotehost";
public static final String SYS_PARAM_CODE_REMOTE_DOMAIN = "domain";
public static final String SYS_PARAM_CODE_REMOTE_USERNAME = "username";
public static final String SYS_PARAM_CODE__REMOTE_PASSWORD = "password";
public static final String SYS_PARAM_CODE_REMOTE_FOLDER_BASEURL = "remotefolderbaseurl";
}
private SmbFile loginRemote(String loginId) throws Exception {
try {
/* Read remote folder configuration from system param */
/*
* EBatchMailSysParam remoteHostSysParam = eBatchMailSysParamDao
* .getByCode(Constants.SYS_PARAM_CODE_REMOTEHOST); if (remoteHostSysParam ==
* null || remoteHostSysParam.getData1() == null) { throw new
* Exception("Please add remotehost config into sys param"); }
*/
/*
* EBatchMailSysParam remoteDomainSysParam = eBatchMailSysParamDao
* .getByCode(Constants.SYS_PARAM_CODE_REMOTE_DOMAIN); if (remoteDomainSysParam
* == null || remoteDomainSysParam.getData1() == null) { throw new
* Exception("Please add domain config into sys param"); }
*/
EBatchMailSysParam remoteUsernameSysParam = eBatchMailSysParamDao
.getByCode(Constants.SYS_PARAM_CODE_REMOTE_USERNAME);
if (remoteUsernameSysParam == null || remoteUsernameSysParam.getData1() == null) {
throw new Exception("Please add username config into sys param");
}
EBatchMailSysParam remotePasswordSysParam = eBatchMailSysParamDao
.getByCode(Constants.SYS_PARAM_CODE__REMOTE_PASSWORD);
if (remotePasswordSysParam == null || remotePasswordSysParam.getData1() == null) {
throw new Exception("Please add password config into sys param");
}
EBatchMailSysParam remoteFolderBaseUrlSysParam = eBatchMailSysParamDao
.getByCode(Constants.SYS_PARAM_CODE_REMOTE_FOLDER_BASEURL);
if (remoteFolderBaseUrlSysParam == null || remoteFolderBaseUrlSysParam.getData1() == null) {
throw new Exception("Please add remotefolderbaseurl config into sys param");
}
EBatchMailSysParam userFolderSysParam = eBatchMailSysParamDao.getByCode(loginId);
if (userFolderSysParam == null || userFolderSysParam.getData1() == null) {
throw new Exception("Please add config into sys param for " + loginId);
}
// UniAddress dc = UniAddress.getByName(remoteHostSysParam.getData1());
// NtlmPasswordAuthentication auth = new
// NtlmPasswordAuthentication(remoteDomainSysParam.getData1(),remoteUsernameSysParam.getData1(),
// remotePasswordSysParam.getData1());
// SmbSession.logon(dc, auth);
// NtlmPasswordAuthentication需要三个参数:(域名,用户名,密码)。域名没有的话填null
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("", remoteUsernameSysParam.getData1(),
remotePasswordSysParam.getData1());
SmbFile out = new SmbFile(
remoteFolderBaseUrlSysParam.getData1() + "/" + userFolderSysParam.getData1() + "/", auth);
return out;
} catch (Exception e) {
throw new Exception("Error to get into remote folder");
}
}