将经典ASP电子邮件代码更新为c#Asp.Net

时间:2021-08-17 05:15:27

I have written many apps that send emails , normally I create an SMTP client , authenticate with username and password , and that's it! I am now updating some OLD classic ASP code where they sent an email like so :

我写了很多发送电子邮件的应用程序,通常我创建一个SMTP客户端,使用用户名和密码进行身份验证,就是这样!我现在正在更新一些旧的经典ASP代码,他们发送了这样的电子邮件:

Set objMessage      = Server.CreateObject("CDO.Message")
objMessage.To       = strTo
objMessage.From     = strFrom
objMessage.Bcc      = strBcc
objMessage.Subject  = strSubject
objMessage.TextBody = strBody
objMessage.Send
Set objMessage      = Nothing

I've Google'd and found that obviously the CDO objects were deprecated long ago,

我已经谷歌了,很明显CDO对象很久以前就被弃用了,

my question is :

我的问题是:

Is this code above actually able to send emails without creating some type of client with authentication?? AND what is the best way to update this code with using c# 4.5 ??

以上代码是否真的能够发送电子邮件而无需创建某种类型的客户端进行身份验证?什么是使用c#4.5更新此代码的最佳方法?

1 个解决方案

#1


5  

CDO is an ActiveX component. It wasn't created for ASP specifically, but it pretty much became the de facto way to bodge email into your ASP applications. It becomes your SMTP client and generally uses a local SMTP server to relay email to another SMTP server for onward transmission.

CDO是一个ActiveX组件。它不是专门为ASP创建的,但它几乎成了将电子邮件提交到ASP应用程序的事实上的方法。它成为您的SMTP客户端,通常使用本地SMTP服务器将电子邮件中继到另一个SMTP服务器以进行传输。

To send emails in the land of .Net 4.5 using C#, use

要使用C#在.Net 4.5的土地上发送电子邮件,请使用

//Create a Smtp client, these settings can be set in the web.config and
//you can use the parameterless constructor
SmtpClient client=new SmtpClient("some.server.com");
//If you need to authenticate
client.Credentials=new NetworkCredential("username", "password");

//Create the message to send
MailMessage mailMessage = new MailMessage();
mailMessage.From = "someone@somewhere.com";
mailMessage.To.Add("someone.else@somewhere-else.com");
mailMessage.Subject = "Hello There";
mailMessage.Body = "Hello my friend!";

//Send the email
client.Send(mailMessage);

#1


5  

CDO is an ActiveX component. It wasn't created for ASP specifically, but it pretty much became the de facto way to bodge email into your ASP applications. It becomes your SMTP client and generally uses a local SMTP server to relay email to another SMTP server for onward transmission.

CDO是一个ActiveX组件。它不是专门为ASP创建的,但它几乎成了将电子邮件提交到ASP应用程序的事实上的方法。它成为您的SMTP客户端,通常使用本地SMTP服务器将电子邮件中继到另一个SMTP服务器以进行传输。

To send emails in the land of .Net 4.5 using C#, use

要使用C#在.Net 4.5的土地上发送电子邮件,请使用

//Create a Smtp client, these settings can be set in the web.config and
//you can use the parameterless constructor
SmtpClient client=new SmtpClient("some.server.com");
//If you need to authenticate
client.Credentials=new NetworkCredential("username", "password");

//Create the message to send
MailMessage mailMessage = new MailMessage();
mailMessage.From = "someone@somewhere.com";
mailMessage.To.Add("someone.else@somewhere-else.com");
mailMessage.Subject = "Hello There";
mailMessage.Body = "Hello my friend!";

//Send the email
client.Send(mailMessage);