从ASP.Net发送邮件,自动保存到客户端的Outlook日历

时间:2015-03-09 11:43:35

标签: asp.net outlook

我只想从我的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);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

Microsoft目前不建议也不支持从任何无人参与的非交互式客户端应用程序或组件(包括ASP,ASP.NET,DCOM和NT服务)自动化Microsoft Office应用程序,因为Office在此环境中运行Office时,可能会出现不稳定的行为和/或死锁。

如果要构建在服务器端上下文中运行的解决方案,则应尝试使用已为安全无人值守执行的组件。或者,您应该尝试找到允许至少部分代码在客户端运行的替代方法。如果从服务器端解决方案使用Office应用程序,则应用程序将缺少许多成功运行的必要功能。此外,您将承担整体解决方案稳定性的风险。您可以在Considerations for server-side Automation of Office文章中详细了解相关内容。

考虑使用EWS(Exchange Web服务)或BCL(基类库)中的标准类。您可能会发现EWS Managed API, EWS, and web services in Exchange文章有用。