根据tutorial on vogella.com,这对我来说效果很好。我还尝试了您的最小代码示例,并且在 OLE 客户端创建过程中没有出错。顺便说一句,我使用的是 SWT 4.3。
有点离题,但一定是 Outlook 吗?我的意思是,您是否只想自动化发送电子邮件 - 您可以使用 JavaMail 并无头地进行,即无需自动化实际的 GUI 客户端。我想使用 Outlook 或其他电子邮件客户端的唯一原因是:
- 您需要发件箱中发送的消息以供参考。
- Outlook 已连接到配置为不接受 JavaMail 使用的 SMTP 连接的 Exchange 服务器。
- 您可能希望编写基本消息并将其显示给用户,以便她可以在发送前添加附件或以交互方式编辑文本。
但如果只是关于自动发送电子邮件,正如我所说,我会推荐 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, SWT.NONE, "OVCtl.OVCtl");
// site.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
// Now get the outlook application
OleClientSite site2 = new OleClientSite(frame, SWT.NONE, "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, String command, int value) {
return auto.invoke(property(auto, command),
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, String name,
int value) {
return auto.setProperty(property(auto, name), new Variant(value));
}
private static int property(OleAutomation auto, String name) {
return auto.getIDsOfNames(new String[] { name })[0];
}
}
我注释掉了最后的附件部分以及第一个 OLE 命令,因为对我来说,没有它它也可以工作。使用它不会造成任何损害,也许你需要它。试试看吧。
我注释掉标题“From”行的原因是它没有效果。要更改发件人,您可能需要另一个代码 sn-p 来切换 Outlook 配置文件或在配置文件中切换多个预配置的发件人。默认情况下,它只会使用您的默认配置文件。
如果有帮助,请告诉我。