Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Outlook Interop, Mail Formatting

I have an application written in C# that uses Outlook Interop to open a new mail message pre-filled with details the user can edit before manually sending it.

var newMail = (Outlook.MailItem)outlookApplication.CreateItem(
    Outlook.OlItemType.olMailItem);
newMail.To = "[email protected]";
newMail.Subject = "Example";
newMail.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
newMail.HTMLBody = "<p>Dear Example,</p><p>Example example.</p>";
newMail.Display(false);

When the same user creates a new message manually the font is set to Calibri or whichever font the user has set as their default. The problem is that the text in the automatic email appears in Times New Roman font which we do not want.

If I view source of one of the delivered emails I can see that Outlook has explicitly set the font in the email source:

// Automated
p.MsoNormal, li.MsoNormal, div.MsoNormal
{
    margin:0cm;
    margin-bottom:.0001pt;
    font-size:12.0pt;
    font-family:"Times New Roman";
}

// Manual
p.MsoNormal, li.MsoNormal, div.MsoNormal
{
    margin:0cm;
    margin-bottom:.0001pt;
    font-size:11.0pt;
    font-family:"Calibri","sans-serif";
}

Why are the formats different and how can I get the automated email to use the users default settings? I am using version 11 of the interop assemblies as there is a mix of Outlook 2003 and 2007 installed.

like image 996
Generic Error Avatar asked Jul 01 '09 01:07

Generic Error


2 Answers

It's totally frustrating.

It doesn't help that when you Google the problem there are countless answers telling you to simply style the text in CSS. Yeeessss, fine, if you're generating the full email and can style/control the entire text. But in your case (and ours) the intention is to launch the email with some initial text and the end user adds his own text. It's his additional text which is unfailingly rendered in Times New Roman.

The solution we found is to approach the problem from another direction. And that is to fix the base/underlying default in Outlook to be our selected font instead of Times New Roman.

That can be done by:

  1. Open Word (yes, not Outlook)
  2. Go to Options -> Advanced -> Web Options
  3. Change the default font in the Fonts tab

Video here:
http://www.youtube.com/watch?v=IC2RvfoMFz8

I appreciate this doesn't help if you need to control or vary the font programmatically. But for those working with customers who simply want the base email font to not be Times New Roman whenever emails are generated from code, this may help.

like image 130
hawbsl Avatar answered Nov 01 '22 00:11

hawbsl


Since it is an HTML email, you can easily embed whatever styling you want into the actual HTML body. I suspect that is what Outlook is doing when you create a message from the Outlook GUI.

I don't actually know how to get the user settings. I looked through the Outlook API (it is a strange beast), but didn't see anything that would provide access to the default message properties.

like image 22
Robert Harvey Avatar answered Nov 01 '22 00:11

Robert Harvey