Add the default outlook signature in the email generated

Viewed 53010

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.

11 Answers

For anyone looking for an answer after all those years.

In Outlook 2016 mailItem.HTMLBody already contains your default signature/footer.

In my case I replied to someone. If you want to add a message before just do as shown below. Simple.

MailItem itemObj = item as MailItem; //itemObj is the email I am replying to
var itemReply = itemObj.Reply();
itemReply.HTMLBody = "Your message" + itemReply.HTMLBody; //here happens the magic, your footer is already there in HTMLBody by default, just don't you delete it :)
itemReply.Send();

also I have dealt with this topic for several hours. Finally I stumbled across a very interesting Microsoft support case.

https://support.microsoft.com/de-de/help/4020759/text-formatting-may-be-lost-when-editing-the-htmlbody-property-of-an

The real problem is buried somewhere else: Microsoft Outlook uses Microsoft Word as the editor. Loss of formatting may occur if the HTML source is verified by the Word HTML module when the item is sent.

To fix the problem, simply load Word.Interopt and use Word as the editor.

Translated with www.DeepL.com/Translator

    public static void SendMail(string subject, string message, List<string> attachments, string recipients)
    {
        try
        {
            Outlook.Application application = new Outlook.Application();

            Outlook.MailItem mailItem = (Outlook.MailItem)application.CreateItem(Outlook.OlItemType.olMailItem);
            Word.Document  worDocument = mailItem.GetInspector.WordEditor as Word.Document;
            Word.Range wordRange = worDocument.Range(0, 0);
            wordRange.Text = message;

            foreach (string attachment in attachments ?? Enumerable.Empty<string>())
            {
                string displayName = GetFileName(attachment);
                int position = (int)mailItem.Body.Length + 1;
                int attachType = (int)Outlook.OlAttachmentType.olByValue;
                Outlook.Attachment attachmentItem = mailItem.Attachments.Add
                    (attachment, attachType, position, displayName);
            }

            mailItem.Subject = subject;

            Outlook.Recipients recipientsItems = (Outlook.Recipients)mailItem.Recipients;
            Outlook.Recipient recipientsItem = (Outlook.Recipient)recipientsItems.Add(recipients);
            recipientsItem.Resolve();
            mailItem.Display();

            recipientsItem = null;
            recipientsItems = null;
            mailItem = null;
            application = null;
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
    }

    private static string GetFileName(string fullpath)
    {
        string fileName = Path.GetFileNameWithoutExtension(fullpath);
        return fileName;
    }

Have fun with it. Thomas

Using GetInspector causes an exception when implementing an Interceptor using the Inspectors_NewInspector event. You will also find that the Signature has yet to be added to the MailItem when the Inspectors_NewInspector event is raised.

If you trigger the Item.Send() with your own code (e.g. your own button) you will have an Anchor tag <a> for both the "Signature" and "Content End". Note these tags are removed from the HTMLBody (in the translation from Word HTML to HTML) if you handle the Outlook ribbon [Send] button click event yourself (as is common for Add-ins).

My solution this is to handle the Item.Open event so that when the Interceptor/Inspectors_NewInspector is created/raised I can then add an Id attribute to the containing <p> tag to later use when sending. This attribute stays in the HTML even after sending.

This ensures that whenever the Send is called I can detect in my code the "Signature" or the "Content End" paragraphs.

Related