简体   繁体   English

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

[英]updating Classic ASP email code to c# Asp.Net

I have written many apps that send emails , normally I create an SMTP client , authenticate with username and password , and that's it! 我编写了许多发送电子邮件的应用程序,通常我创建了一个SMTP客户端,使用用户名和密码进行身份验证,仅此而已! I am now updating some OLD classic ASP code where they sent an email like so : 我现在正在更新一些旧的经典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, 我已经在Google上找到了,显然,很早以前就不推荐使用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更新此代码的最佳方法是什么?

CDO is an ActiveX component. CDO是ActiveX组件。 It wasn't created for ASP specifically, but it pretty much became the de facto way to bodge email into your ASP applications. 它不是专门为ASP创建的,但实际上已成为将电子邮件绑定到ASP应用程序的事实上的方法。 It becomes your SMTP client and generally uses a local SMTP server to relay email to another SMTP server for onward transmission. 它成为您的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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM