0
votes

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

1
What do you mean by "creating some type of client"? Any time you send an SMTP mail, you have to create a client.DavidG
@DavidG - ok what I meant , how , were is that info stored in a classic asp site?Scott Selby
@DavidG - and I understand that , but did classic ASP have something built-in that made it so you didn't have to make a client because they do it for youScott Selby
CDO is an ActiveX component, I can't remember where it gets it's settings from. It wasn't created for ASP specifically, but it pretty much became the de facto way to bodge email into your ASP applications.DavidG
@Vogel612 - meant I need to either access this same ActiveX component from .Net 4.5 or find their credentials to create my own SMTP client . Neither matters now , the client decided to not have me do the job and is sticking with classic ASP . oh well , I was just commenting so you understoodScott Selby

1 Answers

5
votes

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 = "[email protected]";
mailMessage.To.Add("[email protected]");
mailMessage.Subject = "Hello There";
mailMessage.Body = "Hello my friend!";

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