【问题标题】:to open outlook mail from java program and to attach file to the mail from directory从 java 程序打开 Outlook 邮件并将文件附加到目录中的邮件
【发布时间】:2020-10-08 12:57:50
【问题描述】:

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

【问题讨论】:

    标签: java


    【解决方案1】:

    根据these docs你需要的命令是

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

    组装它并通过ProcessBuilder运行它

    (或者听听 MarcoS 的例子,他举了一个很好的例子来说明为什么有时最好不要直接回答问题 :-))

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 对于服务器端应用程序或桌面上的某些特定用例是可以的,但一般来说,您需要 SMTP 服务器来发送电子邮件。并且没有多少用户(统计上没有人)在他们的本地计算机上运行它。您可以使用远程 SMTP 服务器,但即使您构建了所有支持以绕过防火墙和其他障碍,您也不想这样做,因为垃圾邮件发送者会将其当作简单的午餐。
      • @serg.nechaev 在任何情况下,您都需要一个邮件服务器(SMTP、Exchange、Domino 等)来发送电子邮件 :)
      【解决方案3】:

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

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

      【讨论】:

      • 是的,但没有附件
      【解决方案4】:

      我已经能够使用 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" */);
      
      }
      

      【讨论】:

      • 除了链接之外,当您提供文章摘录时,这种答案对社区更有用。在未来的某个时候,该链接可能无法正常工作 - 找到此答案的人会再次被卡住。
      【解决方案5】:

      这是您想要的确切命令:-

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

      第一个参数-Outlook 的路径。

      第二个参数 - Outlook 附件命令。

      第三个参数 - 附件路径

      【讨论】:

        猜你喜欢
        • 2015-04-12
        • 2016-05-15
        • 2019-03-18
        • 1970-01-01
        • 2016-08-29
        • 2020-11-21
        • 2023-03-11
        • 1970-01-01
        • 2021-10-28
        相关资源
        最近更新 更多