java – 如何在Outlook中创建电子邮件并使其可见的用户
我想使用Outlook和OLE客户端创建一个使用
Java应用程序的电子邮件.
我搜索了例子,发现了不少.他们都以同样的方式开始: 创建显示,Shell,OLE框架和OLE客户端站点. 但是我有几个错误: Display display = new Display(); Shell shell = new Shell(display); shell.setText("Outlook Automation"); shell.setLayout(new FillLayout()); OleFrame frm = new OleFrame(shell,SWT.NONE); OleClientSite site = new OleClientSite(frm,SWT.NONE,"Outlook.Application"); 我收到以下错误: Exception in thread "main" org.eclipse.swt.SWTException: Failed to create Ole Client. result = -2147221164 at org.eclipse.swt.ole.win32.OLE.error(OLE.java:302) at org.eclipse.swt.ole.win32.OleClientSite.<init>(OleClientSite.java:242) at outlooktest.Main.main(Main.java:27) 我不知道OLE,我不知道我做错了什么.有没有一些依赖我失踪了?有人知道这个错误可能是什么吗?我搜索了错误代码,但没有找到任何东西. 编辑 那么如果没有人知道为什么OLE不适合我,我还有另一个问题.创建Outlook电子邮件并设置它(主体,正文等)是可能的,还是有图书馆,但是不能发送它,但是可以让用户看到它来更改内容? 编辑2 x86和x64 jar文件没有解决,同样的错误.另外我得到了最新版本的SWT for x86和x64. OS也是x64和java,所以我不能使用x86 SWT库.使用x64会发生上述错误. Outlook版本是15(Outlook 2013). 希望这有帮助吗? 我通过Processbuilder创建电子邮件,但只能使用mailto:参数.这里的问题是: >我想跟踪进程的状态.我想知道电子??邮件何时关闭/发送. 解决方法
对于我来说,这可以很好地根据
tutorial on vogella.com.我也尝试过你的最小代码示例,并在OLE客户端创建过程中没有错误.顺便说一下,我使用了SWT 4.3.
有一点偏离主题,但它必须是Outlook?我的意思是,你只想自动化电子邮件发送 – 你可以使用JavaMail并做到无头,即不自动化一个实际的GUI客户端.想要使用Outlook或其他电子邮件客户端的唯一理由是: >您需要发送的信息在您的外箱中作为参考. 但是如果它只是自动化电子邮件发送,就像我所说的,我会推荐JavaMail. 更新:我从其home page下载了SWT,在我的情况下是最新的稳定版release 4.3 for Windows.在ZIP存档中,您需要的文件是swt.jar. 我的示例代码看起来像这样,工作正常: package de.scrum_master.ole; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.ole.win32.OLE; import org.eclipse.swt.ole.win32.OleAutomation; import org.eclipse.swt.ole.win32.OleClientSite; import org.eclipse.swt.ole.win32.OleFrame; import org.eclipse.swt.ole.win32.Variant; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class OutlookMail { public static void main(String[] args) { sendEMail(); } public static void sendEMail() { Display display = new Display(); Shell shell = new Shell(display); OleFrame frame = new OleFrame(shell,SWT.NONE); // This should start outlook if it is not running yet // OleClientSite site = new OleClientSite(frame,"OVCtl.OVCtl"); // site.doVerb(OLE.OLEIVERB_INPLACEACTIVATE); // Now get the outlook application OleClientSite site2 = new OleClientSite(frame,"Outlook.Application"); OleAutomation outlook = new OleAutomation(site2); OleAutomation mail = invoke(outlook,"CreateItem",0 /* Mail item */).getAutomation(); setProperty(mail,"BodyFormat",2 /* HTML */); setProperty(mail,"Subject","My test subject"); // setProperty(mail,"From","my@sender.org"); setProperty(mail,"To","<John Doe> my@recipient.org"); setProperty(mail,"HtmlBody","<html><body>This is an <b>HTML</b> test body.</body></html>"); // if (null != attachmentPaths) { // for (String attachmentPath : attachmentPaths) { // File file = new File(attachmentPath); // if (file.exists()) { // OleAutomation attachments = getProperty(mail,"Attachments"); // invoke(attachments,"Add",attachmentPath); // } // } // } invoke(mail,"Display" /* or "Send" */); } private static OleAutomation getProperty(OleAutomation auto,String name) { Variant varResult = auto.getProperty(property(auto,name)); if (varResult != null && varResult.getType() != OLE.VT_EMPTY) { OleAutomation result = varResult.getAutomation(); varResult.dispose(); return result; } return null; } private static Variant invoke(OleAutomation auto,String command,String value) { return auto.invoke(property(auto,command),new Variant[] { new Variant(value) }); } private static Variant invoke(OleAutomation auto,String command) { return auto.invoke(property(auto,command)); } private static Variant invoke(OleAutomation auto,int value) { return auto.invoke(property(auto,new Variant[] { new Variant(value) }); } private static boolean setProperty(OleAutomation auto,String name,String value) { return auto.setProperty(property(auto,name),new Variant(value)); } private static boolean setProperty(OleAutomation auto,int value) { return auto.setProperty(property(auto,new Variant(value)); } private static int property(OleAutomation auto,String name) { return auto.getIDsOfNames(new String[] { name })[0]; } } 我在最后注释了附件部分,也是第一个OLE命令,因为对我来说,它也没有它.它不会损坏使用它,但也许你需要它.试试吧. 我发表标题“From”的原因是它没有效果.要更改发件人,您可能需要另一个代码片段来切换Outlook配置文件或配置文件切换器中的几个预配置的发件人.默认情况下,只会使用您的默认配置文件. 告诉我是否有帮助 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |