如何防止附件内容显示在邮件正文中?

发布于 2024-09-25 23:26:10 字数 3989 浏览 2 评论 0 原文

我正在开发一个 asp.net c# 应用程序,该应用程序发送一封带有一个附件的电子邮件。 附件是 vCalendar 文件。 这是代码:

            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:[email protected]");
            sbCalendar.AppendLine("ATTENDEE;ROLE=CHAIR;PARTSTAT=ACCEPTED:MAILTO:[email protected]");
            sbCalendar.AppendLine("CLASS:PUBLIC");
            sbCalendar.AppendLine("ORGANIZER:MAILTO:[email protected]");
            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("[email protected]");

            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参数的内容之外,还出现了附件的内容,类似这样:

From:sender 发送时间:2010 年 10 月 5 日星期二下午 4:59 至:某个电子邮件

这里是messageBody内容

方法:请求 开始:V日历 产品编号:-//DP//NET MIMEDIR //版本:1.0 方法:请求 开始:活动 DTSTAMP:20101006T135934Z 开始时间:20101006T135934Z 日期:20101006T135934Z 地点:明斯特德 描述;编码=引用-可打印:我的第一次会议 摘要:学习日历和日程安排 优先级:3 UID:721d9e3c-9010-47f5-9ad0-83c38cb0cbb7 参加者;角色=请求参与者;PARTSTAT=需要操作:MAILTO:someemail 参加者;角色=主席;PARTSTAT=已接受:MAILTO:someemail 类别:公共 组织者:MAILTO:someemail 序列:0 状态:暂定 结束:VENT 结束:V日历

我想删除该文本,只显示 messageBody 参数的内容,并将 vCalendar 文件附加到邮件消息中。 我该怎么做?这是 Outlook 问题还是编码问题?

编辑:我只对在 Microsoft Outlook 中显示消息感兴趣。我已经查看了邮件的来源(在 Outlook 中右键单击 > 查看源代码),我想要删除的文本位于 >>消息的 html 标签)

I'm working on an asp.net c# application that sends an email with one attachment.
The attachment is a vCalendar file.
Here's the code:

            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:[email protected]");
            sbCalendar.AppendLine("ATTENDEE;ROLE=CHAIR;PARTSTAT=ACCEPTED:MAILTO:[email protected]");
            sbCalendar.AppendLine("CLASS:PUBLIC");
            sbCalendar.AppendLine("ORGANIZER:MAILTO:[email protected]");
            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("[email protected]");

            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);

This works and does what is supposed to do, it sends a working (recognized by outlook) vcalendar file.

The problem is that in the body of the mail, besides the contents of the messageBody parameter, the contents of the attached file also appear, something like this:

From: sender
Sent: Tuesday, October 05, 2010 4:59 PM
To: someemail

messageBody contents here

METHOD: REQUEST
BEGIN:VCALENDAR
PRODID:-//DP//NET
MIMEDIR//ENVERSION:1.0
METHOD:REQUEST
BEGIN:VEVENT
DTSTAMP:20101006T135934Z
DTSTART:20101006T135934Z
DTEND:20101006T135934Z
LOCATION:Minstead
DESCRIPTION;ENCODING=QUOTED-PRINTABLE:My first meeting
SUMMARY:Learning Calendaring and Scheduling
PRIORITY:3
UID:721d9e3c-9010-47f5-9ad0-83c38cb0cbb7
ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION:MAILTO:someemail
ATTENDEE;ROLE=CHAIR;PARTSTAT=ACCEPTED:MAILTO:someemail
CLASS:PUBLIC
ORGANIZER:MAILTO:someemail
SEQUENCE:0
STATUS:TENTATIVE
END:VEVENT
END:VCALENDAR

I want to get rid of that text, and display only the contents of my messageBody parameter and have the vCalendar file just attached to the mail message.
How can i do this? Is this an outlook issue or a coding issue?

Edit: I'm only interested in displaying the message in Microsoft Outlook. I've looked into the source of the message (in Outlook right click > View Source) and the text i want to get rid of is within the <body></body> html tags of the message)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幸福丶如此 2024-10-02 23:26:10

找到解决方案:

http://msdn.microsoft .com/en-us/library/system.net.mime.dispositiontypenames.attachment.aspx

在附件的构造函数中,我将 "text/calendar" 替换为 MediaTypeNames。 Application.Octet 并将 DispositionType 设置为 Attachment,而不是 Inline(这可能是默认值)。

            Attachment attachment = new Attachment(contentStream, "termenLitigiu.ics", MediaTypeNames.Application.Octet);
            attachment.ContentDisposition.DispositionType = DispositionTypeNames.Attachment;

现在,它为我提供了一条干净的邮件消息,消息正文包含应有的内容和一个有效的 .ics 附件。

Found the solution:

http://msdn.microsoft.com/en-us/library/system.net.mime.dispositiontypenames.attachment.aspx

In the constructor of the Attachment I replaced "text/calendar" with MediaTypeNames.Application.Octet and set the DispositionType to Attachment as opposed to Inline which was probably the default value.

            Attachment attachment = new Attachment(contentStream, "termenLitigiu.ics", MediaTypeNames.Application.Octet);
            attachment.ContentDisposition.DispositionType = DispositionTypeNames.Attachment;

It now gives me a clean mail message with the body of the message containing what it should and a working .ics attachment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文