MailMessage.Headers and Content-Type

V

Vlad

We are trying to set MailMessage Content-Type to be text/calendar
The following code has been used:
string subject = "Calendar test 3";
MailMessage emailMessage = new MailMessage();
emailMessage.To = <address>
emailMessage.Subject = "my subject";
emailMessage.Headers.Add("Content-Type","text/calendar;method=\"REQUEST\"");
emailMessage.Body=@"BEGIN:VCALENDAR
PRODID:-//ACME/DesktopCalendar//EN
METHOD:REQUEST
VERSION:2.0....<the rest of iCalendar>
";
MailService.Send(emailMessage);

Even though the content-type was specified in Headers collection the
resulting message still contains "text/plain" content-type.

Please help.

Vlad
 
G

guidomuffin

You probably need to set the BodyFormat property on your MailMessage
object to get the content-type to what you want.

Example:

MailMessage mail = new MailMessage();
mail.To = <address>
mail.From = <address>
mail.Subject = emailSubject;
mail.BodyFormat = System.Web.Mail.MailFormat.Html;

Hope that helps

http://www.harleyc.com
 
V

Vlad

I dont think it has anything to do with HTML/Plain text. Content-type is
email header--not HTTP header. I tried it anyways just to be safe and got
the same results - plain text.

Still need MSFT help with this!
 
C

Christopher Reed

I believe that if the email reader does not support the "text/calendar"
type, it is probably converting it to "text/plain". Why email reader are
you using? If you have a web access email account, you might try sending an
email to that account and view it in something like FireFox which supports
"text/calendar".
 
V

Vlad

Actually we use MS Outlook 2003 which does support it. We tried it with a
custom third-party component that sets the header fine. However when we did
it through .NET native component it didn't work.
 
V

Vlad

Actually we use MS Outlook 2003 which does support it. We tried it with a
custom third-party component that sets the header fine. However when we did
it through .NET native component it didn't work.
 
Joined
Nov 18, 2006
Messages
1
Reaction score
0
Got it to work

After a lot of trial and error, I finally got this to work:

This part was correct:
Dim email As New MailMessage
email.To.Add(New MailAddress("(e-mail address removed)", "John Doe"))
Dim from As New MailAddress("(e-mail address removed)", "Sender Doe")
email.From = from
email.Subject = subject
email.Headers.Add("Content-Type", "text/calendar; method=REQUEST; charset=US-ASCII")

email.Body = "BEGIN:VCALENDAR..."

Dim SmtpMail As New SmtpClient()
SmtpMail.Host = "mail server"
SmtpMail.Send(email)

The issue was with the vCalendar. If the syntax isn't correct, then it comes through as an empty email. For me, the problem was I left out the UID.

The troubleshoot, I attached the vCalendar to an email. When trying to open the attached file, Outlook generated an error. I saved the file locally, and made changes until I could import.

Here is the StringBuilder that I am now using to generate the vCalendar
parameters passed into the function:
(ByVal toAddress As String, _
ByVal toName As String, _
ByVal fromAddress As String, ByVal fromName As String, _
ByVal subject As String, ByVal body As String, _
ByVal location As String, ByVal startDate As DateTime, _
ByVal endDate As DateTime, ByVal setReminder As Boolean, _
ByVal reminderDuration As TimeSpan)

Private dtFormat As String = "yyyyMMddTHHmmssZ"
Dim sb As New StringBuilder()

sb.AppendLine("BEGIN:VCALENDAR")
sb.AppendLine("PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN")
sb.AppendLine("VERSION:2.0")
sb.AppendLine("METHOD:pUBLISH")
sb.AppendLine("BEGIN:VEVENT")
sb.AppendFormat("ATTENDEE;CN=""{0}"";ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:{1}" & vbCrLf, _toName, _toAddress)
sb.AppendFormat("ORGANIZER:MAILTO:{0}" & vbCrLf, _fromAddress)
sb.AppendFormat("DTSTART:{0}" & vbCrLf, _startDate.ToUniversalTime.ToString(dtFormat))
sb.AppendFormat("DTEND:{0}" & vbCrLf, _endDate.ToUniversalTime.ToString(dtFormat))
sb.AppendFormat("LOCATION:{0}" & vbCrLf, _location)
sb.AppendLine("TRANSP:OPAQUE")
sb.AppendLine("SEQUENCE:0")
sb.AppendFormat("UID:{0}" & vbCrLf, DateTime.Now.Ticks.ToString("x") & "-" _
& _fromAddress & Guid.NewGuid().ToString)
sb.AppendFormat("DTSTAMP:{0}" & vbCrLf, DateTime.Now.ToUniversalTime.ToString(dtFormat))
sb.AppendFormat("DESCRIPTION:{0}" & vbCrLf, _body)
sb.AppendFormat("SUMMARY:{0}" & vbCrLf, _subject)
sb.AppendFormat("PRIORITY:{0}" & vbCrLf, 5)
sb.AppendLine("X-MICROSOFT-CDO-IMPORTANCE:1")
sb.AppendLine("CLASS:pUBLIC")
If _setReminder Then
sb.AppendLine("BEGIN:VALARM")
sb.AppendFormat("TRIGGER:p{0}DT{1}H{2}M" & vbCrLf, _reminderDuration.Days, _
_reminderDuration.Hours, _reminderDuration.Minutes)
sb.AppendLine("ACTION:DISPLAY")
sb.AppendLine("DESCRIPTION:Reminder")
sb.AppendLine("END:VALARM")
End If
sb.AppendLine("END:VEVENT")
sb.AppendLine("END:VCALENDAR")

Vlad, that for pulling me on the right track!!
Josh
 
Joined
May 15, 2007
Messages
3
Reaction score
0
Heheh, damn those smileys. Nice post, 7 months on and its helped me too!

Spent ages searching for this, so cheers!
 
Joined
Oct 18, 2007
Messages
1
Reaction score
0
vcalendar email header problem

Hi Josh and Timmy,

I have been struggling with the same problem for the past 3 days. I request you to please, help me. I had written it just the way its been explained above. Here is my code. But it still sends the email with the first header as "text/calendar" but another header overwriting with "text/plain". I cant understand what the problem is. Your help is greatly appreciated.

public static bool buildVCalendarTrial(string toAddress, string toName, string fromAddress, string fromName, string subject, string body, string location, DateTime startDate, DateTime endDate, bool setReminder)
{
bool sentMail = false;
try
{
string dtFormat = "yyyyMMddTHHmmssZ";
StringBuilder sb = new StringBuilder();
sb.AppendLine("BEGIN:VCALENDAR");
sb.AppendLine("PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN");
sb.AppendLine("VERSION:2.0");
sb.AppendLine("METHODUBLISH");
sb.AppendLine("BEGIN:VEVENT");
sb.AppendFormat("ATTENDEE;CN={0};ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:{1}\n", toName, toAddress);
sb.AppendFormat("ORGANIZER:MAILTO:{0}\n", fromAddress);
sb.AppendFormat("DTSTART:{0}\n", startDate.ToUniversalTime().ToString(dtFormat));
sb.AppendFormat("DTEND:{0}\n", endDate.ToUniversalTime().ToString(dtFormat));
sb.AppendFormat("LOCATION:{0}\n", location);
sb.AppendLine("TRANSP:OPAQUE");
sb.AppendLine("SEQUENCE:0");
sb.AppendFormat("UID:{0}\n", DateTime.Now.Ticks.ToString("x") + "-" + fromAddress + Guid.NewGuid().ToString());
sb.AppendFormat("DTSTAMP:{0}\n", DateTime.Now.ToUniversalTime().ToString(dtFormat)) ;
sb.AppendFormat("DESCRIPTION;ENCODING=QUOTED-PRINTABLE:{0}=0D=0A\n", body);
sb.AppendFormat("SUMMARY;ENCODING=QUOTED-PRINTABLE:{0}=0D=0A:{0}\n", subject);
sb.AppendFormat("PRIORITY:{0}\n", 3);
sb.AppendLine("X-MICROSOFT-CDO-IMPORTANCE:1");
sb.AppendLine("CLASSUBLIC");

sb.AppendLine("END:VEVENT");
sb.AppendLine("END:VCALENDAR");
sendVCSEmail(fromAddress, toAddress, "", "", subject, sb.ToString());
sentMail = true;
}
catch (Exception excep)
{
}
return sentMail;

}


public static void sendVCSEmail(string fromAddr, string toAddr, string ccAddr, string bccAddr, string subject, string body)
{
MailMessage msgMail = new MailMessage();

msgMail.To.Add(toAddr);
if (!common.isNull(ccAddr)) msgMail.CC.Add(ccAddr);
if (!common.isNull(bccAddr)) msgMail.Bcc.Add(ccAddr);
msgMail.From = new MailAddress(fromAddr);


msgMail.Headers.Add("Content-Type", "text/calendar; method=REQUEST; charset=US-ASCII");

msgMail.Body = body;

System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["mailServer"].ToString();
smtp.Send(msgMail);

}

Thanks in advance,

Chandra.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top