Send email to Outlook with ics meeting appointment
Asked Answered
W

2

18

I want to send an email with appointment\meeting (ICS) to Outlook client. When the user receive the email he should accept the meeting invitation and automatically the meeting goes to the calendar and the email is automatically deleted.

I'm using this code:

public void Sendmail_With_IcsAttachment()
{

    MailMessage msg = new MailMessage();
    //Now we have to set the value to Mail message properties

    //Note Please change it to correct mail-id to use this in your application
    msg.From = new MailAddress("[email protected]", "ABC");
    msg.To.Add(new MailAddress("[email protected]", "BCD"));
    msg.CC.Add(new MailAddress("[email protected]", "DEF"));// it is optional, only if required
    msg.Subject = "Send mail with ICS file as an Attachment";
    msg.Body = "Please Attend the meeting with this schedule";

    // Now Contruct the ICS file using string builder
    StringBuilder str = new StringBuilder();
    str.AppendLine("BEGIN:VCALENDAR");
    str.AppendLine("PRODID:-//Schedule a Meeting");
    str.AppendLine("VERSION:2.0");
    str.AppendLine("METHOD:REQUEST");
    str.AppendLine("BEGIN:VEVENT");
    str.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", DateTime.Now.AddMinutes(+330)));
    str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", DateTime.UtcNow));
    str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", DateTime.Now.AddMinutes(+660)));
    str.AppendLine("LOCATION: " + this.Location);
    str.AppendLine(string.Format("UID:{0}", Guid.NewGuid()));
    str.AppendLine(string.Format("DESCRIPTION:{0}", msg.Body));
    str.AppendLine(string.Format("X-ALT-DESC;FMTTYPE=text/html:{0}", msg.Body));
    str.AppendLine(string.Format("SUMMARY:{0}", msg.Subject));
    str.AppendLine(string.Format("ORGANIZER:MAILTO:{0}", msg.From.Address));

    str.AppendLine(string.Format("ATTENDEE;CN=\"{0}\";RSVP=TRUE:mailto:{1}", msg.To[0].DisplayName, msg.To[0].Address));

    str.AppendLine("BEGIN:VALARM");
    str.AppendLine("TRIGGER:-PT15M");
    str.AppendLine("ACTION:DISPLAY");
    str.AppendLine("DESCRIPTION:Reminder");
    str.AppendLine("END:VALARM");
    str.AppendLine("END:VEVENT");
    str.AppendLine("END:VCALENDAR");

    //Now sending a mail with attachment ICS file.                     
    System.Net.Mail.SmtpClient smtpclient = new System.Net.Mail.SmtpClient();
    smtpclient.Host = "localhost"; //-------this has to given the Mailserver IP

    smtpclient.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

    System.Net.Mime.ContentType contype = new System.Net.Mime.ContentType("text/calendar");
    contype.Parameters.Add("method", "REQUEST"); 
    contype.Parameters.Add("name", "Meeting.ics");
    AlternateView avCal = AlternateView.CreateAlternateViewFromString(str.ToString(), contype);
    msg.AlternateViews.Add(avCal);
    smtpclient.Send(msg); 
}

The mail is correctly sent but in outlook (I'm testing it with outlook 2010) it shows the body, location,data, and it shows the calendar but above the calendar in the mail I see "Impossible to find the meeting on the calendar" and the "Accept","Decline"" buttons are disabled!

I have tried other solutions and found on the web for ex. DDAY.Ical but I haven't found any example to use it.

Weig answered 29/3, 2014 at 17:33 Comment(5)
FYI, both your MailMessage and SmtpClient need to be in using blocks: using (MailMessage msg = new MailMessage()){... using (SmtpClient smtpClient = new SmtpClient()){ ... smtpClient.Send(msg);}}Periderm
ok thanks but I don't think It's the issueWeig
Whether it's the issue or not, you should do that - I've seen mail not be delivered for 2 minutes, until the garbage collector got around to cleaning up.Periderm
Could you add the full resulting MIME message to your post ?Astragal
How can I get full resulting MIME message?Weig
P
17

After a lot of digging i found the solution:

You need to set the content-class of your eMail to

Content-class: urn:content-classes:calendarmessage

so add this to your code and it should work

msg.Headers.Add("Content-class", "urn:content-classes:calendarmessage");
Puga answered 20/3, 2015 at 12:37 Comment(3)
This situation happened to me upon an Exchange upgrade to 2010. I was using two AlternativeViews (1 for HTML, 1 for the calendar). To fix the problem I had to order them so that the HTML one went first AlternateView HTML = AlternateView.CreateAlternateViewFromString(body, new System.Net.Mime.ContentType("text/html"));msg.AlternateViews.Add(HTML); AlternateView avCal = AlternateView.CreateAlternateViewFromString(str.ToString(),contype); msg.AlternateViews.Add(avCal ); I know this was not the problem for this instance, just mentioning it in case somebody else had the same problem.Consociate
Thanks, I need to use Utc Date also in DTSTART and DTENDWeiss
Adding content-class is exactly what it needs, tks for sharing itCystocarp
I
0

i think you have used same email id in both msg.From and msg.To

msg.From = new MailAddress("[email protected]", "ABC");
msg.To.Add(new MailAddress("[email protected]", "BCD"));

i have faced same issue but after taking different email get resolve.

Illtimed answered 9/6, 2016 at 6:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.