在 Java Jacob 库中创建和保存 Outlook 电子邮件

Create and Save Outlook e-mail in Java Jacob library

嗨,我正在尝试从 Java 创建 Outlook 电子邮件 我正在使用 Windows 10 和 Outlook 365 桌面版 我可以用 AposeEmail 做到这一点 https://docs.aspose.com/email/#asposeemail-for-java 与 短消息 https://github.com/ctabin/jotlmsg 但也有局限性。 Apose 电子邮件不是免费的。只有评估版是免费的。 Jotlmsg 不支持 html 电子邮件。 还有另一种可能使用动态库中的方法。这个有图书馆雅各布 Jacob Outlook example

但是我不知道如何在 Jacob 中找到方法。我在 Whosebug 或其他地方找到的示例不起作用。

我有以下 VBA 有效的代码。 知道如何在 Jacob 中做相同/相似的事情吗? 替代解决方案。 您知道免费的 JAVA 库,它支持 HTML 格式的 .msg 电子邮件吗?

Public Sub CreateEmail()

Dim objMsg As MailItem

Set objMsg = Application.CreateItem(olMailItem)

 With objMsg
  .To = Alias@domain.com
  .CC = Alias2@domain.com
  .BCC = Alias3@domain.com
  .Subject = "This is the subject"
  .Categories = "Test"
  .VotingOptions = "Yes;No;Maybe;"
  .BodyFormat = olFormatPlain ' send plain text message
  .Importance = olImportanceHigh
  .Sensitivity = olConfidential
  .Attachments.Add ("path-to-file.docx")

' Calculate a date using DateAdd or enter an explicit date
 .ExpiryTime = DateAdd("m", 6, Now) '6 months from now
  .DeferredDeliveryTime = #8/1/2012 6:00:00 PM#
  
  .Display
End With

Set objMsg = Nothing
End Sub

我尝试了 AposeE-mail 和 Jotlmsg,但它们不能完全满足我的需求。

我找到例子了 Jacob Outlook Sample
并修改:

/**
 * JACOB Outlook sample contributed by
 * Christopher Brind <christopher.brind@morse.com>
 * Modified by PawDob https://whosebug.com/users/16168586/pawdob
 */

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;

/**
 * sample class to show simple outlook manipulation
 */
public class JacobOutlookEmail {

    private ActiveXComponent ol;
    private Dispatch oOutlook;
    private Object email[] = new Object[1];


    /**
     * standard run loop
     *
     * @param asArgs command line arguments
     * @throws Exception
     */
    public static void main(String asArgs[]) throws Exception {
        System.out.println("Outlook: IN");

        JacobOutlookEmail jacobOutlook = new JacobOutlookEmail();

        ActiveXComponent axOutlook = new ActiveXComponent("Outlook.Application");
        try {
            System.out.println("version=" + axOutlook.getProperty("Version"));

            jacobOutlook.oOutlook = axOutlook.getObject();
            System.out.println("version=" + Dispatch.get(jacobOutlook.oOutlook, "Version"));

            Dispatch oNameSpace = axOutlook.getProperty("Session").toDispatch();
            System.out.println("oNameSpace=" + oNameSpace);

            String emailBody = "<HTML><BODY><p><b>Bold text</b></p>" +
                    "<p><i>Italic text</i></p>" +
                    "<p>Normal text</p>" +
                    "</BODY></HTML>";
            String emailSubject = "Email demo using Jacob";

            String recipientTo = "Alias@domain.com";
            String recipientCC = "Alias2@domain.com";
            String recipientBCC = "Alias3@domain.com";

            String[] attachments = new String[]{"D:\temp.txt"};

            jacobOutlook.createEmail(emailSubject, recipientTo, recipientCC, recipientBCC, emailBody, attachments);

        } finally {
//            Uncomment if you want close outlook after job is done
//            axOutlook.invoke("Quit", new Variant[]{});
        }
    }


    public void createEmail(String subject, String recipientTo, String recipientCC, String recipientBCC, String body, String[] attachments) {
        Dispatch mail = Dispatch.call(oOutlook, "CreateItem", email).toDispatch();
        Dispatch.put(mail, "Subject", subject);
        Dispatch.put(mail, "To", recipientTo);
        Dispatch.put(mail, "CC", recipientCC);
        Dispatch.put(mail, "BCC", recipientBCC);

//        Use if sample text is used
//        Dispatch.put(mail, "Body", body);


        Dispatch.put(mail, "HTMLBody", body);



        if (attachments.length > 0) {
            Dispatch attachs = Dispatch.get(mail, "Attachments").toDispatch();

            for (Object attachment : attachments) {
                Dispatch.call(attachs, "Add", attachment);
            }
        }
//        Save on D drive
        Dispatch.call(mail, "SaveAs","D:\JacobEmail.msg");
//        Display in outlook
        Dispatch.call(mail, "Display");
    }

}

终于可以用了
有时你会要求自己寻找解决方案:-)