Search code examples
c#email-attachmentsicalendarmailmessage

Send email with Attachments and Calendar Invite


I am working on service where I want to send emails with attachments(image, pdf, docs) and ics calendar invitation as an alternateview. Whenever I am sending mail with attachments the ics calendar invitation is missing in a mail but when I don't have any attachments in mail then I am able to receive calendar invitation.

Here is my code

if (Convert.ToString(DR["ATTACHMENT"]) != "")   //mail_att
{
    message.Attachments.Add(new Attachment(DR["ATTACHMENT"].ToString()));
}
if (Convert.ToString(DR["SCRIPT"]) != "")   //Mail_Sub
{
    System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType("text/calendar");
    ct.Parameters.Add("method", "REQUEST");
    ct.Parameters.Add("name", "meeting.ics");
    AlternateView avCal = AlternateView.CreateAlternateViewFromString(Convert.ToString(DR["SCRIPT"]), ct);
    message.AlternateViews.Add(avCal);
}

MailMessage object message is showing Attachments and AlternateView counts, still calendar invite is missing in mails whenever I add attachments.


Solution

  • If you are sending using straight SMTP, the attachments must be embedded inside the VEVENT part in an ATTACH field:

    ATTACH;ENCODING=BASE64;VALUE=BINARY;X-FILENAME=test.txt:NBIBANyGrhFvX6xL...

    Create an appointment with an attachment in Outlook and save it as an ICS file from Outlook as a test then open that ICS file in notepad then you can check how the script looks like, makes changes in your AlternateView calendar script