【问题标题】:Sending ics file with MailKit and Stringbuilder使用 MailKit 和 Stringbuilder 发送 ics 文件
【发布时间】:2021-01-04 03:02:36
【问题描述】:

我正在尝试发送一封带有日历邀请的电子邮件,作为 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();
    }

【问题讨论】:

    标签: c# asp.net asp.net-core model-view-controller mailkit


    【解决方案1】:

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

    【讨论】:

    • 所以当我使用 ".Add("calendar.ics", stream);"我得到“ArgumentException:无法读取流。(参数'流')”。当我使用您的编辑时,对于“编码”,电子邮件去了,但没有附件
    • @NickFleetwood 只是检查一下:你做了 message.Body = emailbody.ToMessageBody (); 吗? (详情见:stackoverflow.com/questions/37853903/…
    猜你喜欢
    • 2020-04-19
    • 2021-05-03
    • 2023-03-18
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 2019-07-03
    • 2018-12-07
    • 1970-01-01
    相关资源
    最近更新 更多