我只想从我的 ASP.Net 应用程序向Outlook发送一封提醒邮件,该邮件会自动保存到目标Outlook Emailid calandar。
我已经实现了这个,但这仅适用于我的系统的 Outlook。它不适用于其他系统。
protected void btSent_Click(object sender, EventArgs e) { SendMail("xyz@xyz.com", "xyx"); } public void SendMail(string targetMail, string shownTargetName) { MailAddress fromAddress = new MailAddress("xyz@xyz.com", "MailSendingProgram"); MailAddress toAddress = new MailAddress(targetMail, shownTargetName); String fromPassword = "xyz"; String subject = "Test Recurrence"; String body = @" Here you can put in any text that will appear in the body multilined and even in "; SmtpClient smtp = new SmtpClient { Host = "smtp.xyz.com", Port = 25, EnableSsl = true, DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = false, Credentials = new NetworkCredential(fromAddress.Address, fromPassword) };
using (MailMessage message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
}
)
{
try
{
ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{ return true; };
smtp.Send(message);
lbError.Text = "E-Mail sent!";
Microsoft.Office.Interop.Outlook.Application olApp = new Microsoft.Office.Interop.Outlook.Application();
CreateNewRecurringAppointment(olApp);
Marshal.ReleaseComObject(olApp);
}
catch
{
lbError.Text = "Sending failed, check your internet connection!";
}
}
}
public void CreateNewRecurringAppointment(Microsoft.Office.Interop.Outlook._Application OutlookApp) { Microsoft.Office.Interop.Outlook.AppointmentItem appItem = null; Microsoft.Office.Interop.Outlook.RecurrencePattern pattern = null; try { appItem = OutlookApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olAppointmentItem) as Microsoft.Office.Interop.Outlook.AppointmentItem; // create a recurrence pattern = appItem.GetRecurrencePattern(); pattern.RecurrenceType = Microsoft.Office.Interop.Outlook.OlRecurrenceType.olRecursWeekly; pattern.StartTime = DateTime.Parse("8:35:00 AM"); pattern.EndTime = DateTime.Parse("9:35:00 PM"); // we can specify the duration instead of using the EndTime property pattern.Duration = 60; pattern.PatternStartDate = DateTime.Parse("03/9/2015"); pattern.PatternEndDate = DateTime.Parse("03/9/2015"); appItem.Subject = "Meeting with the Boss"; appItem.Body = "Test Appointment body"; appItem.Location = "P1"; appItem.ReminderSet = true; appItem.ReminderMinutesBeforeStart = 15; appItem.Importance = Microsoft.Office.Interop.Outlook.OlImportance.olImportanceHigh; appItem.BusyStatus = Microsoft.Office.Interop.Outlook.OlBusyStatus.olBusy; appItem.Save(); appItem.Send();
//appItem.Display(true);
}
catch (Exception ex)
{
lbRecur.Text = ex.Message;
}
finally
{
if (pattern != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(pattern);
}
if (appItem != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(appItem);
}
}
}
Microsoft 当前不建议也不支持从任何无人参与、非交互式客户端应用程序或组件(包括 ASP、ASP.NET、DCOM 和 NT 服务)自动化Microsoft Office 应用程序,因为在此环境中运行 Office 时 Office 可能会表现出不稳定的行为和/或死锁。
如果要生成在服务器端上下文中运行的解决方案,则应尝试使用已安全执行无人参与的组件。或者,您应该尝试找到至少允许部分代码在客户端运行的替代方法。如果使用服务器端解决方案中的 Office 应用程序,则该应用程序将缺少成功运行所需的许多功能。此外,您将承担整体解决方案稳定性的风险。可以在 Office 服务器端自动化的注意事项一文中阅读有关此内容的详细信息。
请考虑使用 EWS(Exchange Web 服务)或 BCL(基类库)中的标准类。您可能会发现 Exchange 中的 EWS 托管 API、EWS 和 Web 服务文章很有帮助。