C# 发送带有文件附件C的会议请求#

C# 发送带有文件附件C的会议请求#,c#,outlook,email-attachments,C#,Outlook,Email Attachments,我需要发送Outlook 2013的会议请求,并附上C#格式的文件附件。 我已经有工作代码发送请求,但我真的不知道如何添加附件到这个 MailMessage message = new MailMessage(); message.To.Add("someMail"); message.Subject = "Test"; message.From = new MailAddress("myMail"); message.Bo

我需要发送Outlook 2013的会议请求,并附上C#格式的文件附件。 我已经有工作代码发送请求,但我真的不知道如何添加附件到这个

    MailMessage message = new MailMessage();
        message.To.Add("someMail");
        message.Subject = "Test";
        message.From = new MailAddress("myMail");
        message.Body = "Test Test";
        SmtpClient smtp = new SmtpClient("mySmtp");

        StringBuilder str = new StringBuilder();
        str.AppendLine("BEGIN:VCALENDAR");
        str.AppendLine("VERSION:2.0");
        str.AppendLine("METHOD:REQUEST");
        str.AppendLine("BEGIN:VEVENT");
        str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmZ}", DateTime.UtcNow));

        str.AppendLine(string.Format("DTSTART:{0}", startTime));
        str.AppendLine(string.Format("DTEND:{0}", endTime));

        str.AppendLine("LOCATION: Zürich");
        str.AppendLine(string.Format("UID:{0}", Guid.NewGuid()));
        str.AppendLine(string.Format("DESCRIPTION:{0}", message.Body));
        str.AppendLine(string.Format("X-ALT-DESC;FMTTYPE=text/html:{0}", message.Body));
        str.AppendLine(string.Format("SUMMARY:{0}", message.Subject));
        str.AppendLine(string.Format("ORGANIZER:MAILTO:{0}", message.From.Address));

        str.AppendLine(string.Format("ATTENDEE;CN=\"{0}\";RSVP=TRUE:mailto:{1}", message.To[0].DisplayName, message.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 ct = new System.Net.Mime.ContentType("text/calendar");
        ct.Parameters.Add("method", "REQUEST");
        AlternateView avCal = AlternateView.CreateAlternateViewFromString(str.ToString(), ct);
        message.AlternateViews.Add(avCal);

        smtp.Send(message);
我希望有人能帮忙 谢谢你,你可以试试下面的代码

MailMessage msg = new MailMessage();
byte[] byteArray = Encoding.UTF8.GetBytes(str.ToString());
Stream contentStream = new MemoryStream(byteArray);
Attachment attachment = new Attachment(contentStream, "calendar.ics", "text/calendar");
attachment.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
msg.Attachments.Add(attachment);