从Outlook桌面应用程序打开邮件时,背景图像未显示

时间:2018-10-11 04:20:14

标签: c# outlook

在我的项目中,我有一个电子邮件html模板。我需要将该电子邮件发送给某些用户。在发送电子邮件之前,我需要嵌入一张背景图片。我将该图像作为base64String存储在数据库中。我使用了下面的代码,当用户使用Web客户端打开电子邮件,但是当用户使用Outlook桌面应用程序打开电子邮件时,背景图片没有显示。

public void SendSMTPEmail(Employee employee, EmailTemplate email)
    {
        string supervisorEmail = employee.SupervisorEmail;
        string employeeEmail = employee.EmployeeEmail;
        string subject = string.Format("Welcome {0}", employee.EmployeeFirstName);

        string mailBody = email.TemplateBaseHTMLString;
        AlternateView htmlView = AlternateView.CreateAlternateViewFromString(mailBody, null, "text/html");
        var imageDataString = email.ImageBase64String;
        var baseIndex = imageDataString.IndexOf("base64,", StringComparison.OrdinalIgnoreCase);
        if (baseIndex > -1)
        {
            imageDataString = imageDataString.Substring(baseIndex + 7);
        }
        var imageData = Convert.FromBase64String(imageDataString);
        using (var imageStream = new MemoryStream(imageData))
        {
            //LinkedResource theEmailImage = new LinkedResource(imageStream, email.ImageMimeType);
            LinkedResource theEmailImage = new LinkedResource(imageStream, new ContentType { MediaType = email.ImageMimeType });
            theEmailImage.ContentId = "myImageID";
            htmlView.LinkedResources.Add(theEmailImage);


            TriggerMail(supervisorEmail, employeeEmail, subject, htmlView, imageStream, email.ImageMimeType);
        }
        //Console.WriteLine("Email send successfully");
    }

private static void TriggerMail(string supervisorEmail, string employeeEmail, string subject, AlternateView altView = null, Stream imageStream = null, string mimeType = null, string mailBody = null)
    {
        bool isValidEmployeeEmail = IsValidEmail(employeeEmail);
        bool isValidSupervisorEmail = IsValidEmail(supervisorEmail);
        if (isValidEmployeeEmail || isValidSupervisorEmail)
        {
            try
            {
                string formEmailAddress = ConfigurationManager.AppSettings["FormEmailAddress"];
                string serverName = ConfigurationManager.AppSettings["SMTPServer"];
                string smtpPort = ConfigurationManager.AppSettings["SMTPPort"];
                SmtpClient client = new SmtpClient(serverName);
                int port = 0;
                if (int.TryParse(smtpPort, out port))
                {
                    client.Port = port;
                }

                MailAddress from = new MailAddress(formEmailAddress);
                MailAddress to = null;
                MailAddress cc = null;
                if (string.IsNullOrWhiteSpace(employeeEmail) || !isValidEmployeeEmail)
                {

                    to = new MailAddress(supervisorEmail);
                }
                else
                {
                    to = new MailAddress(employeeEmail);
                    if (!string.IsNullOrWhiteSpace(supervisorEmail) && isValidSupervisorEmail)
                    {
                        cc = new MailAddress(supervisorEmail);
                    }
                }

                using (MailMessage message = new MailMessage(from, to))
                {
                    if (cc != null)
                    {
                        message.CC.Add(cc);
                    }
                    if (altView != null)
                    {
                        message.AlternateViews.Add(altView);
                    }
                    else
                    {
                        message.Body = mailBody;
                    }

                    message.Subject = subject;
                    message.IsBodyHtml = true;
                    var userName = ConfigurationManager.AppSettings["EmailUserId"];
                    var pwd = ConfigurationManager.AppSettings["EmailPassword"];

                    if (!string.IsNullOrWhiteSpace(userName) && !string.IsNullOrWhiteSpace(pwd))
                    {

                        client.Credentials = new System.Net.NetworkCredential(userName, pwd);
                    }
                    client.EnableSsl = ConfigurationManager.AppSettings["IsEnableSSL"] == "Y";



                    client.Send(message);

                    //client.Send(mail);

                    message.Dispose();
                }
            }
            catch (Exception ex)
            {
               //log
            }
        }
    }

0 个答案:

没有答案