Sending a mail as both HTML and Plain Text in .net

后端 未结 6 1302
暖寄归人
暖寄归人 2020-12-24 05:12

I\'m sending mail from my C# Application, using the SmtpClient. Works great, but I have to decide if I want to send the mail as Plain Text or HTML. I wonder, is there a way

6条回答
  •  滥情空心
    2020-12-24 05:50

    The MSDN Documentation seems to miss one thing though, I had to set the content type manually, but otherwise, it works like a charm :-)

    MailMessage msg = new MailMessage(username, nu.email, subject, body);
    msg.BodyEncoding = Encoding.UTF8;
    msg.SubjectEncoding = Encoding.UTF8;
    
    AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlContent);
    htmlView.ContentType = new System.Net.Mime.ContentType("text/html");
    msg.AlternateViews.Add(htmlView);
    

提交回复
热议问题