C# 从服务器生成电子邮件

C# 从服务器生成电子邮件,c#,asp.net,outlook,interop,openxml,C#,Asp.net,Outlook,Interop,Openxml,我有一个ASP.NET网站,需要能够动态生成一封电子邮件,将发送回用户的本地计算机,然后通过Outlook发送。下面的代码就是这样做的,但它使用Outlook Interop来创建消息,我对在Web应用程序上使用Interop有点犹豫。我研究了OpenXML,但在Outlook上似乎找不到太多 // Creates a new Outlook Application Instance Microsoft.Office.Interop.Outlook.App

我有一个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");
}

我以前也有类似的需要。我搁置了这个想法,因为生成文件很复杂(尽管我认为有很多方法可以做到)。您可以考虑的另一个选择是在用户的机器上安装Outlook插件。轮询web服务器(或者这是一个很好的例子,以便服务器可以推送数据),并让web服务器将电子邮件的详细信息发送到用户的机器。然后外接程序可以根据从服务器收到的详细信息生成电子邮件。这将避免在服务器上运行互操作(这是一个坏主意)。但是,现在部署Outlook外接程序会很复杂,但是如果这是一个公司环境,那么应该不会太困难

如果您不想将其作为外接程序,您仍然可以通过编写一个在用户机器上运行并使用互操作的服务应用程序来实现,但这仍然具有外接程序技术的所有复杂性

或者,如果你的电子邮件真的很简单,你可以。但我发现它们非常有限,因为用这种方式发送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");
}

您应该直接生成MIME文件,而不使用Outlook。有关于如何生成MIME文件的示例代码吗?Outlook的.msg文件不是基于文本的。如果JavaScript适合您,请将其作为答案(而不是问题的一部分)。如果不起作用,请解释原因。不幸的是,我对web服务器和用户计算机上的内容都有很多限制。此外,这条消息是非常动态的,所以简单的邮件不会削减它。最后,用户必须能够查看、进一步剪切并加密电子邮件。