如何将.ics文件作为邮件附件发送 [英] how to send .ics file as an mail attachment

查看:489
本文介绍了如何将.ics文件作为邮件附件发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我想将sample.ics文件作为邮件附件发送.我尝试了以下代码,但无法正常工作.

Hi all
I want to send sample.ics file as an mail attachment. I tried the below code but not working.

            MailMessage m = new MailMessage();
SmtpClient sc = new SmtpClient();
string schLocation = "Conference Room";
string schSubject = "Business visit discussion";
string schDescription = "Schedule description";
System.DateTime schBeginDate = Convert.ToDateTime("7/8/2011 10:00:00 AM");
System.DateTime schEndDate = Convert.ToDateTime("7/8/2011 10:00:00 PM");
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" };
System.IO.File.WriteAllLines("D:\\mail\\sample1.ics", contents);
            
m.From = new MailAddress("frmmail@gmail.com", "displayname");
m.To.Add(new MailAddress("tomail@gmail.com", "displayname");                
m.Subject = "Hi I have attached sample.ics file";
m.IsBodyHtml = true;
m.Body = "This is a Test Mail";
FileStream fs = new FileStream("D:\\mail\\sample1.ics", FileMode.Open, FileAccess.Read);
Attachment a = new Attachment(fs,"sample1.ics", MediaTypeNames.Application.Octet);
m.Attachments.Add(a);

sc.Host = "smtp.gmail.com";
sc.Port = 587;
sc.Credentials = new System.Net.NetworkCredential("from@gmail.com","Password");
sc.EnableSsl = true;
sc.Send(m);
Console.WriteLine("Sent");
Console.ReadLine();


如果我注释掉该行,我可以发送邮件
m.Attachments.Add(a);
谁能告诉我我在哪里做错了...


I can able to send mail if i comment out the line
m.Attachments.Add(a);
can anyone please tell me where i had done mistake...

推荐答案

您需要创建MailAttachment,而不仅仅是附件的附件传递对象.

看一下这个技巧:使用C#发送电子邮件带有或不带有附件:通用例程. [ ^ ]
You need to create MailAttachment and not just Attachment object for attachments to pass on.

Have a look at this tip: Sending an Email in C# with or without attachments: generic routine.[^]


请参阅以下链接
http://www.go4expert.com/forums/showthread.php?t=18599 [ ^ ]
see below links
http://www.go4expert.com/forums/showthread.php?t=18599[^]


这篇关于如何将.ics文件作为邮件附件发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆