0

HI I'm trying to create Outlook e-mail from Java I'm working with Windows 10 and Outlook 365 desktop version I'm able to do it with AposeEmail https://docs.aspose.com/email/#asposeemail-for-java and with jotlmsg https://github.com/ctabin/jotlmsg But there are limitations. Apose E-mial is not free. Only evaluation version is free. Jotlmsg doesn't support html e-mails. There’s also another possibility to use methods from dynamic libraries. There is library Jacob for this Jacob Outlook example

However I don't know how to find way to do it in Jacob. Examples which I found on Stackoverflow or in other places doesn't work.

I have following VBA code which works. Any idea how to do the same / similar thing in Jacob? Alternative solution. Do you know free JAVA library which supports .msg e-mail with HTML format?

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

I tried AposeE-mail and Jotlmsg but they don't satisfy fully my needs.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
PawDob
  • 11
  • 5

1 Answers1

1

I have found example Jacob Outlook Sample
and modified:

/**
 * JACOB Outlook sample contributed by
 * Christopher Brind <christopher.brind@morse.com>
 * Modified by PawDob https://stackoverflow.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");
    }

}

And finally it is working
Sometimes you have ask to find solution yourself :-)

PawDob
  • 11
  • 5