从我的Web应用程序发送Html邮件时收到邮件包含符号& ldquo,& rsquo,'

时间:2013-07-08 13:27:46

标签: asp.net-mvc

我通过此功能发送文本/ Html邮件 这里BodyMessage和mailSubject是Html Text。

但是当收到邮件时,它包含像& ldquo,& rdquo这样的符号,'如何删除邮件。

    public static bool SendMailByTemplates(string toAddress, string ccAddress, string bccAddress, string mailSubject, string bodyMessage, List<KeyValuePair<string, String>> attachmentFile)
    {
        MailMessage msg = new MailMessage();
        GetConfigurationForAdmin();
        GetConfiguration();
        SmtpClient mailClient = new SmtpClient();
        if (toAddress == "Admin")
        {
            toAddress = toAdmin;
        }
        else if (ccAddress == "Admin")
        {
            ccAddress = toAdmin;

        }
        msg.From = new MailAddress(fromAddress);
        msg.To.Add(new MailAddress(toAddress));
        if (ccAddress != "" && ccAddress != null)
            msg.CC.Add(new MailAddress(ccAddress));
        if (bccAddress != "" && bccAddress != null)
            msg.Bcc.Add(new MailAddress(bccAddress));



            LinkedResource headerlogo = new LinkedResource(HttpContext.Current.Server.MapPath("~/Content/Images/EmailTemplateImages/logo.jpg"));

            headerlogo.ContentId = "headerlogo";


            AlternateView htmlView = AlternateView.CreateAlternateViewFromString(bodyMessage, null, MediaTypeNames.Text.Html);
            htmlView.LinkedResources.Add(headerlogo);
            htmlView.LinkedResources.Add(greenlogo);
            htmlView.LinkedResources.Add(skylinelogo);

            msg.Subject = mailSubject;
           /* msg.Body = bodyMessage;
            msg.BodyEncoding = System.Text.Encoding.GetEncoding("utf-8");
            */
            if (attachmentFile != null)
            {
                foreach (KeyValuePair<string, String> attachment in attachmentFile)
                {
                    if (!(attachment.Key == null || attachment.Key == ""))
                    {

                        Attachment attach = new Attachment(attachment.Key);
                        attach.Name = attachment.Value;

                        msg.Attachments.Add(attach);

                    }
                }
            }


            msg.IsBodyHtml = true;
            NetworkCredential basicAuthenticationInfo = new NetworkCredential(fromAddress, password);
            mailClient.Host = host;
            mailClient.Port = port;
            mailClient.EnableSsl = true;
            mailClient.UseDefaultCredentials = false;
            mailClient.Credentials = basicAuthenticationInfo;
            System.Net.Mail.AlternateView plainView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(System.Text.RegularExpressions.Regex.Replace(bodyMessage, @"<(.|\n)*?>", string.Empty), null, "text/plain");
            msg.AlternateViews.Add(plainView);
            msg.AlternateViews.Add(htmlView);
            msg.ReplyTo = new MailAddress(replyAddress);

            mailClient.Send(msg);

        }

    }

任何人都可以为我提供上述问题的解决方案。

1 个答案:

答案 0 :(得分:0)

检查htmlView的另一个构造函数:

ContentType mimeType = new System.Net.Mime.ContentType("text/html");
var htmlView = AlternateView.CreateAlternateViewFromString(bodyMessage, mimeType);