How to send message correctly in Outlook with ics file and calendar part?

I need to send an email with a portion of the meeting calendar as well as the same attachment. When I do this with comments that implement adding an attachment (as in the code below), I get a message in Outlook without the ics file, but with a nicely displayed calendar; otherwise, when the add is added, I can only see the ics file and the message body, but the calendar part is missing.

static void Main(string[] args)
    {
        Console.WriteLine("Welcome");
        var client = new SmtpClient();

        Console.WriteLine("connection success");
        // Now Contruct the ICS file using string builder
        MailMessage msg = new MailMessage();
        msg.From = new MailAddress("fromemail@gmail.com", "Sender Name");
        msg.To.Add(new MailAddress("example@outlook.com", "To Name"));
        msg.Subject = "Send mail with ICS file as an Attachment";
        msg.Body = "Please Attend the meeting with this schedule";
        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(+10)));
        str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", DateTime.UtcNow));
        str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", DateTime.Now.AddMinutes(+60)));
        str.AppendLine("LOCATION: CGI");
        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");

        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);

        //Byte[] bytes = System.Text.Encoding.ASCII.GetBytes(str.ToString());
        //var ms = new System.IO.MemoryStream(bytes);            
        //msg.Attachments.Add(new Attachment(ms, contype));

        client.Send(msg);
        Console.WriteLine("Sent");
        Console.ReadLine();
    }

      

+3


source to share





All Articles