我在Tomcat上运行了一个Java应用程序,它需要在连接客户端的计算机上生成一封发送给Outlook的电子邮件。目前,我的代码始终将电子邮件定向到运行tomcat的服务器上的Outlook,而不是连接客户端。我已经附加了我用来连接到Outlook下面的代码。任何帮助实现这一点将不胜感激。
package org.outlook.emails;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
public class EmailAPI
{
private ActiveXComponent ol;
private Dispatch outlook;
private Object mapi[] = new Object[1];
private Object email[] = new Object[1];
public EmailAPI()
{
mapi[0] = "MAPI";
email[0] = 0;
ol = new ActiveXComponent("Outlook.Application");
outlook = ol.getObject();
Dispatch.call(outlook,"GetNamespace",mapi).toDispatch();
}
public void createEmail(String receiver,String cc,String subject, String body, String attachments[])
{
Dispatch mail = Dispatch.call(outlook,"CreateItem",email).toDispatch();
Dispatch inspector = Dispatch.get(mail, "GetInspector").getDispatch();
Dispatch recipients = Dispatch.call(mail, "Recipients").getDispatch();
Dispatch.call(recipients, "Add" , receiver);
Dispatch.put(mail, "CC",cc);
Dispatch.put(mail, "Subject", subject);
Dispatch.put(mail, "Body", body);
if(attachments.length>0)
{
Dispatch attachs = Dispatch.get(mail, "Attachments").toDispatch();
for(Object attachment : attachments)
{
Dispatch.call(attachs, "Add", attachment);
}
}
Dispatch.call(mail, "Send");
}
}
谢谢。
地中海
答案 0 :(得分:0)
由于您的代码在服务器上运行,当然它会触发服务器上的任何操作(例如,如果存在,则启动outlook)。
您必须将代码传输到客户端并在那里执行。这可以使用java applet但最近已被删除/禁用。另一种可能性是java web start技术可以为你做到这一点。但是,也支持web启动will be removed in future版本的java。所以现在我们对你的场景知之甚少,但我会建议你再做一些评估/分析。根据您的要求,有几种可能性,只能猜出最适合您的选择。一些选择:
window.location.href = "mailto:user@example.com?subject=Subject&body=message%20goes%20here";
或某些仅限IE的解决方案here