Java 如何在 Outlook 中创建电子邮件并使其对用户可见
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18057759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create an E-Mail in Outlook and make it visible for the User
提问by Loki
I want to create an E-Mail with a Java Application using Outlook and the OLE Client.
我想使用 Outlook 和 OLE 客户端创建带有 Java 应用程序的电子邮件。
I searched for examples and found quite a few. They all start the same way:
我搜索了一些例子,发现了很多。他们都以同样的方式开始:
Create the Display, the Shell, the OLE Frame and the OLE Client Site.
创建显示、外壳、OLE 框架和 OLE 客户端站点。
But I get an error with these few steps:
但是我在这几个步骤中遇到错误:
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");
I get the following Error:
我收到以下错误:
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)
I don't know OLE and I'm not sure what I am doing wrong. Are there some dependencies I'm missing? Does somebody know what this error could be? I googled for the Error code but didn't find anything.
我不知道 OLE,我不确定我做错了什么。是否有一些我遗漏的依赖项?有人知道这个错误可能是什么吗?我用谷歌搜索错误代码,但没有找到任何东西。
EDIT
编辑
Well if nobody knows why OLE doesn't work for me I've got another question. Is it possible, or is there a Library, to create an Outlook E-Mail and set it up (subject, body etc) but not send it but make it visible for the User to change things?
好吧,如果没有人知道为什么 OLE 对我不起作用,我还有另一个问题。是否有可能,或者是否有一个库,来创建一个 Outlook 电子邮件并设置它(主题、正文等)但不发送它但让它对用户可见以进行更改?
EDIT 2
编辑 2
The x86 and x64 jar files didn't work out, same error. Also I got the newest versions of SWT for x86 and x64. OS is x64 and java, too, so I can't use the x86 SWT libraries. With the x64 the error above occurs. The Outlook version is 15 (Outlook 2013).
x86 和 x64 jar 文件没有解决,同样的错误。我还获得了适用于 x86 和 x64 的最新版本的 SWT。操作系统也是 x64 和 java,所以我不能使用 x86 SWT 库。使用 x64 时会发生上述错误。Outlook 版本为 15(Outlook 2013)。
Hopefully this helps?
希望这有帮助吗?
I got the creation of E-Mail to work via Processbuilder but only with the mailto: parameter. The problem here is the folllowing:
我通过 Processbuilder 创建了电子邮件,但只能使用 mailto: 参数。这里的问题如下:
- I want to track the status of the Process. I want to know when the E-Mail is closed/send whatsoever.
- I want to insert a picture (BufferedImage) out of the Clipboard into the Body, which is simply impossible with the mailto argument.
- 我想跟踪进程的状态。我想知道电子邮件何时关闭/发送。
- 我想将剪贴板中的图片 (BufferedImage) 插入到正文中,这对于 mailto 参数是完全不可能的。
回答by kriegaex
For me this works nicely according to a tutorial on vogella.com. I also tried your minimal code sample and got no error during OLE client creation. I used SWT 4.3, by the way.
对我来说,根据vogella.com上的教程,这很有效。我还尝试了您的最小代码示例,并且在 OLE 客户端创建过程中没有出现错误。顺便说一下,我使用了 SWT 4.3。
A little off-topic, but does it have to be Outlook? I mean, do you just want to automate e-mail sending - you could use JavaMailand do it headlessly, i.e. without automating an actual GUI client. The only reasons I can imagine for wishing to use Outlook or another e-mail client are:
有点跑题,但一定是 Outlook 吗?我的意思是,您是否只想自动化电子邮件发送 - 您可以使用JavaMail并且无头地进行,即无需自动化实际的 GUI 客户端。我可以想象希望使用 Outlook 或其他电子邮件客户端的唯一原因是:
- You need the sent message in your out-box for reference.
- Outlook is connected to an Exchange server which is configured to not accept SMTP connections as used by JavaMail.
- You might want to compose a basic message and show it to the user so she can add attachments or edit the text interactively before sending.
- 您需要发件箱中已发送的消息以供参考。
- Outlook 连接到 Exchange 服务器,该服务器配置为不接受 JavaMail 使用的 SMTP 连接。
- 您可能希望编写一条基本消息并将其显示给用户,以便她可以在发送之前添加附件或以交互方式编辑文本。
But if it is just about automating e-mail sending, as I said I would recommend JavaMail.
但如果它只是关于自动化电子邮件发送,正如我所说,我会推荐 JavaMail。
Update:I downloaded SWT from its home page, in my case the latest stable release 4.3 for Windows. In the ZIP archive the file you need is swt.jar.
更新:我从它的主页下载了 SWT ,在我的例子中是最新的 Windows稳定版 4.3。在 ZIP 存档中,您需要的文件是swt.jar。
My sample code looks like this and is working fine:
我的示例代码如下所示并且工作正常:
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", "[email protected]");
setProperty(mail, "To", "<John Doe> [email protected]");
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];
}
}
I commented out the attachments part at the end and also the first OLE command because for me it also works without it. It does not do any damage to use it though, maybe you need it. Just give it a try.
我注释掉了最后的附件部分以及第一个 OLE 命令,因为对我来说它也可以在没有它的情况下工作。使用它不会造成任何损害,也许您需要它。试试吧。
The reason why I commented out the header "From" line is that it has no effect. For changing the sender you probably need another code snippet to switch either the Outlook profile or within a profile switch several preconfigured senders. By default it will just use your default profile.
我注释掉标题“From”行的原因是它没有效果。要更改发件人,您可能需要另一个代码片段来切换 Outlook 配置文件或在配置文件中切换多个预配置的发件人。默认情况下,它只会使用您的默认配置文件。
Tell me if it helps.
告诉我是否有帮助。
回答by rodolfoprado
if you are using something with web, this can help you:
如果您在网络上使用某些东西,这可以帮助您:
<!DOCTYPE html>
<html>
<body>
<p>
This is an email link:
<a href="mailto:[email protected]?Subject=Hello%20again&body=your%20textBody%20here" target="_top">
Send Mail</a>
</p>
<p>
<b>Note:</b> Spaces between words should be replaced by %20 to ensure that the browser will display the text properly.
</p>
</body>
</html>
but in a application you can start a process mailto:
但在应用程序中,您可以启动一个过程 mailto:
like
喜欢
System.Diagnostics.Process.Start("mailto:[email protected]?Subject=Hello%20again&body=your%20textBody%20here")
it will work with all e-mail clients
它将适用于所有电子邮件客户端
回答by VikranthSanka
com,
com,
System.Diagnostics.Process.Start("mailto:[email protected]?Subject=Hello%20again&body=your%20textBody%20here")
with the above code Outlook mail is opened with predefined mailto, Subject and Body of the mail, could you please explain me how can we Add the address in CC also.
使用上述代码打开 Outlook 邮件时,预定义的 mailto、邮件主题和正文,能否请您解释一下我们如何在 CC 中添加地址。
回答by mrchidam
Your MS Outlook might be 32-bit (x86). So a 64-bit (x64) SWT cannot start the outlook. You need to use 32-bit SWT Jar File which will not run on 64-bit JVM. So you need to install 32-Bit JVM (JRE).
您的 MS Outlook 可能是 32 位 (x86)。因此 64 位 (x64) SWT 无法启动 Outlook。您需要使用无法在 64 位 JVM 上运行的 32 位 SWT Jar 文件。所以你需要安装 32 位 JVM (JRE)。
Even if you are running 64-bit Windows, you can still download and install the 32-bit (x86) JRE and run your application.
即使您运行的是 64 位 Windows,您仍然可以下载并安装 32 位 (x86) JRE 并运行您的应用程序。