从服务器生成Outlook电子邮件

时间:2015-01-16 16:52:17

标签: c# asp.net outlook interop openxml

我有一个ASP.NET站点,需要能够动态生成一封电子邮件,然后将其发送回用户本地计算机,然后通过Outlook发送。下面的代码只是这样,但它使用Outlook Interop来创建消息,我有点hesitent在Web应用程序上使用Interop。我查看了OpenXML,但似乎无法在Outlook上找到太多。

            // Creates a new Outlook Application Instance
        Microsoft.Office.Interop.Outlook.Application objOutlook = new Microsoft.Office.Interop.Outlook.Application();

        // Creating a new Outlook Message from the Outlook Application Instance
        Microsoft.Office.Interop.Outlook.MailItem mic = (Microsoft.Office.Interop.Outlook.MailItem)(objOutlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem));

        mic.To = "to@email.com";
        mic.CC = "cc@email.com";
        mic.Subject = "Test Subject";
        mic.HTMLBody = "Test Message Body";


        string strNewEmailPath = strEmailPath + "\\EmailMessages\\" + strUser + "_Message_PEI.msg";

        mic.SaveAs(strNewEmailPath, Microsoft.Office.Interop.Outlook.OlSaveAsType.olMSG);

        HttpContext.Current.Response.ContentType = "application/vnd.ms-outlook";
        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=Message.msg");
        HttpContext.Current.Response.TransmitFile(strNewEmailPath);
        HttpContext.Current.Response.End();

任何人都可以帮助提出使用ASP.NET自动化Outlook消息的更好建议吗?

更新

我确实找到了似乎具有类似功能的Javascript代码。

var theApp    //Reference to Outlook.Application 
var theMailItem   //Outlook.mailItem
//Attach Files to the email, Construct the Email including     
//To(address),subject,body
var subject = sub
var msg = body
//Create a object of Outlook.Application
try
{
    var theApp = new ActiveXObject("Outlook.Application")
    var theMailItem = theApp.CreateItem(0) // value 0 = MailItem
      //Bind the variables with the email
      theMailItem.to = to
      theMailItem.Subject = (subject);
      theMailItem.Body = (msg);
      //Show the mail before sending for review purpose
      //You can directly use the theMailItem.send() function
      //if you do not want to show the message.
      theMailItem.display()
  }
catch(err)
{
    alert("Error");
}

2 个答案:

答案 0 :(得分:0)

我之前有类似的需求。由于生成.msg文件的复杂性,我搁置了这个想法(虽然我认为some commercial libraries可以做到这一点)。您可以考虑的另一种方法是在用户的计算机上安装Outlook加载项。轮询Web服务器(或者这将是SignalR的一个很好的案例,因此服务器可以推送数据)并让Web服务器将电子邮件的详细信息发送到用户的计算机。然后,加载项可以根据从服务器收到的详细信息生成电子邮件。这样可以避免在服务器上运行Interop(这是一个错误的想法)。但是,您现在将具有部署Outlook加载项的复杂性,但如果这是一个企业环境,那么它应该不会太困难。

如果您不想将其作为加载项执行,您仍然可以通过编写在用户计算机上运行并使用Interop的服务应用程序来实现,但仍然具有添加的所有复杂性 - 在技​​术方面。

或者,如果您的电子邮件非常简单,那么您可以use a mailto URI。但我发现它们非常有限,因为以这种方式发送HTML消息很困难或不可能。

最后,您可以让服务器代表用户发送电子邮件,而不是涉及在用户计算机上运行的代码。

答案 1 :(得分:0)

我确实找到了似乎具有类似功能的Javascript代码。我希望有人可能有一个OpenXML解决方案,但这个JS解决方案可以工作,并且比Outlook Interop

更好
var theApp    //Reference to Outlook.Application 
var theMailItem   //Outlook.mailItem
//Attach Files to the email, Construct the Email including     
//To(address),subject,body
var subject = sub
var msg = body
//Create a object of Outlook.Application
try
{
    var theApp = new ActiveXObject("Outlook.Application")
    var theMailItem = theApp.CreateItem(0) // value 0 = MailItem
      //Bind the variables with the email
      theMailItem.to = to
      theMailItem.Subject = (subject);
      theMailItem.Body = (msg);
      //Show the mail before sending for review purpose
      //You can directly use the theMailItem.send() function
      //if you do not want to show the message.
      theMailItem.display()
  }
catch(err)
{
    alert("Error");
}