gpt4 book ai didi

c# - 使用 C# 创建 ics 文件并发送带有附件的电子邮件

转载 作者:行者123 更新时间:2023-11-30 19:35:59 25 4
gpt4 key购买 nike

我需要发送附有 ics 文件的日历约会电子邮件,其中包含约会详细信息,我正在收到邮件,但缺少附件帮助我,有人可以解决这个问题

需要创建简单的 ics 文件并将其作为电子邮件的附件。

请看下面我试过的代码

   public IHttpActionResult cal(calendar objApptEmail)
{
MailMessage msg = new MailMessage();
//Now we have to set the value to Mail message properties

//Note Please change it to correct mail-id to use this in your application
msg.From = new MailAddress("ssss@gmail.com", "ABC");
msg.To.Add(new MailAddress("ssss@outlook.com", "BCD"));
// msg.CC.Add(new MailAddress("zzzzz@xyz.com", "DEF"));// it is optional, only if required
// msg.Headers.Add("Content-class", "urn:content-classes:calendarmessage");
msg.Subject = "Send mail with ICS file as an Attachment";
msg.Body = "Please Attend the meeting with this schedule";

// Now Contruct the ICS file using string builder
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(+330)));
str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", DateTime.UtcNow));
str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", DateTime.Now.AddMinutes(+660)));
str.AppendLine("LOCATION: " + objApptEmail.Location);
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");
AlternateView avCal = AlternateView.CreateAlternateViewFromString(str.ToString(), contype);
msg.AlternateViews.Add(avCal);

//Now sending a mail with attachment ICS file.
System.Net.Mail.SmtpClient smtpclient = new System.Net.Mail.SmtpClient();
smtpclient.Host = "smtp.gmail.com"; //-------this has to given the Mailserver IP
smtpclient.EnableSsl = true;
smtpclient.Credentials = new System.Net.NetworkCredential("dddd@gmail.com", "xxxxx");
smtpclient.Send(msg);
return Ok();

}

最佳答案

我删除了所有代码并在我的机器上测试后输入代码。这在我的情况下工作正常。在你的情况下,它不起作用尝试在 Gmail 中设置不太安全的应用程序(我看到你正在使用 gmail 发送电子邮件)

System.Net.Mail.MailMessage msg = new MailMessage("anirudhagupta01@example.com","anirudha@testing.com");

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(+330)));
str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", DateTime.UtcNow));
str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", DateTime.Now.AddMinutes(+660)));
str.AppendLine("LOCATION: " + "abcd");
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");

byte[] byteArray = Encoding.ASCII.GetBytes(str.ToString());
MemoryStream stream = new MemoryStream(byteArray);

Attachment attach = new Attachment(stream,"test.ics");

msg.Attachments.Add(attach);

System.Net.Mime.ContentType contype = new System.Net.Mime.ContentType("text/calendar");
contype.Parameters.Add("method", "REQUEST");
// contype.Parameters.Add("name", "Meeting.ics");
AlternateView avCal = AlternateView.CreateAlternateViewFromString(str.ToString(), contype);
msg.AlternateViews.Add(avCal);

//Now sending a mail with attachment ICS file.
System.Net.Mail.SmtpClient smtpclient = new System.Net.Mail.SmtpClient();
smtpclient.Host = "smtp.gmail.com"; //-------this has to given the Mailserver IP
smtpclient.EnableSsl = true;
smtpclient.Credentials = new System.Net.NetworkCredential("anirudhagupta0011@gmail.com", "testing");
smtpclient.Send(msg);

关于c# - 使用 C# 创建 ics 文件并发送带有附件的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49358161/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com