简体   繁体   中英

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! I am now updating some OLD classic ASP code where they sent an email like so :

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,

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 ??

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.

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

//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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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