C# 如何防止附件内容显示在邮件正文中?
我正在开发一个asp.net c#应用程序,它发送一封带有一个附件的电子邮件。 附件是一个vCalendar文件。 代码如下:C# 如何防止附件内容显示在邮件正文中?,c#,asp.net,email,vcalendar,C#,Asp.net,Email,Vcalendar,我正在开发一个asp.net c#应用程序,它发送一封带有一个附件的电子邮件。 附件是一个vCalendar文件。 代码如下: StringBuilder sbCalendar = new StringBuilder(); DateTime dtStart = eventDate; DateTime dtEnd = eventDate; sbCalendar.AppendLine("METHOD:
StringBuilder sbCalendar = new StringBuilder();
DateTime dtStart = eventDate;
DateTime dtEnd = eventDate;
sbCalendar.AppendLine("METHOD: REQUEST");
sbCalendar.AppendLine("BEGIN:VCALENDAR");
sbCalendar.AppendLine("PRODID:-//DP//NET");
sbCalendar.AppendLine("MIMEDIR//ENVERSION:1.0");
sbCalendar.AppendLine("METHOD:REQUEST");
sbCalendar.AppendLine("BEGIN:VEVENT");
sbCalendar.AppendLine("DTSTAMP:" + dtStart.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z"));
sbCalendar.AppendLine("DTSTART:" + dtStart.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z"));
sbCalendar.AppendLine("DTEND:" + dtEnd.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z"));
sbCalendar.AppendLine("LOCATION:" + eventLocation);
sbCalendar.AppendLine("DESCRIPTION;ENCODING=QUOTED-PRINTABLE:" + eventBody);
sbCalendar.AppendLine("SUMMARY:" + eventSubject);
sbCalendar.AppendLine("PRIORITY:3");
sbCalendar.AppendLine("UID:" + Guid.NewGuid().ToString());
sbCalendar.AppendLine("ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION:MAILTO:required@participant.foo");
sbCalendar.AppendLine("ATTENDEE;ROLE=CHAIR;PARTSTAT=ACCEPTED:MAILTO:chair@foo.foo");
sbCalendar.AppendLine("CLASS:PUBLIC");
sbCalendar.AppendLine("ORGANIZER:MAILTO:organizer@foo.foo");
sbCalendar.AppendLine("SEQUENCE:0");
sbCalendar.AppendLine("STATUS:TENTATIVE");
sbCalendar.AppendLine("END:VEVENT");
sbCalendar.AppendLine("END:VCALENDAR");
byte[] byteArray = Encoding.UTF8.GetBytes(sbCalendar.ToString());
Stream contentStream = new MemoryStream(byteArray);
SmtpClient smtp = new SmtpClient("localhost");
MailMessage memo = new MailMessage();
memo.IsBodyHtml = true;
memo.From = new MailAddress("from@ddress.foo");
foreach (string emailAddress in emailAddresses)
{
memo.To.Add(emailAddress);
}
memo.Body = messageBody;
memo.Subject = messageSubject;
Attachment attachment = new Attachment(contentStream, "termenLitigiu.ics", "text/calendar");
attachment.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
memo.Attachments.Add(attachment);
smtp.Send(memo);
它工作正常,并且完成了应该执行的操作,它发送一个工作(由outlook识别)vcalendar文件
问题是,在邮件正文中,除了messageBody参数的内容外,还显示了附加文件的内容,如下所示:
发件人:发件人
发送时间:2010年10月5日星期二下午4:59
致:电子邮件
messageBody内容在这里
方法:请求
开始:VCALENDAR
PRODID:-//DP//NET
MIMEDIR//ENVERSION:1.0
方法:请求
开始:VEVENT
DTSTAMP:20101006T135934Z
DTSTART:20101006T135934Z
DTEND:20101006T135934Z
地点:明斯特
描述编码=引用-可打印:我的第一次会议
总结:学习日历和日程安排
优先事项:3
UID:721d9e3c-9010-47f5-9ad0-83C38CB0CB7
与会者;角色=REQ-参与者;PARTSTAT=NEEDS-ACTION:MAILTO:someemail
与会者;角色=主席;PARTSTAT=ACCEPTED:MAILTO:someemail
类别:公共
组织者:MAILTO:someemail
序列:0
现状:暂定
完:维文特
完:VCALENDAR
我想去掉该文本,只显示messageBody参数的内容,并将vCalendar文件附加到邮件消息。
我该怎么做?这是outlook问题还是编码问题
编辑:我只对在Microsoft Outlook中显示邮件感兴趣。我已经查看了邮件的来源(在Outlook中单击鼠标右键>查看来源),我要删除的文本位于邮件的html标记中)找到了解决方案:
在附件的构造函数中,我将“text/calendar”
替换为MediaTypeNames.Application.Octet
,并将DispositionType
设置为Attachment
,而不是Inline
,后者可能是默认值
Attachment attachment = new Attachment(contentStream, "termenLitigiu.ics", MediaTypeNames.Application.Octet);
attachment.ContentDisposition.DispositionType = DispositionTypeNames.Attachment;
现在,它为我提供了一封干净的邮件,邮件正文包含了它应该包含的内容和一个有效的.ics附件。找到了解决方案:
在附件的构造函数中,我将“text/calendar”
替换为MediaTypeNames.Application.Octet
,并将DispositionType
设置为Attachment
,而不是Inline
,后者可能是默认值
Attachment attachment = new Attachment(contentStream, "termenLitigiu.ics", MediaTypeNames.Application.Octet);
attachment.ContentDisposition.DispositionType = DispositionTypeNames.Attachment;
现在,它给了我一封干净的邮件,邮件主体包含了它应该包含的内容和一个有效的.ics附件