Continually Improving.... let us know how support@devdiamond.net Sign in | Sign up
home articles news blog forum  

 
 


 
Skip Navigation LinksHome > Article > Sending Emails using ASP.Net v 2.0
Sending Emails using ASP.Net v 2.0
Abstract :
A common thing you will need to do with ASP.Net v 2.0, is to send emails, I will show you here how to send emails using ASP.Net v 2.0, implementing authentication

Views : 4504
Published : Friday, August 12, 2005
By
HyperLink

Avarage Rating :
Page Page 1 of 1

A common thing you will need to do with ASP.Net v 2.0, is to send emails, I will show you here how to send emails using ASP.Net v 2.0, implementing authentication

The namespace for sending mail within ASP.Net v 2.0 is not System.Web.Mail anymore this was replaced by System.Net.Mail

You can use just three lines of code to send emails,

Dim SmtpClient As New System.Net.Mail.SmtpClient
SmtpClient.Host = “localhost”
SmtpClient.Send(from,to,subject,body)

This will send your email assuming your SMTP server is “localhost”, change this to your real SMTP host, probably “mail.yourdomain.com”

If your SMTP server requires authentication then you will need to add one line of code so your code will be

Dim SmtpClient As New System.Net.Mail.SmtpClient
SmtpClient.Host = “localhost”
SmtpClient.Credentials = New NetworkCredential( "username" , "password" )
SmtpClient.Send( "fomemail" ,toemail,subject,body)

While this will send your emails, you can feather specify more details like HTML body, encoding and all the other cool things, I have a sub routine that encapsulate this code so it can be reused

Sub Send( ByVal emailFrom As String , ByVal emailTo As String , ByVal emailSubject As String , ByVal emailMessage As String , ByVal isBodyHTML As Boolean )

Dim msgMail As New System.Net.Mail.MailMessage
Dim emailFromAddress As New System.Net.Mail.MailAddress(emailFrom)
Dim emailToAddress As New System.Net.Mail.MailAddress(emailTo)
Dim SmtpClient As New System.Net.Mail.SmtpClient
msgMail.To(0) = emailToAddress
msgMail.From = emailFromAddress
msgMail.Subject = emailSubject
msgMail.IsBodyHtml = isBodyHTML
msgMail.Body = emailMessage
SmtpClient.Host = "localhost"
SmtpClient.Credentials = New NetworkCredential( "username" , "password" )
SmtpClient.Send(msgMail)

End Sub

Hope this will help,



About Author

        Yasir Send Feedback
        Yasir is a .NET expert, with over 5 years experience in Microsoft Technologies, 8 years overall programming experience, he is the owner, founder & primary contributor of Minwar.com, and he also works as IT Director in the hospitality industry.