使用 MailKit 和 Stringbuilder 发送 ics 文件

Sending ics file with MailKit and Stringbuilder

我正在尝试发送带有日历邀请的电子邮件,作为 ics 文件附件。我正在使用 Mailkit,因为我的理解是 system.net.mail 已被弃用。

玩过之后,我了解到“健美运动员”正在寻找文件路径,但这不是我在这里尝试做的,所以我有点迷路了。

网络上的很多文档似乎已经过时了...

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", "nic-fleetwood@behr.travel"));
            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("nic-fleetwood@behr.travel", "foobar");

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

        return View();
    }

AttachmentCollection有几种添加指定附件的方法。

你需要用到这个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);
    }
}

Add(string, byte[]):

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