Sending ics file with MailKit and Stringbuilder

I’m trying to send an email with a calendar invite, as an ics file attachment. I’m using Mailkit, as my understanding is the is deprecated.

After playing with it, I understand that the "bodybuilder" is looking for a filepath, but that’s not what I’m trying to do here, and so I’m sort of lost.

Alot of the documentation on the web seems to be outdated…

public IActionResult ThrEmail(string sendto, string subject, string body)
        if (!String.IsNullOrEmpty(sendto))
            //construct mail
            var message = new MimeMessage();
            message.From.Add(new MailboxAddress("nick", "[email protected]"));
            message.To.Add(new MailboxAddress(sendto));
            message.Subject = subject;
            //message.Body = new TextPart("plain")
            //    Text = body

            BodyBuilder emailBody = new BodyBuilder();
            emailBody.TextBody = body;

            //ics file -- use yyyMMddTHHmmssZ format
            StringBuilder str = new StringBuilder();
            str.AppendLine("PRODID:-//RYNE MOVING//EN");
            //THE EVENT
            str.AppendLine("DTSTAMP:" + DateTime.Now);
            str.AppendLine("UID:" + Guid.NewGuid());
            str.AppendLine("CREATED:" + DateTime.Now);
            //sb.AppendLine("DESCRIPTION:" + res.Details);
            str.AppendLine("LAST-MODIFIED:" + DateTime.Now);



            //send the email
            using (var client = new SmtpClient())
                client.Connect("", 587, false);

                // Note: only needed if the SMTP server requires authentication
                client.Authenticate("[email protected]", "foobar");


        return View();

Stack Overflow Asked by Nick Fleetwood on February 23, 2021

1 Answers

One Answer

AttachmentCollection has several methods to add the specified attachment.

You need to use this one Add(String, Stream):

var emailBody = new BodyBuilder();

var ics = new StringBuilder();
/* initialize calendar options */

using (var stream = new MemoryStream())
    using (var writer = new StreamWriter(stream))

        stream.Position = 0;

        emailBody.Attachments.Add("calendar.ics", stream);

or Add(string, byte[]):

emailBody.Attachments.Add("calendar.ics", Encoding.UTF8.GetBytes(ics.ToString()));

Correct answer by vladimir on February 23, 2021

Add your own answers!

Related Questions

Seperate strings with regex and panda

1  Asked on January 11, 2021 by sara-daniel


Login into SAP using admin user fails

1  Asked on January 11, 2021 by gram77


Using LIKE with multiple columns in a table

3  Asked on January 10, 2021 by soldfor


Does the value of global variable persist in multiple API calls

2  Asked on January 10, 2021 by shubroto-shuvo


React Component not rendering to HTML

1  Asked on January 10, 2021 by red-apple


How can I count a pandas dataframe over duplications

4  Asked on January 10, 2021 by toby-chamberlain


javascript .eval() gives me undifined

2  Asked on January 10, 2021 by nourza


Loop for column names in python

3  Asked on January 10, 2021 by essegn


Return array of values from object based on array

2  Asked on January 9, 2021 by user4584963


How do I get out of ‘screen’ without typing ‘exit’?

5  Asked on January 9, 2021 by meder-omuraliev


How can I regroup ‘sublists’ in a list with Python?

2  Asked on January 9, 2021 by zhengrong


Ask a Question

Get help from others!

© 2022 All rights reserved.