none
Out to send an Outlook email with both HTML doc Word and default predefined Outlook signature RRS feed

  • Question

  • Hello all,

    In one hand, I have a word document that I saved in two format :
    - one in HTML
    - one in .docx

    In another hand, I know how to get the default signature of an account from Outlook.
    Here my situation :

    1) I can create a new email with the default Outlook signature and attach the Word file (the .docx) to this new email and send it. It works !

    Here is the code :

        using Outlook = Microsoft.Office.Interop.Outlook;

        private void SendViaOutlook(
            string              subject,
            string              recepient,
            string              otherFormatDocFile
        )
        {
            OutLook.Application mailApp = new OutLook.Application();
            Outlook.MailItem email = mailApp.CreateItem(Outlook.OlItemType.olMailItem);

            email.GetInspector; // this add the default signature to the new email
            email.Subject  = subject;
            email.To       = recepient;

            // Add the Word document in attachment to the email :
            email.Attachments.Add(otherFormatDocFile, Outlook.OlAttachmentType.olByValue, 1);
            email.Send();
        }

    It works prefectly.


    2) I can create a new email with the content of the Word file (that I load in HTML from the .htm file to HTMLBody property of the new email) and send it. In this email, the default Outlook signature is not present.
    It is almost the same code as the previous code :

        using Outlook = Microsoft.Office.Interop.Outlook;

        private void SendViaOutlook(
            string              subject,
            string              recepient,
            string              htmlDocFile
        )
        {
            OutLook.Application mailApp = new OutLook.Application();
            Outlook.MailItem email = mailApp.CreateItem(Outlook.OlItemType.olMailItem);

            email.Subject  = subject;
            email.To       = recepient;


            email.HTMLBody   = File.ReadAllText(htmlDocFile, Encoding.GetEncoding(1256));
            email.InternetCodepage = 1256;

            email.Send();
        }

    It also works prefectly.


    Now my question : how can I have the HTML content of the Word document in the body of the email AND the default Outlook signature ?
    When I debug the two codes, I can see that the attribut HTMLBody contains an HTML structure, and if I call "email.GetInspector" in my second code, the HTMLBody is erased by the signature.
    In the second code, I tried this :

            string  body = File.ReadAllText(htmlDocFile, Encoding.GetEncoding(1256));
            email.HTMLBody   = body + email.GetInspector;

    It doesnt work !
    In fact, the signature and the content of the document are both HTML and we cant concatenate two HTML structures together.

    Help well be great !
    Thanks


    • Edited by 2nids44 Sunday, September 16, 2018 3:03 PM
    Sunday, September 16, 2018 2:59 PM

All replies

  • Hi 2nids44,

    According to your description, you can use the following code:

    static void Main(string[] args)
            {
                {
                    Outlook.Application mailApp = new Outlook.Application();
                    Outlook.MailItem email = mailApp.CreateItem(Outlook.OlItemType.olMailItem);
                    email.Subject = Subject;
                    email.To = "";
                    string text = File.ReadAllText(path, Encoding.GetEncoding(1256));
                    email.HTMLBody = text;
                    email.HTMLBody += "\n\n" + ReadSignature();
                    email.Display();
                }
    
            }
            private static 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;
            }


    This is the result of my running:


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.



    Monday, September 17, 2018 7:33 AM
  • Thanks for your asking. Did you resolve your issue ? If the post helps you, you can mark it as answer and close the post. Thank you for understanding. If you have any question, or update, please feel free to let us know. I wish you a happy life!

    Alina



    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Friday, September 21, 2018 2:46 AM