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 system.net.mail 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", "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="87e9eee4aae1ebe2e2f3f0e8e8e3c7e5e2eff5a9f3f5e6f1e2eb">[email protected]</a>"));
            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()
            str.AppendLine("BEGIN:VCALENDAR");
            str.AppendLine("PRODID:-//RYNE MOVING//EN");
            str.AppendLine("VERSION:2.0");
            str.AppendLine("METHOD:PUBLISH");
            //THE EVENT
            str.AppendLine("BEGIN:VEVENT");
            str.AppendLine("DTSTART:20210215T100000");
            str.AppendLine("DTEND:20210215T110000");
            str.AppendLine("DTSTAMP:" + DateTime.Now);
            str.AppendLine("UID:" + Guid.NewGuid());
            str.AppendLine("CREATED:" + DateTime.Now);
            str.AppendLine("X-ALT-DESC;FMTTYPE=text/html:");
            //sb.AppendLine("DESCRIPTION:" + res.Details);
            str.AppendLine("LAST-MODIFIED:" + DateTime.Now);
            str.AppendLine("LOCATION:NYC");
            str.AppendLine("SEQUENCE:0");
            str.AppendLine("STATUS:CONFIRMED");
            str.AppendLine("SUMMARY:");
            str.AppendLine("TRANSP:OPAQUE");
            str.AppendLine("END:VEVENT");

            str.AppendLine("END:VCALENDAR");

            emailBody.Attachments.Add(str.ToString());

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

                // Note: only needed if the SMTP server requires authentication
                client.Authenticate("<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="026c6b612f646e676776756d6d664260676a702c76706374676e">[email protected]</a>", "foobar");

                client.Send(message);
                client.Disconnect(true);
            }
        }

        return View();
    }

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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))
    {
        writer.Write(ics.ToString());

        writer.Flush();
        stream.Position = 0;

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

or Add(string, byte[]):

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x