Java批量发带远程附件的邮件1--获取远程文件

业务背景及需求:

公司需要统一发送同类型的邮件给一批人时,比如发送员工个人薪资表分别至个人邮箱。

此类邮件标题,内容,发送人等基本一致,且需要带附件,而其附件是收件人的隐私性个人信息。

方案:

可以这样处理:统一将要发送的附件放在确定路径下(支持远程路径),附件名称统一以收件人的id命名,访问远程路径权限及路径地址作为参数配置到系统里。例如:由操作发送邮件人名称命名的路径。

此时便可以:操作人单击receiver 按钮,系统根据配置参数到对应路径下遍历所有id命名的附件,并将其员工信息查询(邮箱地址)出来赋值,单击send email 按钮,发送时,遍历收件人列表,逐个发送
如果receiver list里带有附件,也一并发出带附件的收件人。

 访问远程路径:获得访问权限并登录

其中 sysParam从数据库中获取(系统参数已配置并保存在数据库里)

Table:SysParam  
Field NameField TypeNull(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");
		}

	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值