从java程序打开Outlook邮件并将文件附加到目录中的邮件

时间:2011-05-18 13:50:06

标签: java

我需要在我的Java应用程序中实现电子邮件功能,这将打开microsoft outlook并从我的目录中附加文件。有没有实现相同的?

5 个答案:

答案 0 :(得分:10)

根据these docs您需要的命令是

"path/to/Outlook.exe /c ipm.note /a \"path/to/attachment\""

汇总并通过ProcessBuilder

运行

(或者听听MarcoS,他给出了一个非常好的例子,说明为什么有时候最好不要回答问题: - )

答案 1 :(得分:6)

您可以使用desktop类打开系统的电子邮件客户端。

Desktop.getDesktop().mail( new URI( "mailto:address@somewhere.com" ) )

答案 2 :(得分:5)

如果您想在Java中实现电子邮件功能,请考虑JavaMail。此外,如果您的应用程序具有电子邮件功能,则您无需打开另一个电子邮件客户端(例如outlook)。

答案 3 :(得分:3)

以下是您想要的确切命令: -

new ProcessBuilder("C:\\Program Files\\Microsoft Office\\Office14\\OUTLOOK.exe","/a","C:\\Desktop\\stackoverflow.txt").start();

第一个参数 - Outlook的路径。

Second Argument- Outlook附件命令。

第三个论点 - 附件路径

答案 4 :(得分:2)

我已经能够使用HTML电子邮件打开MS Outlook 2007。我使用SWT OLE API完成了这个。这是关于Vogela的教程:http://www.vogella.com/articles/EclipseMicrosoftIntegration/article.html

它在教程中说它也适用于非RCP Java。

public void sendEMail()
{

    OleFrame frame = new OleFrame(getShell(), 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", subject);
    setProperty(mail, "HtmlBody", content);

    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" */);

}