I need to send meeting invitations that sometimes can include .pdf attachments. So i'm building System.Net.Mail.MailMessage with Alternative view (for iCalendar content), with Body and with an attachment if it exists. I need that most popular mail clients (that support calendars) be able deal with my invitation emails.
Here is my code that is pretty working for:
gmail (web/mobile app) with/without pdf attachment
yahoo mail (web/mobile app) with/without pdf attachment
outlook (desktop) without pdf attachment only
public virtual MailMessage CreateMailMessage(string content, Meeting meeting)
{
MailMessage mailMessage = new MailMessage();
mailMessage.From = _mailsAddressesProvider.GetFromEmaiMailAddress(meeting);
mailMessage.To.AddRange(_mailsAddressesProvider.GetToEmaiMailAddresses(meeting));
mailMessage.CC.AddRange(_mailsAddressesProvider.GetCCEmailAddresses(meeting));
mailMessage.Bcc.AddRange(_mailsAddressesProvider.GetBCCEmailAddresses(meeting));
mailMessage.Subject = GetMessageSubject(meeting);
mailMessage.Body = GetMessageBody(meeting);
mailMessage.IsBodyHtml = IsBodyHtml();
ContentType contentType = new ContentType("text/calendar");
contentType.Parameters.Add("method", Method);
contentType.Parameters.Add("name", AttachmentName);
AlternateView avCal = AlternateView.CreateAlternateViewFromString(content, contentType);
mailMessage.AlternateViews.Add(avCal);
AddAttachments(mailMessage, meeting);
return mailMessage;
}
public virtual bool AddAttachments(MailMessage mailMessage, Meeting meeting)
{
byte[] meetingMaterialsBytes = _meetingMaterialsRepository.GetMeetingMaterails(meeting);
if (meetingMaterialsBytes == null || meetingMaterialsBytes.Length == 0)
{
return false;
}
ContentType contentType = new ContentType(MediaTypeNames.Application.Pdf);
Attachment attach = new Attachment(new MemoryStream(meetingMaterialsBytes), contentType);
attach.ContentDisposition.FileName = "MeetingMaterials.pdf";
attach.ContentDisposition.Inline = true;
attach.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
attach.ContentId = "MeetingMaterials";
mailMessage.Attachments.Add(attach);
return true;
}
So the issue is when email with attachment is being opened in Outlook.
Here is how such an email is opened in Outlook without pdf attachment - OK:
Here is how such an email is opened in Outlook with pdf attachment - OK: Probably there is no issue with iCalendar (ics) content but with System.Net.Mail.MailMessage object.
So the problem is that I need Outlook to open an email with attachment as a meeting invitation but not as a ordinary email.
Any suggestions about what can be wrong with my MailMessage parts? Thanks in advance.