0

I have a requirement where I need to fetch data from SQL DB and view/download that content into Microsoft Outlook. DB Table contains various columns like Id, Content, Subject, ToAddress, and other regular information. Content column in this table is in HTML format.

I'm using Angular 6 and Wep API in my application.

I tried multiple approaches but none of them solved my problem:

Approach 1:

                Outlook.Application outlookApp = new Outlook.Application();
                Outlook.MailItem mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
                mailItem.Subject = message.Subject;
                mailItem.To = message.ToEmailAddress;
                mailItem.HTMLBody = message.Content;
                mailItem.Importance = Outlook.OlImportance.olImportanceLow;
                mailItem.Display(true);

For this, email is opening on the server machine but not on the client's machine.

Approach 2:

mailto:stuff@things.com&subject=something&body=<doctype html><span style="color:red">Hi how are you</span>

I can not use this approach, because it is not handling HTML in the email body. I'm using Content data for the email body.

Sample data in Content column:

<style type="text/css">
    html {
        font-family: Arimo, sans-serif, Calibri (Body);
    }

    body {
        font-family: Arimo, sans-serif, Calibri (Body);
    }


    label {
        font-weight: normal !important;
    }

    p, div {
        font-family: Arimo, sans-serif, Calibri (Body);
    }

    h1, h2, h3, h4, h5, h6, .h1, .h2, .h3, .h4, .h5, .h6 {
        font-family: Arimo, sans-serif, Calibri (Body);
    }

    footer {
        font-family: Arimo, sans-serif, Calibri (Body);
    }
</style>

<html>
<body>
    <p>Hello</p>
    <p>Thank you for using the application</p>
    <p>
        https://www.google.com
    </p>
    <p>
        This is another paragraph 
        <br /><br />
        Thank you,
        <br />
        Your DEV Team
    </p>
</body>
<footer>
    <span style="font-size:x-small;font-style: italic;color:#2b568f">
        Note: Please do not reply to this email.
    </span>
</footer>
</html>

Thanks in advance

ivj
  • 39
  • 6
  • You can not open outlook on the client machine from web API c# code. You need to do this from angular which is running on client browser. You first get data from web API to angular and then open mail client from Angular. https://stackoverflow.com/questions/36553390/open-an-outlook-within-angular-controller – Chetan Jun 23 '20 at 00:20
  • @ChetanRanpariya Yes, I tried that approach also. The problem is data in my DB is stored in HTML format and when I assign data to the body it is treating as plain text and displaying HTML code in email body. – ivj Jun 23 '20 at 00:26
  • Setting html body for mailto is not possible. You can read here https://stackoverflow.com/questions/5620324/mailto-link-with-html-body. You want users email client open with email body so that user can just click Send button to send the email? – Chetan Jun 23 '20 at 01:26
  • Yes, Users should be able to send email if they want to. – ivj Jun 23 '20 at 13:41
  • hmm... as I said earlier, setting html body for mailto is not possible. You might want to give feature to display content in html page and allow user to copy it so that user can copy and paste it in their email client and send email manually... – Chetan Jun 23 '20 at 15:07

1 Answers1

0

Controller side code

            string[] headerArray = new string[]
            {
                    "To: " + message.ToEmailAddress,
                    "Subject: "+ message.Subject,
                    "From: " + message.ToEmailAddress,
                    "Reply-To: " + message.ToEmailAddress,
                    "Sender: " + message.ToEmailAddress,
                    "MIME-Version: 1.0",
                    "Content-Type: text/html; charset=ISO-8859-1",
                    "Date: "+ message.Modification.ModifiedDateTime
            };

            string headers = String.Join("\r\n", headerArray);
            string content = message.Content;
            string file = $"{headers}{content}";

            return File(System.Text.Encoding.ASCII.GetBytes(file), "text/html", 
            "MyTestEmail.eml");

I called above code in controller using anchor tag which automatically downloads the file on to client's machine and importantly it is also handling Content of type HTML

ivj
  • 39
  • 6