MailMessage sent string as body without newline in outlook

前端 未结 10 834
挽巷
挽巷 2020-12-11 15:44

HI, I am trying to send a simple notification using system.net.mail.mailmessage. I just pass the string as the message body. But problem is even though multi-line message be

相关标签:
10条回答
  • 2020-12-11 16:28

    //the email body should be HTML //replaces the new line characters \n\r with the break HTML

    mailMsg.IsBodyHtml = true;
    mailMsg.Body = this.Body;
    mailMsg.BodyEncoding = System.Text.Encoding.ASCII;
    mailMsg.Body = mailMsg.Body.Replace(Environment.NewLine, "<br/>"); 
    
    0 讨论(0)
  • 2020-12-11 16:31

    This worked for me:

        mail.Body = String.Format(@"Some text here:
    A new line here  
    And another line here");
    

    Just be careful to not indent on the 2nd and 3rd line.

    0 讨论(0)
  • 2020-12-11 16:32

    Necro ansering a question but could come in handy for some as well.

    msg.IsBodyHtml = true;
    AlternateView av = AlternateView.CreateAlternateViewFromString(msg.Body, new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Text.Html));
    av.TransferEncoding = System.Net.Mime.TransferEncoding.SevenBit;
    msg.AlternateViews.Add(av);
    AlternateView avPlain = AlternateView.CreateAlternateViewFromString(msg.Body, new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Text.Plain));
    avPlain.TransferEncoding = System.Net.Mime.TransferEncoding.SevenBit;
    msg.AlternateViews.Add(avPlain); 
    
    0 讨论(0)
  • 2020-12-11 16:37

    Set the IsBodyHTML to false:

    ms.IsBodyHtml = false;

    By default it is false but it appears you have set it to true somewhere as outlook thinks it is HTML.

    0 讨论(0)
提交回复
热议问题