使用jacob调用outlook发送邮件

public void sendEmail(List<EmaillogInfo> emaillogInfoList,int flag) throws Exception {
		ActiveXComponent axOutlook = null;
		try{
			axOutlook = new ActiveXComponent("Outlook.Application"); 
		}
		catch (Exception e) {
			MsgBox.showInfo("调用outlook失败,无法发送邮件");
		}

		IEmaillog ieEmaillog = EmaillogFactory.getRemoteInstance();
		EmaillogInfo emaillogInfo = null;
		List<String> idlist = new ArrayList<String>();
		List<String> successlist = new ArrayList<String>();
		List<String> failedlist = new ArrayList<String>();
		for(int i = 0; i <  emaillogInfoList.size(); i++) {
			emaillogInfo = emaillogInfoList.get(i);
			try {
				Dispatch mailItem = (Dispatch) Dispatch.call(axOutlook, "CreateItem", 0).getDispatch();

				//设置在不在新的邮件界面显示
				Dispatch.call(mailItem, "Display");

				//设置收件人   ,Add是固定用法, Recipients---收件人,Add---地址
				Dispatch recipients = (Dispatch) Dispatch.call(mailItem, "Recipients").getDispatch(); 
				String receiver = emaillogInfo.getReceiver();
				String[] split = receiver.split(";");
				for (int j = 0; j < split.length; j++) {
					Dispatch.call(recipients, "Add", split[j]);
				}

				//添加抄送人 
				Dispatch.put(mailItem, "CC", emaillogInfo.getCc());

				//邮件主题   Subject---主题 
				Dispatch.put(mailItem, "Subject", emaillogInfo.getSubject());

				//内容
				String content = emaillogInfo.getContent();
				String html = "<div style='font-size: 14px;color:#1F4E79;font-family: Arial;'>"+content+"</div>";//识别换行等
				Dispatch.put(mailItem, "HTMLBody",  html+Dispatch.get(mailItem, "HTMLBody"));

				//附件
				Dispatch attachments = (Dispatch) Dispatch.call(mailItem, "Attachments").getDispatch(); 
				Dispatch.call(attachments, "Add" , emaillogInfo.get("filepath").toString());     

				//发送
				Dispatch.call(mailItem, "Send");

				ieEmaillog.save(emaillogInfo);
				//将状态置为“发送成功”
				successlist.add(emaillogInfo.getBoid());

			} catch (Exception e) {
				//发送邮件异常的话要把发送邮件状态置为失败
				failedlist.add(emaillogInfo.getBoid());
			}
		}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值