【发布时间】:2021-01-04 03:02:36
【问题描述】:
我正在尝试发送一封带有日历邀请的电子邮件,作为 ics 文件附件。我正在使用 Mailkit,因为我的理解是 system.net.mail 已被弃用。
玩过之后,我知道“健美运动员”正在寻找文件路径,但这不是我想要在这里做的,所以我有点迷路了。
网络上的很多文档似乎已经过时了......
public IActionResult ThrEmail(string sendto, string subject, string body)
{
if (!String.IsNullOrEmpty(sendto))
{
//construct mail
var message = new MimeMessage();
message.From.Add(new MailboxAddress("nick", "nic-fleetwood@behr.travel"));
message.To.Add(new MailboxAddress(sendto));
message.Subject = subject;
//message.Body = new TextPart("plain")
//{
// Text = body
//};
BodyBuilder emailBody = new BodyBuilder();
emailBody.TextBody = body;
//ics file -- use yyyMMddTHHmmssZ format
StringBuilder str = new StringBuilder();
//str.AppendLine()
str.AppendLine("BEGIN:VCALENDAR");
str.AppendLine("PRODID:-//RYNE MOVING//EN");
str.AppendLine("VERSION:2.0");
str.AppendLine("METHOD:PUBLISH");
//THE EVENT
str.AppendLine("BEGIN:VEVENT");
str.AppendLine("DTSTART:20210215T100000");
str.AppendLine("DTEND:20210215T110000");
str.AppendLine("DTSTAMP:" + DateTime.Now);
str.AppendLine("UID:" + Guid.NewGuid());
str.AppendLine("CREATED:" + DateTime.Now);
str.AppendLine("X-ALT-DESC;FMTTYPE=text/html:");
//sb.AppendLine("DESCRIPTION:" + res.Details);
str.AppendLine("LAST-MODIFIED:" + DateTime.Now);
str.AppendLine("LOCATION:NYC");
str.AppendLine("SEQUENCE:0");
str.AppendLine("STATUS:CONFIRMED");
str.AppendLine("SUMMARY:");
str.AppendLine("TRANSP:OPAQUE");
str.AppendLine("END:VEVENT");
str.AppendLine("END:VCALENDAR");
emailBody.Attachments.Add(str.ToString());
//send the email
using (var client = new SmtpClient())
{
client.Connect("smtp.office365.com", 587, false);
// Note: only needed if the SMTP server requires authentication
client.Authenticate("nic-fleetwood@behr.travel", "foobar");
client.Send(message);
client.Disconnect(true);
}
}
return View();
}
【问题讨论】:
标签: c# asp.net asp.net-core model-view-controller mailkit