Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add the default outlook signature in the email generated

I am using the Microsoft.Office.Interop.Outlook.Application to generate an email and display it on the screen before the user can send it. The application is a winform application coded in C# in the .NET Framework 3.5 SP1 and it is Microsoft Outlook 2003. I am using the following code:

public static void GenerateEmail(string emailTo, string ccTo, string subject, string body)     {         var objOutlook = new Application();         var mailItem = (MailItem)(objOutlook.CreateItem(OlItemType.olMailItem));                 mailItem.To = emailTo;                   mailItem.CC = ccTo;         mailItem.Subject = subject;         mailItem.HTMLBody = body;         mailItem.Display(mailItem);     } 

My question is:

How do i insert/add the default signature of the user who is using the application in the body of the email generated? Any help appreciated.

like image 858
reggie Avatar asked Jun 22 '11 15:06

reggie


2 Answers

There is a really quick easy way that hasn't been mentioned. See modified below:

public static void GenerateEmail(string emailTo, string ccTo, string subject, string body) {     var objOutlook = new Application();     var mailItem = (MailItem)(objOutlook.CreateItem(OlItemType.olMailItem));             mailItem.To = emailTo;               mailItem.CC = ccTo;     mailItem.Subject = subject;     mailItem.Display(mailItem);     mailItem.HTMLBody = body + mailItem.HTMLBody; } 

By editing the HTMLBody after you display the mailitem you allow for Outlook to do the work of adding the default signature and then essentially copy, edit, and append.

like image 67
Michael Sullivan Avatar answered Sep 28 '22 00:09

Michael Sullivan


Take a look at the link below. It explains where the signatures can be found in the file system as well as how to read them properly.

http://social.msdn.microsoft.com/Forums/en/vsto/thread/86ce09e2-9526-4b53-b5bb-968c2b8ba6d6

The thread only mentions Window XP and Windows Vista signature locations. I have confirmed that Outlooks signatures on Windows 7 live in the same place as Vista. I have also confirmed that the signature location is the same for Outlook 2003, 2007, and 2010.

Here's a code sample if you choose to go this route. Taken from this site.

private string ReadSignature() {     string appDataDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Microsoft\\Signatures";     string signature = string.Empty;     DirectoryInfo diInfo = new DirectoryInfo(appDataDir);      if(diInfo.Exists)     {         FileInfo[] fiSignature = diInfo.GetFiles("*.htm");          if (fiSignature.Length > 0)         {             StreamReader sr = new StreamReader(fiSignature[0].FullName, Encoding.Default);             signature = sr.ReadToEnd();              if (!string.IsNullOrEmpty(signature))             {                 string fileName = fiSignature[0].Name.Replace(fiSignature[0].Extension, string.Empty);                 signature = signature.Replace(fileName + "_files/", appDataDir + "/" + fileName + "_files/");             }         }     }         return signature; } 

Edit: See here to find the name of the default signature for Outlook 2013 or @japel's answer in this thread for 2010.

like image 41
James Hill Avatar answered Sep 28 '22 00:09

James Hill