facebook  linkedin  Twitter  skype  Rss googlePlus

SMTP Authentication in ASP.NET

Jan 28 2004

My Exchange server needs authentication to send mails. Later found that .NET 1.0 doenst support authencation with an SMTP server. I had to switch over to .NET 1.1 to send mails. All which is required to add a few lines of code for authentication.

//This is a usual code
System.Web.Mail.MailMessage msg = new System.Web.Mail.MailMessage();
msg.Subject = "Test Subject";
msg.Body = "Sample Body Message";
msg.From = yourname@domain.com;
msg.To = someone@domain.com;
System.Web.Mail.SmtpMail.SmtpServer = "EXCHANGESERVER";
System.Web.Mail.SmtpMail.Send( msg );
//Add this line of code for authentication.

Where the 2 specifies NTLM, 1 for basic, 0 for none.


Some comments from the original post on WWWCoder.com:

I use Brinkster.com as my web/mail hosting service and GoDaddy as my domain name registrar. The smtp server and auth. info to use is NOT the same info you use to login to their respective site, but rather the smtp server specified in their faq and the email address you've set up in their email manager. Brinkster's is sendmail.brinkster.com. This is different than the one you use for your email account. However, the authentication IS the info from your email account (if they host your mail, too). In short, for Brinkster, I use sendmail.brinkster.com as my smtp server, and johndoe@mybrinksterdomainname.com as the username, and the password I use (to check email on this account) as the password. 

Hope this helps..I know it's confusing..just got to read the faqs on your respective provider's site because it's usually a freaky combination such as this to get it to work..but it will work. I created a simple web service that accepts all info as parameters and returns a string either stating "success" or the actual exception caught when trying to send email (so can read whether or not connection was made, or auth. was bad, etc.). This service is written in C#, but it works like a charm! 
If you don't need to authenticate, leave the last 3 parameters blank (just send empty strings), else fill in w/ your smtp server and login info. 

[WebMethod (Description="Simple mailer w/ authentication")]
public string SimpleMail(string mailto, string mailfrom, string mailsubject, string mailbody, string mailcc, string mailbcc, string smtpserver, string username, string password)
string _results = "success!";
MailMessage mm = new System.Web.Mail.MailMessage();
mm.From = mailfrom;
mm.To = mailto;
mm.Subject = mailsubject;
mm.Body = mailbody;
mm.Cc = mailcc;
mm.Bcc = mailbcc;
mm.BodyFormat = MailFormat.Text;
int cdoBasic = 1;
int cdoSendUsingPort = 2;
catch (Exception e)
_results = e.ToString();
return _results;
Just in case anybody was wondering how to authenticate it .net <B>1.0</B> (it took me a while) this works. 
Dim ObjSendMail
Dim iConf
Dim Flds
ObjSendMail = CreateObject("CDO.Message")
iConf = CreateObject("CDO.Configuration")
Flds = iConf.Fields
ObjSendMail.Configuration = iConf
ObjSendMail.From = SentBy
ObjSendMail.To = EmailAddress
ObjSendMail.Subject = Subject
If isText Then
ObjSendMail.HTMLBody = Message
ObjSendMail.TextBody = Message
End If

Nothing new, its just using CDO, but its new to me. I've been using web.mail for a while, but I've got .Net 1.0 and all the sudden my host started requiring users to authenticate, so this is what I found. 

About the Author, Staff

You can write for Santry.com, just email us at info@santry.com for more information.

blog comments powered by Disqus


Thank you for visiting our site, before you leave, please visit some of the areas or information you may have missed.

Popular Articles