Tom*_*ear 6 c# email content-type mime-types
我有一封我想从C#发送的电子邮件,它有一个vCalendar和一个HTML正文部分.
我创建了一个MailMessage,并设置了2个备用视图:
AlternateView avCal = new AlternateView("VCALENDAR:...", null, "text/calendar");
AlternateView avHtml = new AlternateView("<p>some html</p>", null, "text/html");
mailMessage.AlternateViews.Add(avCal);
mailMessage.AlternateViews.Add(avHtml);
Run Code Online (Sandbox Code Playgroud)
这给了我一个消息,一个Content-Type的multipart/alternative.
这将在我的网络邮件上显示日历约会和HTML部分,但不显示Outlook.
如何以不同的内容类型显示这样的两个不同部分?我正在寻找的更像是Content-Type: multipart/mixed"备用视图"出现的地方.
编辑
当我使用@Chris Haas的方法时,我会接近,但标记不会呈现.这似乎是无视的MailMessage.IsBodyHtml = true

不确定如何在Outlook中查看原始但只是标题...
Return-Path: <*****@****.com>
X-Footer: ZWJyaWRnZS5jb20=
Received: from localhost ([127.0.0.1])
by mail.foo.com
for *****@****.com;
Wed, 2 Jan 2013 17:20:14 -0500
MIME-Version: 1.0
From: "George Washington" <*****@****.com>
To: "George Washington" <*****@****.com>
Date: 2 Jan 2013 17:29:14 -0500
Subject: To-Do: test test - test
Content-Type: multipart/mixed;
boundary=--boundary_0_4fbc08b4-2198-45b1-bf2e-9659179aad84
Run Code Online (Sandbox Code Playgroud)
尝试发送VCALENDAR作为Attachment与Inline属性设置为true:
using (MailMessage mm = new MailMessage("...", "...", "Subject here", "Body here")) //Pick whatever constructor you want
{
using (Attachment a = new Attachment("c:\\test.ics", "text/calendar")) //Either load from disk or use a MemoryStream bound to the bytes of a String
{
a.Name = "meeting.ics"; //Filename, possibly not required
a.ContentDisposition.Inline = true; //Mark as inline
mm.Attachments.Add(a); //Add it to the message
using (SmtpClient s = new SmtpClient("...")) //Send using normal
{
s.Send(mm);
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑
好的,我已经将代码更新为不依赖于文件,以便我们使用完全相同的 ICS 文件。更新顶部的字符串和SmtpClient如果需要,否则保留代码原样。ICS 来自本页的中间。
String mailFrom = "xyz@example.com";
String mailTo = "xyz@example.com";
String mailSubject = "This is a test";
String mailBody = "<p><strong>Hello</strong> world</p>";
String smtpServer = "mail.example.com";
using (var mm = new MailMessage()) //Pick whatever constructor you want
{
mm.To.Add(mailFrom);
mm.From = new MailAddress(mailTo);
mm.Subject = mailSubject;
mm.Body = mailBody;
mm.IsBodyHtml = true;
String t = "BEGIN:VCALENDAR\r\n" +
"METHOD:REQUEST\r\n" +
"BEGIN:VEVENT\r\n" +
"DTSTAMP:20080325T202857Z\r\n" +
"DTSTART:20080325T200000Z\r\n" +
"DTEND:20080325T220000Z\r\n" +
"SUMMARY:Test meeting request\r\n" +
"UID:040000008200E00074C5B7101A82E00800000000B2BB07349575C80100000000000000001000000019BF8D0149C50643A81325C54140C093\r\n" +
"ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=\"Dan\":MAIL\r\n" +
" TO:myuser@mydom.com\r\n" +
"ORGANIZER;CN=\"Administrator\":MAILTO:administrator@mydom.com\r\n" +
"LOCATION: Here\r\n" +
"DESCRIPTION:Test Request\r\n" +
"SEQUENCE:0\r\n" +
"PRIORITY:5\r\n" +
"CLASS:\r\n" +
"CREATED:20080321T190958Z\r\n" +
"STATUS:CONFIRMED\r\n" +
"TRANSP:OPAQUE\r\n" +
"END:VEVENT\r\n" +
"END:VCALENDAR";
Byte[] bytes = System.Text.Encoding.ASCII.GetBytes(t);
using (var ms = new System.IO.MemoryStream(bytes))
{
using (var a = new Attachment(ms, "meeting.ics", "text/calendar")) //Either load from disk or use a MemoryStream bound to the bytes of a String
{
a.ContentDisposition.Inline = true; //Mark as inline
mm.Attachments.Add(a); //Add it to the message
using (SmtpClient s = new SmtpClient(smtpServer)) //Send using normal
{
s.Send(mm);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)