Add the default outlook signature in the email generated

前端 未结 11 454
甜味超标
甜味超标 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: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.

提交回复
热议问题