【问题标题】:Email Body Format Problem电子邮件正文格式问题
【发布时间】:2012-04-30 18:05:28
【问题描述】:

我的电子邮件格式有问题。我想换个新线..

这是电子邮件中的格式..

Name: sdds Phone: 343434 Fax: 3434 Email: valencia_arman@yahoo.com Address: dsds Remarks: dsds Giftwrap: Yes Giftwrap Instructions: sdds Details: PEOPLE OF THE BIBLE(SCPOTB-8101-05) 1 x Php 275.00 = Php 275.00 Total: Php275.00

这是我的 C# 代码..

mail.Body = "Name: " + newInfo.ContactPerson + Environment.NewLine
                                + "Phone: " + newInfo.Phone + Environment.NewLine
                                + "Fax: " + newInfo.Fax + Environment.NewLine
                                + "Email: " + newInfo.Email + Environment.NewLine
                                + "Address: " + newInfo.Address + Environment.NewLine
                                + "Remarks: " + newInfo.Notes + Environment.NewLine
                                + "Giftwrap: " + rbGiftWrap.SelectedValue + Environment.NewLine
                                + "Giftwrap Instructions: " + newInfo.Instructions + Environment.NewLine + Environment.NewLine
                                + "Details: " + Environment.NewLine
                                + mailDetails;

【问题讨论】:

  • 我还在学习,但是使用String Builder来创建这个字符串不是更好吗?

标签: c# asp.net email


【解决方案1】:

如果您以 HTML 格式发送,请确保设置格式。

mail.BodyFormat = MailFormat.Html;

然后您可以根据需要使用<br/>

更新:

试试这个作为替代方案:

using System.Net.Mail;

...

MailMessage myMail;
myMail = new MailMessage();
myMail.IsBodyHtml = true;

【讨论】:

  • 所以您已将邮件类型设置为 html,并且您将
    添加到每个项目的末尾而不是 Environment.NewLine?您如何查看电子邮件?
  • 我在我的邮件中查看了它。我尝试了上面的代码,但在使用 mail.bodyformat = mailformat.html 时收到了问题。
  • 我添加了一些更改。您使用的是哪个 .Net 框架?我的片段使用 4.0。
  • @SilverFang - 如果您要使用 HTML,您可能需要对“备注”等字段进行 HtmlEncode,其中可能包含在 HTML 中具有特殊含义的字符。
【解决方案2】:

也许你可以试试这个...

我们创建单独的电子邮件模板(例如 EmailTemplate.htm),它包含要发送的消息。消息中的换行不会有问题。

那么这是我们的代码:

private void SendEmail()
{
   string emailPath = "../EmailTemplate.htm"; //Define your template path here
   string emailBody = string.Empty;

   StreamReader sr = new StreamReader(emailPath);

   emailBody = sr.ReadToEnd();
   sr.Close();
   sr.Dispose();

   //Send Email; you can refactor this out
   MailMessage message = new MailMessage();

   MailAddress address = new MailAddress("sender@domain.com", "display name");

   message.From = address;
   message.To.Add("to@domain.com");
   message.Subject = "Your Subject";
   message.IsBodyHtml = true; //defines that your email is in Html form
   message.Body = emailBody;

   //smtp is defined in web.config
   SmtpClient smtp = new SmtpClient();

   try
   {
      smtp.Send(message);
   }
   catch (Exception ex)
   {
      //catch errors here...
   }
}

【讨论】:

  • 是的,对我来说太晚了。哈哈! :D
【解决方案3】:

您是否尝试过“+\n”而不是 Environment.NewLine?

【讨论】:

    猜你喜欢
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 2011-06-02
    • 2020-05-09
    • 2012-02-26
    • 2016-02-23
    相关资源
    最近更新 更多