简体   繁体   English

从ASP.NET网站发送的电子邮件中未加载图片和CSS

[英]Images and css are not loaded in emails sent from asp.net website

In my asp.net website I have functionality to send HTML enabled emails. 在我的asp.net网站上,我具有发送启用HTML的电子邮件的功能。 These emails are sent using SMTP and these emails contain images and are also referring css files. 这些电子邮件是使用SMTP发送的,这些电子邮件包含图像,并且还引用了CSS文件。 The problem is that images in the received emails are not displaying when opened in gmail, yahoo and other domains. 问题是,在gmail,yahoo和其他域中打开时,收到的电子邮件中的图像无法显示。 The same problem is occurring for the css code/files that I have used in the HTML of email. 我在电子邮件HTML中使用的CSS代码/文件也发生了同样的问题。

I am using below SMTP code to send emails: 我正在使用下面的SMTP代码发送电子邮件:

        string _mailFrom = ConfigurationManager.AppSettings["mailfrom"].ToString();
        string _emailTo = ConfigurationManager.AppSettings["ScheduledServiceRecipientTO"].ToString();
        string _emailCC = ConfigurationManager.AppSettings["ScheduledServiceRecipientCC"].ToString();

        var _mailMessage = new System.Net.Mail.MailMessage();
        _mailMessage.To.Add(_emailTo);
        _mailMessage.CC.Add(new MailAddress(_emailCC));
        _mailMessage.From = new MailAddress(_mailFrom);
        _mailMessage.Subject = subject;
        _mailMessage.Body = pBody;
        _mailMessage.IsBodyHtml = true;

        try
        {
            var smtp = new System.Net.Mail.SmtpClient();
            {
                smtp.Host = ConfigurationManager.AppSettings["Host"];
                smtp.Port = Convert.ToInt32(ConfigurationManager.AppSettings["Port"]);
                //smtp.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["UserName"], ConfigurationManager.AppSettings["Password"]);
            }

            smtp.Send(_mailMessage);
            return true;
        }

        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }

Please help me in solving this issue. 请帮助我解决这个问题。

Thanks in advance. 提前致谢。

You have to render your HTML. 您必须呈现HTML。 We do this with a RazorHelper Function and a Partial View to style your email. 我们使用RazorHelper函数和“局部视图”来设置电子邮件的样式。 There is this function in a Helper Class: 助手类中有以下功能:

public static string RenderPartialViewToString(Controller controller, string viewName, object model)
{
    controller.ViewData.Model = model;
    using (var sw = new StringWriter())
    {
        ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
        var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
        viewResult.View.Render(viewContext, sw);

        return sw.GetStringBuilder().ToString();
    }
}

Then call it in a Controller context: 然后在Controller上下文中调用它:

email.Body = RazorHelper.RenderPartialViewToString(this, "PartialViewName", model);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM