0

I am trying to set the default Outlook signature for both new messages and replies using C#.

Everything is working as expected for new messages (meaning that when I create a new message in Outlook, I can see the default signature), but nothing shows up when I reply to messages (or forward them).

This seems weird for two reasons:

  • In Outlook, I can see in the signature manager that that the default signature for replies is set properly. If I select another signature and select "Auto_Signature" again, everything starts working as expected, so it's not a problem with the signature itself.
  • The new messages signature is set the same way as the replies signature and that one works.

Here is the code that I am using to set the default signatures:

using Microsoft.Office.Interop.Word;

private void ApplyDefaultSignatureExecuted(object? obj)
{
    var signaturesFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Microsoft\\Signatures");
    var fileName = Path.Combine(signaturesFolder, "Auto_Signature.htm");
    
    using (var writer = new StreamWriter(fileName))
    {
        writer.WriteLine("<html><body>");
        writer.WriteLine("<div><p><i></span></b></p></div>");
        writer.WriteLine(HttpUtility.HtmlEncode("Signature that was generated via C#"));
        writer.WriteLine("</i></p><p><b><span style='font-size:24.0pt'>");
        writer.WriteLine(HttpUtility.HtmlEncode("Name goes here"));
        writer.WriteLine("</span></b></p></div>");
        writer.WriteLine("</body></html>");
    }
    
    Application oWord = new Application();
    EmailOptions oOptions = oWord.Application.EmailOptions;
    oOptions.EmailSignature.NewMessageSignature = "Auto_Signature";
    // This is the line that should be setting the auto-reply signature
    oOptions.EmailSignature.ReplyMessageSignature = "Auto_Signature";
    
    //Release Word, not sure if this has any effect as I get the same results with or without those lines
    System.Runtime.InteropServices.Marshal.ReleaseComObject(oOptions);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord);
}

And here are some screenshots to go along with my explanation:

Reply - Signature does not appear automatically Reply - Signature does not appear automatically

Signature manager - Default signatures are set properly Signature manager - Default signatures are set properly by C#, and changing to another one and back makes everything work as it should

New message - Signature appears automatically New message - Signature appears automatically

According to everything I have read, this should be working since setting the signature for new message and replies is the same code, but it's not. I am using Office 365 and I don't mind switching libraries if that solves my issues.

Jumbala
  • 4,764
  • 9
  • 45
  • 65
  • See https://stackoverflow.com/questions/75001702/choose-reply-signature – Dmitry Streblechenko Feb 14 '23 at 18:45
  • Or https://stackoverflow.com/a/32418077/332059 – Dmitry Streblechenko Feb 14 '23 at 18:47
  • It didn't seem relevant at first since the other question is in VBA and I don't have access to the same methods, but I got it working thanks to something in the accepted answer: "Account.ReplySignature is an object! So, it must be set, it cannot be a string (Signature.name)...". I changed oOptions.EmailSignature.ReplyMessageSignature = "Auto_Signature"; to oOptions.EmailSignature.ReplyMessageSignature = oOptions.EmailSignature.NewMessageSignature; and it's now working. Thanks! – Jumbala Feb 14 '23 at 18:55

1 Answers1

0

I got it working by simply changing the line

oOptions.EmailSignature.ReplyMessageSignature = "Auto_Signature";

To

oOptions.EmailSignature.ReplyMessageSignature = oOptions.EmailSignature.NewMessageSignature;

Everything is now working exactly the same for replies and forwards as it does for new messages.

Jumbala
  • 4,764
  • 9
  • 45
  • 65