使用C#将ICS文件作为附件发送 [英] sending ICS files as an attachment using C#
本文介绍了使用C#将ICS文件作为附件发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
HI全部,
我试图通过从C#创建ics文件和电子邮件将.ics文件作为附件发送给用户自己编码。我能够将ICS文件保存到本地驱动器并附加到电子邮件。
我想发送ics文件而不保存到任何驱动器。
帮助我有什么办法从C#页面本身发送ics文件......
谢谢
HI All,
I am trying to send .ics file as an attachment to user by creating ics file and email from C# code it self.I am able to send ICS file saving in to a local drive and attaching to email.
I would like to send ics file with out saving in to any drive.
help me is there any way to send ics file from the C# page itself...
Thanks
推荐答案
解决方案将Calender Invite作为附件(.ics文件)发送给最终用户。
代码开始:
static void Main(string [] args)
{
Console.WriteLine(Welcome);
var client = new SmtpClient(smtp.gmail.com,587)
{
Credentials = new NetworkCredential(myemail @ gmail。 com,密码),
EnableSsl = true,
超时= 100000
};
控制台.WriteLine(连接成功);
//现在构建IC使用字符串生成器的S文件
MailMessage msg = new MailMessage();
msg.From = new MailAddress(fromemail@gmail.com,发件人姓名);
msg.To.Add(new MailAddress(tomail@gmail.com,To Name));
msg.Subject =用ICS文件发送邮件作为附件;
msg.Body =请按照这个时间表参加会议;
StringBuilder str = new StringBuilder();
str.AppendLine(BEGIN:VCALENDAR);
str.AppendLine(PRODID: - //安排会议);
str.AppendLine(VERSION :2.0);
str.AppendLine(METHOD:REQUEST);
str.AppendLine(BEGIN:VEVENT);
str.AppendLine(string.Format(DTSTART:{0:yyyyMMddTHHmmssZ},DateTime.Now.AddMinutes(+10)));
str.AppendLine(string.Format(DTSTAMP: {0:yyyyMMddTHHmmssZ},DateTime.UtcNow));
str.AppendLine(string.Format(DTEND:{0:yyyyMMddTHHmmssZ},DateTime.Now.AddMinutes(+60))) ;
str.AppendLine(LOCATION:CGI);
str.AppendLine(string.Format(UID:{0},Guid.NewGuid()));
str.AppendLine(string.Format(DESCRIPTION:{0} ,msg.Body));
str.AppendLine(string.Format(X-ALT-DESC; FMTTYPE = text / html:{0},msg.Body));
str.AppendLine(string.Format(SUMMARY:{0},msg.Subject));
str.AppendLine(string.Format(ORGANIZER:MAILTO:{ 0},msg.From.Address));
str.AppendLine(string.Format(ATTENDEE; CN = \{0} \ ; RSVP = TRUE:mailto:{1},msg.To [0] .DisplayName,msg.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 contype = new System.Net.Mime.ContentType(text / calendar);
contype.Parameters.Add(me thod,REQUEST);
contype.Parameters.Add(name,Meeting.ics);
msg.Attachments.Add(new Attachment(str.ToString(),contype) );
client.Send(msg);
Console.WriteLine(已发送);
Console.ReadLine();
}
Solution send Calender Invite as an attachment(.ics file) to end user.
Code Starts:
static void Main(string[] args)
{
Console.WriteLine("Welcome");
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("myemail@gmail.com", "password"),
EnableSsl = true,
Timeout =100000
};
Console.WriteLine("connection success");
// Now Contruct the ICS file using string builder
MailMessage msg = new MailMessage();
msg.From = new MailAddress("fromemail@gmail.com", "Sender's Name");
msg.To.Add(new MailAddress("tomail@gmail.com", "To Name"));
msg.Subject = "Send mail with ICS file as an Attachment";
msg.Body = "Please Attend the meeting with this schedule";
StringBuilder str = new StringBuilder();
str.AppendLine("BEGIN:VCALENDAR");
str.AppendLine("PRODID:-//Schedule a Meeting");
str.AppendLine("VERSION:2.0");
str.AppendLine("METHOD:REQUEST");
str.AppendLine("BEGIN:VEVENT");
str.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", DateTime.Now.AddMinutes(+10)));
str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", DateTime.UtcNow));
str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", DateTime.Now.AddMinutes(+60)));
str.AppendLine("LOCATION: CGI");
str.AppendLine(string.Format("UID:{0}", Guid.NewGuid()));
str.AppendLine(string.Format("DESCRIPTION:{0}", msg.Body));
str.AppendLine(string.Format("X-ALT-DESC;FMTTYPE=text/html:{0}", msg.Body));
str.AppendLine(string.Format("SUMMARY:{0}", msg.Subject));
str.AppendLine(string.Format("ORGANIZER:MAILTO:{0}", msg.From.Address));
str.AppendLine(string.Format("ATTENDEE;CN=\"{0}\";RSVP=TRUE:mailto:{1}", msg.To[0].DisplayName, msg.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 contype = new System.Net.Mime.ContentType("text/calendar");
contype.Parameters.Add("method", "REQUEST");
contype.Parameters.Add("name", "Meeting.ics");
msg.Attachments.Add(new Attachment(str.ToString(), contype));
client.Send(msg);
Console.WriteLine("Sent");
Console.ReadLine();
}
这篇关于使用C#将ICS文件作为附件发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文