Add the default outlook signature in the email generated

前端 未结 11 456
甜味超标
甜味超标 2020-12-08 07:11

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

相关标签:
11条回答
  • 2020-12-08 08:06

    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();
    
    0 讨论(0)
  • 2020-12-08 08:07

    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.

    0 讨论(0)
  • 2020-12-08 08:09

    For some reason libraries are made a bit different depending on language installed. Also a signature can hold a logo-image, wich I do not know why, but it is made in 2 files in 2 different sizes.

    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/");
                    }
                }
            }
            else
            {
                appDataDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Microsoft\\Signaturer";
                signature = string.Empty;
                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/");
                        }
                    }
                }
            }
            if (signature.Contains("img"))
            {
                int position = signature.LastIndexOf("img");
                int position1 = signature.IndexOf("src", position);
                position1 = position1 + 5;
                position = signature.IndexOf("\"", position1);
                billede1 = appDataDir.ToString() + "\\" + signature.Substring(position1, position - position1);
                position = billede1.IndexOf("/");
                billede1 = billede1.Remove(position, 1);
                billede1 = billede1.Insert(position, "\\");
    
                billede1 = System.Web.HttpUtility.UrlDecode(billede1);
    
                position = signature.LastIndexOf("imagedata");
                position1 = signature.IndexOf("src", position);
                position1 = position1 + 5;
                position = signature.IndexOf("\"", position1);
                billede2 = appDataDir.ToString() + "\\" + signature.Substring(position1, position - position1);
                position = billede2.IndexOf("/");
                billede2 = billede2.Remove(position, 1);
                billede2 = billede2.Insert(position, "\\");
    
                billede2 = System.Web.HttpUtility.UrlDecode(billede2);
            }
            return signature;
        }
    

    Getting and inserting the signature: Global variables:

        string billede1 = string.Empty;    // holding image1
        string billede2 = string.Empty;    // holding image2
    
                            string signature = ReadSignature();
                            if (signature.Contains("img"))
                            {
                                int position = signature.LastIndexOf("img");
                                int position1 = signature.IndexOf("src", position);
                                position1 = position1 + 5;
                                position = signature.IndexOf("\"", position1);
    
                                //CONTENT-ID
                                const string SchemaPR_ATTACH_CONTENT_ID = @"http://schemas.microsoft.com/mapi/proptag/0x3712001E";
                                string contentID = Guid.NewGuid().ToString();
    
                                //Attach image
                                mailItem.Attachments.Add(@billede1, Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, mailItem.Body.Length, Type.Missing);
                                mailItem.Attachments[mailItem.Attachments.Count].PropertyAccessor.SetProperty(SchemaPR_ATTACH_CONTENT_ID, contentID);
    
                                //Create and add banner
                                string banner = string.Format(@"cid:{0}", contentID);
                                signature = signature.Remove(position1, position - position1);
                                signature = signature.Insert(position1, banner);
    
                                position = signature.LastIndexOf("imagedata");
                                position1 = signature.IndexOf("src", position);
                                position1 = position1 + 5;
                                position = signature.IndexOf("\"", position1);
    
                                //CONTENT-ID
                               // const string SchemaPR_ATTACH_CONTENT_ID = @"http://schemas.microsoft.com/mapi/proptag/0x3712001E";
                                contentID = Guid.NewGuid().ToString();
    
                                //Attach image
                                mailItem.Attachments.Add(@billede2, Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, mailItem.Body.Length, Type.Missing);
                                mailItem.Attachments[mailItem.Attachments.Count].PropertyAccessor.SetProperty(SchemaPR_ATTACH_CONTENT_ID, contentID);
    
                                //Create and add banner
                                banner = string.Format(@"cid:{0}", contentID);
                                signature = signature.Remove(position1, position - position1);
                                signature = signature.Insert(position1, banner);
                            }
                            mailItem.HTMLBody = mailItem.Body + signature;
    

    The stringhandling can be don smarter, but this Works and gave me my sinature God luck.

    0 讨论(0)
  • 2020-12-08 08:11

    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.

    0 讨论(0)
  • 2020-12-08 08:13

    To get/set the user's default signature you can use the windows registry. Example for Outlook 2010: HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Common\MailSettings Name: NewSignature Datatype: String Value: (name of signature file without ending)

    0 讨论(0)
提交回复
热议问题