在C#中发送电子邮件时不显示背景色

时间:2019-04-19 22:29:34

标签: c# html css smtp

这是我的C#代码,它将电子邮件发送到给定的电子邮件地址,它几乎可以正常工作,但是问题是我只获得了文本作为“标记”,但是颜色根本没有在后台显示(这应该是是DodgerBlue),为什么会发生这种情况? (我认为问题可能是由身体本身引起的...)

    private bool SendEmail(string BodyText)
    {
        var fromAddress = new MailAddress("EmailIHave@gmail.com", "HackerOne Scanner");
        var toAddress = new MailAddress("ToWhom@gmail.com", "Title");
        const string fromPassword = "Password";
        const string subject = "Important Update";
        string body = "<h3 style=\"background - color:DodgerBlue; padding: 25px 25px 25px 25px; text - align: center; \">"+BodyText+"</h3>";

        var smtp = new SmtpClient
        {
            Host = "smtp.gmail.com",
            Port = 587,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
        };
        using (var message = new MailMessage(fromAddress, toAddress)
        {
            Subject = subject,
            Body = body
        })
        {
            try
            {
                message.IsBodyHtml = true;
                smtp.Send(message);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }

谢谢。

1 个答案:

答案 0 :(得分:1)

修剪空格

string body = "<h3 style=\"background-color:DodgerBlue; padding: 25px 25px 25px 25px; text-align: center; \">"+BodyText+"</h3>";