0

我正在使用此代码向我的同事发送邮件。当我使用“\r\n”时,mailMessage.Body 的部分不起作用。而不是像这样显示电子邮件:

entity.PrimaryMeal.Title

entity.SecondaryMeal.Title

Porosine mund ta beni ketu:<>(这是我的语言 AL)

它显示如下:

entity.PrimaryMeal.Title,entity.SecondaryMeal.Title。Porosine mund ta beni ketu: <>

我究竟做错了什么?

private void SendMail(string MailReciever)
{
    Configuration configuration = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
    MailSettingsSectionGroup mailSettingsSectionGroup = (MailSettingsSectionGroup)configuration.GetSectionGroup("system.net/mailSettings");

    string MailSender = mailSettingsSectionGroup.Smtp.From;
    string Username = mailSettingsSectionGroup.Smtp.Network.UserName;
    string UserPassword = mailSettingsSectionGroup.Smtp.Network.Password;
    string SmtpServer = mailSettingsSectionGroup.Smtp.Network.Host;
    int Port = mailSettingsSectionGroup.Smtp.Network.Port;
    bool UseSsl = mailSettingsSectionGroup.Smtp.Network.EnableSsl;
    bool UseDefaultCredentials = mailSettingsSectionGroup.Smtp.Network.DefaultCredentials;

    using (SmtpClient smtpClient = new SmtpClient())
    using (MailMessage mailMessage = new MailMessage())
    {
        mailMessage.To.Add(MailReciever);
        mailMessage.From = new MailAddress(MailSender);
        mailMessage.Subject = ConfigurationManager.AppSettings["NewMailSubject"];

        smtpClient.Host = SmtpServer;
        smtpClient.UseDefaultCredentials = UseDefaultCredentials;
        smtpClient.Port = Port;
        smtpClient.Credentials = new NetworkCredential(Username, UserPassword);
        smtpClient.EnableSsl = UseSsl;

        #region MailMessageBody

        var entity = Factory.Orders.List(item => item.OrderDate == DateTime.Today).ToList().FirstOrDefault();

        if (entity.SecondaryMealId == -1)
        {
            mailMessage.Body = entity.PrimaryMeal.Title + ".\r\nPorosine mund ta beni ketu: http://10.200.30.11:8888";
        }

        else if (entity.TertiaryMealId == -1)
        {
            mailMessage.Body = entity.PrimaryMeal.Title + ",\r\n" + entity.SecondaryMeal.Title + ".\r\nPorosine mund ta beni ketu: http://10.200.30.11:8888";
        }

        else
        {
            mailMessage.Body = entity.PrimaryMeal.Title + ",\r\n" + entity.SecondaryMeal.Title + ",\r\n" + entity.TertiaryMeal.Title + ".\r\nPorosine mund ta beni ketu: http://10.200.30.11:8888";
        }

        #endregion

        mailMessage.IsBodyHtml = true;

        smtpClient.Send(mailMessage);
    }
}
4

2 回答 2

5
mailMessage.IsBodyHtml = true;

您以Html 格式发送电子邮件(忽略原始换行符),您应该添加<br>标签(或使用段落)。

    if (entity.SecondaryMealId == -1)
    {
        mailMessage.Body = entity.PrimaryMeal.Title + ".<br>Porosine mund ta beni ketu: http://10.200.30.11:8888";
    }

    else if (entity.TertiaryMealId == -1)
    {
        mailMessage.Body = entity.PrimaryMeal.Title + ",<br>" + entity.SecondaryMeal.Title + ".\r\nPorosine mund ta beni ketu: http://10.200.30.11:8888";
    }

    else
    {
        mailMessage.Body = entity.PrimaryMeal.Title + ",<br>" + entity.SecondaryMeal.Title + ",<br>" + entity.TertiaryMeal.Title + ".<br>Porosine mund ta beni ketu: http://10.200.30.11:8888";
    }
于 2013-10-29T08:08:42.340 回答
1

我认为最好发送 HTML 邮件。这意味着您需要放置<br/>而不是 \r\n 并将消息正文类型设置为 HTML。

于 2013-10-29T08:11:33.013 回答