Java使用本地邮件客户端自动发送邮件 [英] Java send mail automatically with local mail client
问题描述
我想从Java程序中通过用户的本地邮件客户端自动发送邮件.
From within a java program I want to send a mail via the local mail client of the user automatically.
我使用以下代码打开客户端并填写必填字段,但是现在如何在没有任何用户交互的情况下自动发送它呢?
I use the following code to open up the client and fill in the required fields but how do I send it now automatically without any user interaction?
private void sendMail() throws MessagingException {
try {
Desktop.getDesktop().mail(new URI("mailto:abc@def.com?subject=someSubject&cc=aa@bb.cc,dd@dd.ds&bcc=x@y.zz&body=someBodyText"));
} catch (Exception e) {
e.printStackTrace();
}
}
基本上我想发送不离开公司网络的邮件.
Basically I want to send mails that don't leave the network of the company.
推荐答案
I found a way to deal with outlook at least, following this guide: Vogella, Eclipse-Microsoft Integration
基本上,我正在使用OleClientSite类来调用Outlook.然后,我使用oleAutomation类发送邮件.
Basically I'm using the OleClientSite class to invoke outlook. Then I'm using the oleAutomation class to send the mail.
代码段:
Shell shell = new Shell(Display.getDefault());
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, "To", "aav@gmail.com"); /*
* Empty but could also be
* predefined
*/
setProperty(mail, "Bcc", "test@gmail.com"); /*
* Empty but could also be
* predefined
*/
setProperty(mail, "BodyFormat", 2 /* HTML */);
setProperty(mail, "Subject", "Top News for you");
setProperty(mail, "HtmlBody",
"<html>Hello<p>, please find some infos here.</html>");
invoke(mail, "Send" /* or "Send" */);
这篇关于Java使用本地邮件客户端自动发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!