简体   繁体   English

使用memorystream将ICS作为邮件附件发送

[英]Sending ICS as Mail Attachment using memorystream

I am creating a ics file in my controller. 我正在控制器中创建一个ics文件。 Using asp.net core 2.0 mvc. 使用asp.net core 2.0 mvc。 I now have the content and everything done and I can send the ics file as a mail attachment. 现在,我已经完成了内容和所有工作,可以将ics文件作为邮件附件发送。 But I dont want to create a file locally on my pc, I want to use memorystream or something without a physical path. 但是我不想在我的PC上本地创建文件,我想使用memorystream或没有物理路径的东西。

This is how im creating the ics content: 这是即时消息创建ics内容的方式:


string schLocation = "Nord 1";
            string schSubject = "Badge-Austausch";
            string schDescription = "Termin zum Austauschen des Badges";
            System.DateTime schBeginDate = Convert.ToDateTime(DateOwnRegister);
            System.DateTime schEndDate = Convert.ToDateTime(schBeginDate.AddMinutes(30));
            string[] contents = { "BEGIN:VCALENDAR",
                               "PRODID:-//Flo Inc.//FloSoft//EN",
                               "BEGIN:VEVENT",
                               "DTSTART:" + schBeginDate.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z"),
                               "DTEND:" + schEndDate.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z"),
                               "LOCATION:" + schLocation,
                          "DESCRIPTION;ENCODING=QUOTED-PRINTABLE:" + schDescription,
                               "SUMMARY:" + schSubject, "PRIORITY:3",
                          "END:VEVENT", "END:VCALENDAR" 

This is how Ive been trying to use the memorystream: 这就是我一直试图使用memorystream的方式:

char[] charArray = contents.SelectMany(x => x.ToCharArray()).ToArray();
            byte[] byteArray = Encoding.UTF8.GetBytes(charArray);
            System.IO.MemoryStream mStream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(charArray));
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            System.IO.StreamWriter writer = new System.IO.StreamWriter(ms);
            writer.Write(byteArray);

And this is how i send it as attachment. 这就是我将其作为附件发送的方式。 The sending works but i cant open the file in outlook. 发送工作,但我无法在Outlook中打开文件。 So it doesnt work as a calendar invitation; 因此,它不能用作日历邀请;

System.Net.Mime.ContentType contype = new System.Net.Mime.ContentType("text/calendar");
                System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(ms, contype);
                attach.ContentDisposition.FileName = "myFile.ics";
                msg.Attachments.Add(attach);
                sc.Send(msg);

Happy about any ideas or solutions on how I could get this to work. 对于任何有关如何使它起作用的想法或解决方案感到高兴。 As I said the "contents" worked when I sent the file from my local data but the sending without local paths didnt work yet. 正如我说的那样,当我从本地数据发送文件时,“内容”起作用了,但是没有本地路径的发送还没有起作用。

Got it to work using string builder. 使用字符串生成器使其正常工作。

 StringBuilder sb = new StringBuilder();
            foreach (var line in contents)
            {
                sb.AppendLine(line);

            }
            var calendarBytes = Encoding.UTF8.GetBytes(sb.ToString());
            MemoryStream memorystream = new MemoryStream(calendarBytes);
            System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(memorystream, "event.ics", "text/calendar");```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM