Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html not showing up in outlook mail

Tags:

html

c#

outlook

I have the following code that is sending emails to different recipients in a loop

public void SendMail2(string subject, string body, string emailAddress, string cc)
    {

        Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
        Microsoft.Office.Interop.Outlook.MailItem mailItem = app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
        mailItem.Subject = subject;
        mailItem.To = emailAddress;
        mailItem.CC = cc;
        mailItem.Body = body;
        mailItem.SentOnBehalfOfName = "name";
        mailItem.Display(false);
        mailItem.Send();
    }

However the html is just showing up as text with all the tags in the email, while it was perfect when i was using

        // Create the Outlook application.
        Outlook.Application oApp = new Outlook.Application();

        // Get the NameSpace and Logon information.
        Outlook.NameSpace oNS = oApp.GetNamespace("mapi");

        // Log on by using a dialog box to choose the profile.
        oNS.Logon(Missing.Value, Missing.Value, true, true); 

but I had to revert back to the first method so i can change the "From" address

Any ideas please?

like image 307
PrOjEkTeD Avatar asked Dec 24 '22 21:12

PrOjEkTeD


2 Answers

mailItem.Body = body;

That is because you use the Body property. Use the HTMLBody instead.

like image 64
Eugene Astafiev Avatar answered Jan 05 '23 18:01

Eugene Astafiev


Try using mailItem.HTMLBody = Body; instead of mailItem.Body = body;, and then add mailItem.BodyFormat = olFormatHTML;

like image 42
rory.ap Avatar answered Jan 05 '23 17:01

rory.ap