从服务器生成 Outlook 电子邮件

Generate Oulook Email From Server

我有一个 ASP.NET 站点需要能够动态生成一封电子邮件,该电子邮件将发送回用户本地计算机,然后通过 Outlook 发送。下面的代码就是这样做的,但它使用 Outlook Interop 来创建消息,我有点犹豫要不要在 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");
}

我以前也有过类似的需求。我搁置了这个想法,因为生成 .msg files (though I think there's some commercial libraries that can do it). One alternative you may consider is installing an Outlook add-in on the user's machine. Poll the web server (or this would be a great case for SignalR 的复杂性使得服务器可以推送数据)并让 Web 服务器将电子邮件的详细信息发送到用户的机器。然后加载项可以根据从服务器收到的详细信息生成电子邮件。这将避免 运行 服务器上的互操作(这是一个 糟糕的 想法)。但是,您现在会遇到部署 Outlook 加载项的复杂性,但如果这是公司环境,应该不会太困难。

如果您不想将它作为一个插件来实现,您仍然可以通过编写一个在用户机器上运行并使用 Interop 的服务应用程序来实现它,但这仍然具有插件技术。

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

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

我确实找到了 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");
}