发送HTML电子邮件时,内联CSS样式无法正确应用

时间:2018-07-16 20:45:40

标签: c# html css email

我正在尝试通过C#代码发送带有html的邮件,但该邮件无法在Gmail上正确呈现。可以让我知道我在做什么错..

 MailMessage msg = new MailMessage();

        msg.From = new MailAddress("no.reply.checkarr@gmail.com");
        msg.To.Add(ReciverMail);
        msg.Subject = title;


        String bodyBuilder = "";

        bodyBuilder += "<div style = \"font - family: Calibri, Arial; background - image: url('https://www.aqeqah.com/wp-content/uploads/pattern-07.jpg'); background - color: transparent; background - position: center center; background - repeat: repeat; background - attachment: fixed; position: fixed; width: 100 %; height: 100 %; left: 0; top: 0; \">";
        bodyBuilder += "    <div style = \"margin: 0 auto; width: 90 %; border: 1px solid grey; text - align: center; background - color: white; border - radius: 5px; margin - top: 5 %; box - shadow: 0 16px 26px 0 rgba(0, 0, 0, 0.2), 0 6px 26px 0 rgba(0, 0, 0, 0.19); \">";
        bodyBuilder += "        <h1 style = \"color: #ff1017;\">Checkarr</h1>";
        bodyBuilder += "        <p style = \"padding - left: 5 %; padding - right: 5 %; \">We are sending this mail to you to inform about important updates for your account</p>";
        bodyBuilder += "        <h2>Account Verification</h2>";
        bodyBuilder += "        <p style = \"padding - left: 5 %; padding - right: 5 %; \">It looks like it's a great time to verify your account. Verified account are subjected to get best out of our services. Either use the following code to verify your account or click the button.</p>";
        bodyBuilder += "        <h3 style=\"color: grey; border: 3px dashed #ff1017; width: 100px; height: 30px;margin:0 auto; padding-top: 10px; border-radius: 5px\">ASDF32</h3>";
        bodyBuilder += "        <p style = \"color: grey\"> OR</p>";
        bodyBuilder += "        <button style=\"background - color: #ff1017;border: none;color: white;padding: 20px;text-align: center;text-decoration: none;display: inline-block;font-size: 16px;margin: 4px 2px;border-radius: 5px;cursor: pointer;\">Verify Account</button>";
        bodyBuilder += "    <p>Stay awesome!</p><br/><br/>";
        bodyBuilder += "    <p style = \"color: grey; font - size: 10px\">This is auto generated mail. Please don't reply</p>";
        bodyBuilder += "    <p style = \"color: grey; font - size: 10px\">Checkarr © - Islamabad, Pakistan </br>www.checkarr.pk</p>";
        bodyBuilder += "    </div>";
        bodyBuilder += "</div>";



        msg.Body = bodyBuilder;
        msg.IsBodyHtml = true;
        SmtpClient client = new SmtpClient();
        client.UseDefaultCredentials = true;
        client.Host = "smtp.gmail.com";
        client.Port = 587;
        client.EnableSsl = true;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.Credentials = new NetworkCredential("no.reply.checkarr@gmail.com", "/*PASS REMOVED*/");
        client.Timeout = 20000;
        try
        {
            client.Send(msg);
            System.Diagnostics.Debug.WriteLine("Mail has been successfully sent!");
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine("Exception while sending mail" + ex.Message);
        }
        finally
        {
            msg.Dispose();
        }

在gmail上的外观

outcome

**来自浏览器的html视图-应该如何显示

enter image description here

2 个答案:

答案 0 :(得分:0)

bodyBuilder字符串中存在一些格式问题。例如,我将“字体-家庭”更改为“字体家庭”,这样就解决了问题。我想我应该睡觉了:3

答案 1 :(得分:0)

发布您的C#代码会使解密您的问题变得更加困难。这是您的一些问题:

background-image不适用于所有版本的Gmail,也不适用于Outlook:

rgba-colors适用于Gmail,但不适用于任何版本的Outlook。

在css值之间添加空格可能在某些电子邮件客户端中可用,但这不是标准的。使用此方法:background - color而不是background-color也会助您一臂之力。