如何将电子邮件功能从ASP Classic迁移到ASP.NET?

时间:2022-04-27 01:38:24

I previously used CDO.Message and CDO.Configuration in ASP Classic to create HTML emails which was VERY simple to do. In .NET, it appears that you have to give the System.Net.Mail.Message object an HTML string for the content and then somehow embed the required images. Is there an easy way to do this in .NET? I'm pretty new to .NET MVC and would most appreciate any help.

我以前在ASP Classic中使用了CDO.Message和CDO.Configuration来创建非常简单的HTML电子邮件。在.NET中,您似乎必须为System.Net.Mail.Message对象提供内容的HTML字符串,然后以某种方式嵌入所需的图像。有没有一种简单的方法在.NET中执行此操作?我是.NET MVC的新手,非常感谢任何帮助。

This is how it looks in ASP Classic:

这就是ASP Classic的外观:

Set objCDO = Server.CreateObject("CDO.Message")
objCDO.To = someone@somthing.com
objCDO.From = me@myaddress.com
objCDO.CreateMHTMLBody "http://www.example.com/somepage.html"
objCDO.Subject = sSubject

'the following are for advanced CDO schematics
'for authentication and external SMTP

Set cdoConfig = CreateObject("CDO.Configuration")
With cdoConfig.Fields  
  .Item(cdoSendUsingMethod) = cdoSendUsingPort '2 - send using port  
  .Item(cdoSMTPServer) = mail.myaddress.com
  .Item(cdoSMTPServerPort) = 25
  .Item(cdoSMTPConnectionTimeout) = 10
  .Item(cdoSMTPAuthenticate) = cdoBasic
  .Item(cdoSendUsername) = "myusername"
  .Item(cdoSendPassword) = "mypassword"
  .Update  
End With

Set objCDO.Configuration = cdoConfig

objCDO.Send

Basically I would like to send one of my views (minus site.master) as an email, images embedded.

基本上我想发送一个我的观点(减去site.master)作为电子邮件,嵌入图像。

2 个解决方案

#1


I don't know of a simple way right off, but you could use WebClient to get your page, then pass the response as the body.

我不知道一个简单的方法,但您可以使用WebClient获取您的页面,然后将响应作为正文传递。

Example:

var webClient = new WebClient();

byte[] returnFromPost = webClient.UploadValues(Url, Inputs);

var utf = new UTF8Encoding();
string returnValue = utf.GetString(returnFromPost);

return returnValue;

Note: Inputs is just a dictionary of post variables.

注意:输入只是一个后变量字典。

One problem I think you'll run into right off is that I don't think you'd get the images. You could parse the HTML you get and then make the images absolute back to your server.

我认为你会遇到的一个问题是我不认为你会得到这些图像。您可以解析所获得的HTML,然后将图像绝对地返回到您的服务器。

#2


Thank you both for your help - here is a very clean and comprehensive tutorial posted by a .NET MVP http://msdn.microsoft.com/en-us/vbasic/bb630227.aspx

谢谢你的帮助 - 这是一个非常简洁和全面的教程,由.NET MVP发布http://msdn.microsoft.com/en-us/vbasic/bb630227.aspx

#1


I don't know of a simple way right off, but you could use WebClient to get your page, then pass the response as the body.

我不知道一个简单的方法,但您可以使用WebClient获取您的页面,然后将响应作为正文传递。

Example:

var webClient = new WebClient();

byte[] returnFromPost = webClient.UploadValues(Url, Inputs);

var utf = new UTF8Encoding();
string returnValue = utf.GetString(returnFromPost);

return returnValue;

Note: Inputs is just a dictionary of post variables.

注意:输入只是一个后变量字典。

One problem I think you'll run into right off is that I don't think you'd get the images. You could parse the HTML you get and then make the images absolute back to your server.

我认为你会遇到的一个问题是我不认为你会得到这些图像。您可以解析所获得的HTML,然后将图像绝对地返回到您的服务器。

#2


Thank you both for your help - here is a very clean and comprehensive tutorial posted by a .NET MVP http://msdn.microsoft.com/en-us/vbasic/bb630227.aspx

谢谢你的帮助 - 这是一个非常简洁和全面的教程,由.NET MVP发布http://msdn.microsoft.com/en-us/vbasic/bb630227.aspx