How to use authentication while sending mails using SMTP with ASP.NET.

Submitted on: 1/2/2015 4:45:00 AM
By: Kaustav N (from psc cd)  
Level: Beginner
User Rating: By 3 Users
Compatibility: C#, VB.NET, ASP.NET
Views: 3958
     This article demonstrates how to authenticate a SMTP server in an ASP.NET application.


Authenticate a SMTP Server in ASP.NET -- By Kaustav Neogy

In .NET we can use the System.Web.Mail Namespace to send mails. The SMTPMail Class provides properties and methods for sending messages using the Collaboration Data Objects for Windows 2000 (CDOSYS) message component. However there exists no direct way to use authentication with a SMTP Server. Here’s a quick workaround to it.

Public Sub Send_Mail_With_Auth(ByVal szTo As String, ByVal szFrom As String, ByVal szSubject As String, ByVal szBody As String)

Dim myMsg
Dim myConfig
Dim Flds

Const cdoSendUsingPort = 2
Const cdoBasic As Integer = 1

myMsg = CreateObject("CDO.Message")
myConfig = CreateObject("CDO.Configuration")
Flds = myConfig.Fields

With Flds
        .Item("") = cdoSendUsingPort
        .Item("") = "your_smtp_server"
        .Item("") = 10
        .Item("") = "username"
        .Item("") = "password"
        .Item("") = cdoBasic
End With

With myMsg
        .Configuration = myConfig
        .To = szTo
        .From = szFrom
        .Subject = szSubject
        .HTMLBody = szBody
End With

myMsg = Nothing
myConfig = Nothing
Flds = Nothing

End Sub

Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.