我有一个简单的方法,可以将iCalendar邀请发送到收件人的电子邮件地址。由于某种原因,我不断得到
{“事务处理失败。服务器响应为:5.2.0 STOREDRV.Submission.Exception:InvalidRecipientsException;由于消息的永久性异常而导致无法处理消息,因为该消息不包含收件人而无法发送消息。InvalidRecipientsException:无法发送消息,因为它不包含收件人。[Hostname = AM6PR10MB2759.EURPRD10.PROD.OUTLOOK.COM]“} 异常。
奇怪的是,该异常与UID属性有关,这在iCalendar规范中是必需的。当我使用指定的UID属性时,根本不会收到邮件的日历邀请,因为
smtpClient.Send(mailMessage); 触发异常。
但是,当我从日历邀请中删除UID属性时,日历邀请邮件已正确发送到收件人的邮件中,但由于缺少UID 而无法正确显示。
我的问题是:我是否缺少应在iCalendar消息中填充的任何其他收件人属性?
我试图用几种方法填充UID属性。没有任何作品。但是,当我删除UID属性时,我会收到邮件,但是它具有“不支持的日历message.ics”附件。
我还将该消息上传到了iCalendar验证程序,它显示了一个错误,该错误与缺少UID有关。
public class CalendarInvitation
{
public string Name { set; get; }
public string EmailAddress { set; get; }
public string Location { set; get; }
public DateTime StartDateTime { set; get; }
public DateTime EndDateTime { set; get; }
public string Subject { set; get; }
public string Body { set; get; }
}
public static string SendCalendarInvitation(string eMail, CalendarInvitation calendarInvitation)
{
try
{
if (!eMail.Contains("@"))
return null;
int myEmailPort = 587;
int myEmailHostName = "smtp.office365.com";
SmtpClient smtpClient = new SmtpClient(myEmailHostName, myEmailPort);
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential("myEmail@mydomain.com", "myPassword");
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("myEmail@mydomain.com", Messages.EmailNewPasswordFromIdentity);
mailMessage.To.Add(new MailAddress(calendarInvitation.EmailAddress, calendarInvitation.Name));
mailMessage.Subject = calendarInvitation.Subject;
mailMessage.Body = calendarInvitation.Body;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine("BEGIN:VCALENDAR");
stringBuilder.AppendLine("PRODID:-//" + calendarInvitation.EmailAddress);
stringBuilder.AppendLine("VERSION:2.0");
stringBuilder.AppendLine("METHOD:REQUEST");
stringBuilder.AppendLine("BEGIN:VEVENT");
stringBuilder.AppendLine(string.Format("UID:{0}", Guid.NewGuid())); // HAVING THIS LINE OF CODE CAUSES THE EXCEPTION
stringBuilder.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", calendarInvitation.StartDateTime.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z")));
stringBuilder.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", DateTime.Now.ToUniversalTime().ToString("yyyyMMdd\\THHmmss")));
stringBuilder.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", calendarInvitation.EndDateTime.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z")));
stringBuilder.AppendLine(string.Format("SEQUENCE:0"));
stringBuilder.AppendLine(string.Format("CREATED:{0:yyyyMMddTHHmmssZ}", DateTime.Now.ToUniversalTime().ToString("yyyyMMdd\\THHmmss")));
stringBuilder.AppendLine(string.Format("LAST-MODIFIED:{0:yyyyMMddTHHmmssZ}", DateTime.Now.ToUniversalTime().ToString("yyyyMMdd\\THHmmss")));
stringBuilder.AppendLine("LOCATION:" + calendarInvitation.Location);
stringBuilder.AppendLine(string.Format("DESCRIPTION:{0}", calendarInvitation.Body));
stringBuilder.AppendLine(string.Format("X-ALT-DESC;FMTTYPE=text/html:{0}", calendarInvitation.Body));
stringBuilder.AppendLine(string.Format("SUMMARY:{0}", calendarInvitation.Subject));
stringBuilder.AppendLine(string.Format("ORGANIZER:{0}", eMail));
stringBuilder.AppendLine(string.Format("ATTENDEE;CN=\"{0}\";RSVP=TRUE:mailto:{1}", mailMessage.To[0].DisplayName, mailMessage.To[0].Address));
stringBuilder.AppendLine("BEGIN:VALARM");
stringBuilder.AppendLine("TRIGGER:-PT15M");
stringBuilder.AppendLine("ACTION:DISPLAY");
stringBuilder.AppendLine("DESCRIPTION:REMINDER");
stringBuilder.AppendLine("END:VALARM");
stringBuilder.AppendLine("END:VEVENT");
stringBuilder.AppendLine("END:VCALENDAR");
ContentType contentType = new ContentType("TEXT/CALENDAR");
contentType.Parameters.Add("METHOD", "REQUEST");
AlternateView alternativeView = AlternateView.CreateAlternateViewFromString(stringBuilder.ToString(), contentType);
mailMessage.AlternateViews.Add(alternativeView);
smtpClient.Port = myEmailPort;
smtpClient.EnableSsl = true;
smtpClient.Credentials = new NetworkCredential("myEmail@mydomain.com", "myPassword");
try
{
smtpClient.Send(mailMessage);
return string.Empty;
}
catch (Exception e) // JUMPS TO THIS EXCEPTION, WHEN HAVING UID POPULATED IN THE CODE
{
return "Fail";
}
}
catch { }
return string.Empty;
}