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());
}
}
使用jacob调用outlook发送邮件
最新推荐文章于 2024-02-22 09:25:51 发布