views:

26

answers:

1

Hi, 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)

+1  A: 

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.

drupop