+1 vote
by (130 points)

Hi,
I am trying to send a calendar with image showing in the message body. But when I tried to send one. I received the message in text only. All the images and html are not displayed in the message body..Can anyone help me on this? Thanks! Note the msg.body contains html and images.

string startTime1 = Convert.ToDateTime(startTime).ToString("yyyyMMddTHHmmssZ");
string endTime1 = Convert.ToDateTime(endTime).ToString("yyyyMMddTHHmmssZ");
SmtpClient sc = new SmtpClient("");

MailMessage msg = new MailMessage();

msg.From = new MailAddress("", "HR Self Service");
msg.To.Add(new MailAddress(emailto));
msg.Subject = "Holiday Approval";
msg.Body = emailbody;

StringBuilder str = new StringBuilder();
str.AppendLine("BEGIN:VCALENDAR");

//PRODID: identifier for the product that created the Calendar object
str.AppendLine("PRODID:-//ABC Company//Outlook MIMEDIR//EN");
str.AppendLine("VERSION:2.0");
str.AppendLine("METHOD:REQUEST");

str.AppendLine("BEGIN:VEVENT");

str.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", startTime1));//TimeZoneInfo.ConvertTimeToUtc("BeginTime").ToString("yyyyMMddTHHmmssZ")));
str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", DateTime.UtcNow));
str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", endTime1));//TimeZoneInfo.ConvertTimeToUtc("EndTime").ToString("yyyyMMddTHHmmssZ")));
str.AppendLine(string.Format("LOCATION: {0}", "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("STATUS:CONFIRMED");
str.AppendLine("BEGIN:VALARM");
str.AppendLine("TRIGGER:-PT15M");
str.AppendLine("ACTION:Accept");
str.AppendLine("DESCRIPTION:Reminder");
str.AppendLine("X-MICROSOFT-CDO-BUSYSTATUS:BUSY");
str.AppendLine("END:VALARM");
str.AppendLine("END:VEVENT");
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("END:VCALENDAR");
System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType("text/calendar");
ct.Parameters.Add("method", "REQUEST");
ct.Parameters.Add("name", "meeting.ics");
AlternateView aCal = AlternateView.CreateAlternateViewFromString(str.ToString(), ct);
msg.AlternateViews.Add(aCal);
sc.Send(msg);

1 Answer

0 votes
by (58.9k points)
edited by

Hello,

with Rebex Secure Mail component you can construct and save or send emails with calendar appointments. Please see below or visit this forum post for a complete code example on how to create email with calendar appointments with Rebex.

Please note that Outlook requires the HTML body to be set for calendar emails to work properly.

Please also note that this is support forum for users of Rebex components, whereas your code uses standard .NET classes (e.g. SmtpClient and System MailMessage). A better place to ask about Rebex unrelated questions is a general programmers forum, like stackoverflow.com.

Your code also does not set any embedded picture at all. For your convenience here is a sample code that creates the calendar appointment. It also includes an embedded picture within the html body of the email:

// construct the message
MailMessage message = new MailMessage();

message.From = new MailAddress("name@domain.com", "sender name");
message.To.Add(new MailAddress("name.surname@domain.com"));
message.BodyText = "Holiday Approval";
message.BodyHtml = "<p>Holiday Approval with picture: <img src='cid:0123@rebex.net'></p>";

//add a linked picture into the message resources
LinkedResource picture = new LinkedResource("image.png", "image/png");
picture.ContentId = "0123@rebex.net"; //choose unique ID
message.Resources.Add(picture);


// construct the calendar view
AlternateView appointment = new AlternateView();
appointment.SetContent(GetData(message), "text/calendar");
appointment.ContentType.Parameters.Add("method", "REQUEST");
message.AlternateViews.Add(appointment);

message.Save("mail-with-calendar-with-embedded-picture.eml");

string GetData(MailMessage mail)
{
    DateTime startTime = new DateTime(2016, 8, 18, 0, 15, 0);
    DateTime endTime = new DateTime(2016, 8, 18, 0, 35, 0);

    string startTime1 = Convert.ToDateTime(startTime).ToString("yyyyMMddTHHmmssZ");
    string endTime1 = Convert.ToDateTime(endTime).ToString("yyyyMMddTHHmmssZ");

    StringBuilder appointmentData = new StringBuilder();
    appointmentData.AppendLine("BEGIN:VCALENDAR");
    appointmentData.AppendLine("PRODID:-//ABC Company//Outlook MIMEDIR//EN");
    appointmentData.AppendLine("VERSION:2.0");
    appointmentData.AppendLine("METHOD:REQUEST");

    appointmentData.AppendLine("BEGIN:VEVENT");

    appointmentData.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", startTime1));//TimeZoneInfo.ConvertTimeToUtc("BeginTime").ToString("yyyyMMddTHHmmssZ")));
    appointmentData.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", DateTime.UtcNow));
    appointmentData.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", endTime1));//TimeZoneInfo.ConvertTimeToUtc("EndTime").ToString("yyyyMMddTHHmmssZ")));
    appointmentData.AppendLine(string.Format("LOCATION: {0}", "Location"));
    appointmentData.AppendLine(string.Format("UID:{0}", Guid.NewGuid()));
    appointmentData.AppendLine(string.Format("DESCRIPTION:{0}", mail.BodyText));
    appointmentData.AppendLine(string.Format("X-ALT-DESC;FMTTYPE=text/html:{0}", mail.BodyText));
    appointmentData.AppendLine(string.Format("SUMMARY:{0}", mail.Subject));
    appointmentData.AppendLine("STATUS:CONFIRMED");
    appointmentData.AppendLine("BEGIN:VALARM");
    appointmentData.AppendLine("TRIGGER:-PT15M");
    appointmentData.AppendLine("ACTION:Accept");
    appointmentData.AppendLine("DESCRIPTION:Reminder");
    appointmentData.AppendLine("X-MICROSOFT-CDO-BUSYSTATUS:BUSY");
    appointmentData.AppendLine("END:VALARM");
    appointmentData.AppendLine("END:VEVENT");
    appointmentData.AppendLine(string.Format("ORGANIZER:MAILTO:{0}", mail.From[0].Address));
    appointmentData.AppendLine(string.Format("ATTENDEE;CN=\"{0}\";RSVP=TRUE:mailto:{1}", mail.To[0].DisplayName, mail.To[0].Address));
    appointmentData.AppendLine("END:VCALENDAR");
    return appointmentData.ToString();
}
...