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
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
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.