c# - 如何将 "paragraph breaks"添加到我的 html 电子邮件中?

标签 c# html gmail html-email

我正在尝试在我生成的电子邮件消息中添加分段符。我已经尝试了 Environment.NewLine 并添加了多个 html 段落:

// Take 1, using Environment.NewLine:
htmlBody.Add(string.Format("<p>Hello {0}. You have been assigned the {1} on the " +
"<em>Apply Yourself to the Field Ministry</em> midweek meeting for the week beginning {2}.{3}{3}" +
"Your counsel point is {4}; if this is not a Bible reading, your HH is {5};{3}{3}" +
"You can find the material for your assignment in your Meeting Workbook.{3}{3}" + 
"Thank your for your paticipation!</p>",
    fullName, friendlyTalkType, weekOfTalk.ToLongDateString(), Environment.NewLine, counselPoint, HH, MWURL));

// Take 2, using p tags:
htmlBody.Add(string.Format("<div><p>Hello, {0}. You have been assigned the {1} on the " +
"<em>Apply Yourself to the Field Ministry</em> midweek meeting for the week beginning {2} (actual date is {3}).</p><p></p><p></p>" +
"<p>Your counsel point is {4}; if applicable, your HH is {5};</p><p></p><p></p>" +
"<p>You can find the material for your assignment in your Meeting Workbook or online here: {6}</p><p></p><p></p>" +
"Thank your for your participation!</p></div>",
    fullName, friendlyTalkType, weekOfTalk.ToLongDateString(),
    weekOfTalk.AddDays(DAYS_BETWEEN_MONDAY_AND_THURSDAY).ToLongDateString(),
    counselPoint, HH, MWURL));

在这两种情况下,文本各部分之间都没有垂直分隔。我需要克服什么障碍才能做到这一点?

对于上下文,这是整个方法:

public static void SendEmail(string fullName, string toEmail, string HH, int talkType, DateTime weekOfTalk, int counselPoint)
{
    const int DAYS_BETWEEN_MONDAY_AND_THURSDAY = 3;
    var fromAddress = new MailAddress(FROM_EMAIL, FROM_EMAIL_NAME);
    var toAddress = new MailAddress(toEmail, fullName);
    string fromPassword = GMAIL_PASSWORD;
    string subject = $"{UPCOMING_AYttFM_ASSIGNMENT} ({weekOfTalk.ToLongDateString()})";
    string friendlyTalkType = GetTalkTypeAsStringForInt(talkType);
    string body;
    string MWURL = GetLinkForMeetingWorkbookForWeek(weekOfTalk);

    List<String> htmlBody = new List<string>
        {
            "<html><body>"
        };
    htmlBody.Add(string.Format("<div><p>Hello, {0}. You have been assigned the {1} on the " +
     "<em>Apply Yourself to the Field Ministry</em> midweek meeting for the week beginning {2} (actual date is {3}).</p><p></p><p></p>" +
     "<p>Your counsel point is {4}; if applicable, your Householder is {5};</p><p></p><p></p>" +
     "<p>You can find the material for your assignment in your Meeting Workbook or online here: {6}</p><p></p><p></p>" +
     "Thank your for your participation!</p></div>",
        fullName, friendlyTalkType, weekOfTalk.ToLongDateString(),
        weekOfTalk.AddDays(DAYS_BETWEEN_MONDAY_AND_THURSDAY).ToLongDateString(),
        counselPoint, HH, MWURL));

    htmlBody.Add("</body></html>");
    body = string.Join("", htmlBody.ToArray());

    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,
        IsBodyHtml = true
    })
    {
        smtp.Send(message);
    }
}

最佳答案

添加</br>段落中的标记。像这样

htmlBody.Add(string.Format("<p>Hello </br>. You have been assigned the </br> on the " +
"<em>Apply Yourself to the Field Ministry</em> midweek meeting for the week beginning </br>" +
"Your counsel point is </br>; if this is not a Bible reading, your HH is </br>}" +
"You can find the material for your assignment in your Meeting Workbook.</br>" + 
"Thank your for your paticipation!</p>",

关于c# - 如何将 "paragraph breaks"添加到我的 html 电子邮件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35692796/

相关文章:

c# - 空合并参数给出意外警告

javascript - 内联 CSS 以对齐社交分享图标

html - 添加链接到按钮 HTML

javascript - 如何组织一个在 Node-express 服务器上运行的网站?

php - 有没有办法在 gmail/wave/gdocs 中粘贴代码片段/ block ?

Android以编程方式打开电子邮件客户端

c# - 如何使用 C# 代码运行 C# 代码?

c# - NServiceBus 处理程序内的多线程

c# - 将 EntityFramework 实体绑定(bind)到 TreeView

php - 无法使用 2 个可能的身份验证器使用用户名在 SMTP 服务器上进行身份验证