текстовая / обычная версия составного электронного письма, отправленного с mailkit, не полученным правильно - PullRequest
0 голосов
/ 20 сентября 2019

Я использую mimekit и mailkit для создания электронной почты, состоящей из нескольких частей, которая содержит HTML-текст и текстовое тело.HTML-версия работает нормально.Простой текст работает нормально, но только когда HTML не включен.

Когда я создаю электронную почту, состоящую из нескольких частей, которая содержит как текст, так и HTML-тела, кажется, что только HTML получен как в Outlook, так и в Gmail.Если я установлю в моей почте только обычный текст, я получу текст, разобранный в теле html.Я посмотрел оригинал в gmail и расшифровал его, чтобы найти, что это разобранный html, а не отдельная текстовая версия, которую я пытаюсь отправить с помощью mailkit.

Вот код, который создает и отправляет электронное письмо.

public void SendEmail(List<string> toAddresses, string fromAddress, string subject, string htmlBody, string textBody)
{
        var message = new MimeMessage();

        message.From.Add(new MailboxAddress(fromAddress));

        foreach (var to in toAddresses)
        {
            message.To.Add(new MailboxAddress(to));
        }

        message.Subject = subject;

        var bodyBuilder = new BodyBuilder
        {
            HtmlBody = htmlBody,
            TextBody = textBody
        };

        message.Body = bodyBuilder.ToMessageBody();

        _smtpClient.Connect(_settings.SmtpHostServer, _settings.SmtpPortNumber);
        _smtpClient.Authenticate(_settings.SmtpUserName, _settings.SmtpUserPassword);
        _smtpClient.Send(message);
        _smtpClient.Disconnect(true);
}

Когда я записываю сообщение в файл, оно имеет формат MIME и содержит отдельные разделы для HTML и обычного текста с содержанием, которое я ожидаю для каждого раздела.

From: ************
Date: Mon, 23 Sep 2019 09:10:35 -0600
Subject: Welcome To ************
Message-Id: <FVVQHEHRM8U4.J4JDCO9VD6OS2@localhost.localdomain>
To: ************
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="=-YWTR3eTYkSy366wQ3CLhlw=="

--=-YWTR3eTYkSy366wQ3CLhlw==
Content-Type: text/plain; charset=utf-8

=================================================================================
Welcome to ************
=================================================================================

You are receiving this email because you have registered with *****.

Please copy the following URL below into your web browser to login to *******.

https://************/login

Sincerely,

The ******* Team

=================================================================================
******** Contact Information
=================================================================================

Email: ************
Phone: ***********

--=-YWTR3eTYkSy366wQ3CLhlw==
Content-Type: text/html; charset=utf-8
Content-Id: <6MLWHEHRM8U4.ZO5T7EVTEFZ42@localhost.localdomain>

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <style type="text/css">

        /* FONTS */
        @media screen {
            @font-face {
                font-family: 'Lato';
                font-style: normal;
                font-weight: 400;
                src: local('Lato Regular'), local('Lato-Regular'), url(https://fonts.gstatic.com/s/lato/v11/qIIYRU-oROkIk8vfvxw6QvesZW2xOQ-xsNqO47m55DA.woff) format('woff');
            }

            @font-face {
                font-family: 'Lato';
                font-style: normal;
                font-weight: 700;
                src: local('Lato Bold'), local('Lato-Bold'), url(https://fonts.gstatic.com/s/lato/v11/qdgUG4U09HnJwhYI-uK18wLUuEpTyoUstqEm5AMlJo4.woff) format('woff');
            }

            @font-face {
                font-family: 'Lato';
                font-style: italic;
                font-weight: 400;
                src: local('Lato Italic'), local('Lato-Italic'), url(https://fonts.gstatic.com/s/lato/v11/RYyZNoeFgb0l7W3Vu1aSWOvvDin1pK8aKteLpeZ5c0A.woff) format('woff');
            }

            @font-face {
                font-family: 'Lato';
                font-style: italic;
                font-weight: 700;
                src: local('Lato Bold Italic'), local('Lato-BoldItalic'), url(https://fonts.gstatic.com/s/lato/v11/HkF_qI1x_noxlxhrhMQYELO3LdcAZYWl9Si6vvxL-qU.woff) format('woff');
            }
        }

        /* CLIENT-SPECIFIC STYLES */
        body, table, td, a {
            -webkit-text-size-adjust: 100%;
            -ms-text-size-adjust: 100%;
        }

        table, td {
            mso-table-lspace: 0pt;
            mso-table-rspace: 0pt;
        }

        img {
            -ms-interpolation-mode: bicubic;
        }

        /* RESET STYLES */
        img {
            border: 0;
            height: auto;
            line-height: 100%;
            outline: none;
            text-decoration: none;
        }

        table {
            border-collapse: collapse !important;
        }

        body {
            height: 100% !important;
            margin: 0 !important;
            padding: 0 !important;
            width: 100% !important;
        }

        /* iOS BLUE LINKS */
        a[x-apple-data-detectors] {
            color: inherit !important;
            text-decoration: none !important;
            font-size: inherit !important;
            font-family: inherit !important;
            font-weight: inherit !important;
            line-height: inherit !important;
        }

        /* MOBILE STYLES */
        @media screen and (max-width:600px) {
            h1 {
                font-size: 32px !important;
                line-height: 32px !important;
            }
        }

        /* ANDROID CENTER FIX */
        div[style*="margin: 16px 0;"] {
            margin: 0 !important;
        }
    </style>
</head>
<body style="background-color: #f4f4f4; margin: 0 !important; padding: 0 !important;">
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
        <!-- LOGO -->
        <tr>
            <td bgcolor="#539be2" align="center">
                <!--[if (gte mso 9)|(IE)]>
                <table align="center" border="0" cellspacing="0" cellpadding="0" width="600">
                <tr>
                <td align="center" valign="top" width="600">
                <![endif]-->
                <table border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width: 600px;">
                    <tr>
                        <td align="center" valign="top" style="padding: 50px 20px 50px 20px;">     
                            &nbsp;
                        </td>
                    </tr>
                </table>
                <!--[if (gte mso 9)|(IE)]>
                </td>
                </tr>
                </table>
                <![endif]-->
            </td>
        </tr>
        <!-- HERO -->
        <tr>
            <td bgcolor="#539be2" align="center" style="padding: 0px 10px 0px 10px;">
                <!--[if (gte mso 9)|(IE)]>
                <table align="center" border="0" cellspacing="0" cellpadding="0" width="600">
                <tr>
                <td align="center" valign="top" width="600">
                <![endif]-->
                <table border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width: 600px;">
                    <tr>
                        <td bgcolor="#ffffff" align="center" valign="top" style="padding: 40px 20px 20px 20px; border-radius: 4px 4px 0px 0px; color: #111111; font-family: 'Lato', Helvetica, Arial, sans-serif; font-size: 48px; font-weight: 400; letter-spacing: 4px; line-height: 48px;">
                            <h1 style="font-size: 48px; font-weight: 400; margin: 0;">Welcome to ********</h1>
                        </td>
                    </tr>
                </table>
                <!--[if (gte mso 9)|(IE)]>
                </td>
                </tr>
                </table>
                <![endif]-->
            </td>
        </tr>
        <!-- COPY BLOCK -->
        <tr>
            <td bgcolor="#f4f4f4" align="center" style="padding: 0px 10px 0px 10px;">
                <!--[if (gte mso 9)|(IE)]>
                <table align="center" border="0" cellspacing="0" cellpadding="0" width="600">
                <tr>
                <td align="center" valign="top" width="600">
                <![endif]-->
                <table border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width: 600px;">
                    <!-- COPY -->
                    <tr>
                        <td bgcolor="#ffffff" align="center" style="padding: 20px 30px 10px 30px; color: #666666; font-family: 'Lato', Helvetica, Arial, sans-serif; font-size: 18px; font-weight: 400; line-height: 25px;">

<p>
    You are receiving this email because you have registered with *********.
</p>


<table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
        <td bgcolor="#ffffff" align="center" style="padding: 20px 30px 30px 30px;">
            <table border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td align="center" bgcolor="#539be2">
                        <a href="https://**********/login" target="_blank" style=" border-radius: 3px;  font-size: 20px; font-family: Helvetica, Arial, sans-serif; color: #ffffff; text-decoration: none; color: #ffffff; text-decoration: none; padding: 15px 25px; border-radius: 2px; border: 1px solid #539be2; display: inline-block;">
                            Login To *********
                        </a>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>

<p>Sincerely,</p>
<p>The ******** Team</p>
                        </td>
                    </tr>
                </table>
                <!--[if (gte mso 9)|(IE)]>
                </td>
                </tr>
                </table>
                <![endif]-->
            </td>
        </tr>
        <!-- FOOTER -->
        <tr>
            <td bgcolor="#f4f4f4" align="center" style="padding: 0px 10px 0px 10px;">
                <!--[if (gte mso 9)|(IE)]>
                <table align="center" border="0" cellspacing="0" cellpadding="0" width="600">
                <tr>
                <td align="center" valign="top" width="600">
                <![endif]-->
                <table border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width: 600px;">
                    <!-- NAVIGATION -->
                    <tr>
                        <td bgcolor="#ffffff" align="center" style="padding: 30px 30px 30px 30px; color: #666666; font-family: 'Lato', Helvetica, Arial, sans-serif; font-size: 14px; font-weight: 400; line-height: 18px;">
                            <p style="margin: 0;">
                                <a href="mailto:*********" style="color: #039be5; font-weight: 700;">**********</a>
                            </p>
                            <p style="margin: 0;">
                                <a href="tel:8865715014" style="color: #039be5; font-weight: 700;">(866) 571-5014</a>
                            </p>
                        </td>
                    </tr>
                </table>
                <!--[if (gte mso 9)|(IE)]>
                </td>
                </tr>
                </table>
                <![endif]-->
            </td>
        </tr>
    </table>
</body>
</html>
--=-YWTR3eTYkSy366wQ3CLhlw==--

1 Ответ

0 голосов
/ 23 сентября 2019

Звучит как ошибки в GMail и Outlook.Сырой MIME выглядит хорошо.В этом нет абсолютно ничего плохого.

...