jacob:是一个Java-com桥,允许您从Java调用COM自动化组件。它使用JNI对COM库进行本机调用。Jacob在支持32位和64位JVM的x86和X64环境中运行。
官网地址:https://sourceforge.net/projects/jacob-project/
Java调用Outlook 进行发送邮件:
说明:
1、引用Jar可以从Maven仓库下,也可以从官网下
2、注意同时相同版本的dll要放入Java lib路径中。
异常:
1、Exception in thread "main" com.jacob.com.ComFailException: Invoke of: CreateItem
Source: Microsoft Outlook
Description: 操作失败。
可能由于Outlook已经打开了,需要关闭进行尝试
源码:
```
public static void main(String[] args) {
//Method 1 原理是打开outlook程序,然后人工手动进行发送
String body2 = "<div style='color:red;'>This is a Test !</div>";
// try {
// Runtime.getRuntime().exec("C:\\Program Files\\Microsoft Office\\root\\Office16\\OUTLOOK.EXE mailto:aa@aa.com?subject=Chinese New Year backup arrangement" +
// "&cc=aa@aa.com&body="+body2);
// } catch (IOException e) {
// e.printStackTrace();
// }
System.out.println(System.getProperty("java.library.path"));
//Method 2 后台发送邮件,outlook会一闪而过
ActiveXComponent axOutlook = new ActiveXComponent("Outlook.Application");
//Dispatch ds = axOutlook.getObject();
/* Dispatch namespace = Dispatch.call(axOutlook, "GetNamespace", "MAPI").toDispatch() ;
String path = new File("F:/test.htm").getAbsolutePath();
Dispatch mailItem = Dispatch.call(namespace, "OpenSharedItem", path).toDispatch(); */
Dispatch mailItem = Dispatch.call(axOutlook, "CreateItem", 0).getDispatch();
//Dispatch mailItem = Dispatch.call(axOutlook, "Open", "F:/test.htm").toDispatch();
// Dispatch body = Dispatch.call(mailItem, "Body").getDispatch();;
// Dispatch.call(body, "Copy");
Dispatch inspector = Dispatch.get(mailItem, "GetInspector").getDispatch();
//收件人
Dispatch recipients = Dispatch.call(mailItem, "Recipients").getDispatch();
Dispatch.call(recipients, "Add" , "18961740725@163.com");
//邮件主题
Dispatch.put(mailItem, "Subject", "Test5!");
// Dispatch.put(mailItem, "CC", "1@qq.com");
//Dispatch.put(mailItem, "CC", "cry@163.com");
//Dispatch.put(mailItem, "ReadReceiptRequested", "false");
String body = "<html><body><div style='color:red;'>This is a Test !</div></body></html>";
//Dispatch.put(mailItem, "HTMLBody", body);
// File file = new File("C:/test.htm");
// FileOutputStream f = new FileOutputStream("F:/test.htm");
// f.write(body.getBytes());
// f.close();
// BufferedReader bf = new BufferedReader(new FileReader(new File("F:/Ro.htm")));
// String html = "";
// String str = "";
// while( (str=bf.readLine()) != null){
// html += str;
// }
Dispatch.put(mailItem, "HTMLBody", body);
//Dispatch bodyDis = Dispatch.get(mailItem, "Body").toDispatch();
//Dispatch.call(bodyDis, "Add" , "F:/test.htm");
//Dispatch.put(mailItem, "Body", new Variant("F:/test.htm"));
//Dispatch.put(mailItem, "Body", body);
//Dispatch.call(mailItem, "InsertAfter",body );//插入一个段落
//Dispatch selection = Dispatch.get(axOutlook, "Selection").toDispatch();
//Dispatch inHtmleditor = Dispatch.call(inspector, "HTMLEditor").getDispatch();
// Dispatch.call(inHtmleditor, "Paste").getDispatch();
//Dispatch.invoke(inHtmleditor, "Paste", Dispatch.Method, new Object[]{false}, new Variant(false));
//附件
// Dispatch attachments = Dispatch.call(mailItem, "Attachments").getDispatch();
// Dispatch.call(attachments, "Add" , "D:\\20110127.txt");
// Dispatch.call(attachments, "Add" , "D:\\20110128.txt");
//显示新邮件窗口
Dispatch.call(mailItem, "Display");
Dispatch.call(mailItem, "Send");
}
```
关于jacob 还能干什么,我们再下一篇《Java -Jacob(2)-COM 组件》进行介绍。