首页 > 解决方案 > 发送 html 电子邮件时,内联 css 样式未正确应用

问题描述

我正在尝试通过 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 上的外观

结果

** 来自浏览器的 html 视图——它应该是怎样的 **

在此处输入图像描述

标签: c#htmlcssemail

解决方案


发布您的 c# 代码会使破译您的问题变得更加困难。以下是您的一些问题:

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

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

在 css 值之间添加空格可能适用于某些电子邮件客户端,但这不是标准的。使用 this:background - color而不是也background-color可能导致您的问题。


推荐阅读