I have a desktop application developed in Java, I am creating an email with HTML body then saving it as "email.msg", and immediately open that email file in Outlook application using JACOB.

Here is the code:

//initiate the email
    private boolean sendEmailViaOutlook(Data data) {
            boolean emailSent;
            OutlookEmail jacobOutlook = new OutlookEmail();
            ActiveXComponent axOutlook = new ActiveXComponent("Outlook.Application");
            try {
                jacobOutlook.oOutlook = axOutlook.getObject();
                Dispatch oNameSpace = axOutlook.getProperty("Session").toDispatch();
                String emailBody = this.generateHTMLBody(data);
                String emailSubject = "subject line";
                String recipientTo = customTo;
                String recipientCC = customCc;
                String[] attachments = new String[]{"D:\\temp.txt"};
                jacobOutlook.createEmail(emailSubject, recipientTo, recipientCC, null, emailBody, attachments);

            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                emailSent = true;
            }
            return emailSent;
        }



//create HTML email body
 private String generateHTMLBody(AlarmDTO selectedAlarm, SiteDTO siteDTO,AlarmStatusDto statusDto, SeverityDto severityDto) {

            System.out.println("Site Province: "+siteDTO);

            return "<HTML><BODY><table border='1' style='border:solid 1px gray;width: 20%;'>" +
                    "<tr style='background-color: #0036A2; color:white;'>" +
                    "<th style='width: 50%;'>Site Information</th>" +
                    "<th style='width: 50%;'>Description</th>" +
                    "</tr>" +
                    "<tr>" +
                    "<td style='background-color: #74BCF8;font-weight:semi-bold; color:black;'>TT No</td>" +
                    "<td>" + data.getTtNo() + "</td>" +
                    "</tr>" +
                    "</table></BODY></HTML>";

        }



//create actual email in .msg format, save it and open it in outlook        
 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);
            Dispatch.put(mail, "HTMLBody", body);
            Dispatch.call(mail, "SaveAs","D:\\JacobEmail.msg");
            Dispatch.call(mail, "Display");
        }

So far, it works fine.

The question is that I want to create email threads using the same approach, and append new email to the conversation based on the certain criterias.

How to achieve that?

 

Last edit: Sayed Hussainullah Sadat 2024-05-15