如何在C#中将dymanically生成的html作为电子邮件附件发送

时间:2017-08-05 09:32:35

标签: c# asp.net

我正在使用以下代码发送电子邮件,但如果我使用了html附件,则会失败。

System.IO.MemoryStream ms = new System.IO.MemoryStream();

System.IO.StreamWriter writer2 = new System.IO.StreamWriter(ms);
writer2.Write("<html><head></head><body>Invoice 1</body></html>");
writer2.Flush();
writer2.Dispose();

MailMessage mm = new MailMessage();
mm.To.Add("test@gmail.com");
mm.CC.Add("test2@gmail.com");
mm.From = new MailAddress("test3@gmail.com");
mm.Subject = "नोटिस";
mm.Body = sb.ToString();

System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Text.Html);
System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(ms, ct);
attach.ContentDisposition.FileName = "myFile.html";
mm.Attachments.Add(attach);
mm.Attachments.Add(new Attachment(new MemoryStream(bytes), "rcms.pdf"));
ms.Close();
mm.IsBodyHtml = true;                   

1 个答案:

答案 0 :(得分:0)

only one line needed.

var a = System.Net.Mail.Attachment.CreateAttachmentFromString("‌​'"+sb.ToString()+"'<‌​/body>", "notices.html");