Question

I'm using this code to send mails to my coworkers. The part at the mailMessage.Body, when I'm using "\r\n" is not working. Instead of showing the e-mail like this:

entity.PrimaryMeal.Title

entity.ScondaryMeal.Title

Porosine mund ta beni ketu: <> (this is in my language AL)

it is showing like this:

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

What am I doing wrong?

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);
    }
}
Was it helpful?

Solution

mailMessage.IsBodyHtml = true;

You are sending your email as Html (which ignores raw line breaks), you should add the <br> tag instead (or work with paragraphs).

    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";
    }

OTHER TIPS

I think its better to send HTML mail. That means you need to put <br/> instead of \r\n and set Message body type as HTML.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top