How to Send Email from your site using Gmail as your server using ASP NET
One of the most common tasks in Web development is sending an email. We often need to give our users a way to contact us by email. We can use a good mailing method in our sites in cases of “Contact us”, “I forgot my Password” or “I would like information on” links.
We can always use HTML Mailto method, but that means that we have to assume the user has a mail client on his computer. Sometimes we want to have more control on the way the mail is sent. Maybe we don’t want our user to know that a mail was sent. In my sites I have a Connection page that let users share their opinions with me in a well formatted form. When the user clicks on the Send button I use a send mail method behind the screens.
Using asp.net it is very easy to perform this action, and because all of us like to use free services I am going to use Gmail in order to demonstrate e-mail delivery via Gmail in ASP.NET code.
We are going to use two .net classes: SmtpMail and MailMessage. in order to use them we must import System.net.Mail. We can do it by writing the following line of code at the top of the page:
Imports System.Net.Mail.
Now, all we have left to do is to write a procedure that sends the email:
Send mail with the help of Gmail
Dim objMail As MailMessage = New MailMessage ()
objMail.To.Add (jane@cloudyflow.com)
objMail.To.Add (“jon@live.com”)
objMail.From = New MailAddress (“yourname@yourdomain.com”)
objMail.Subject = “Sending Emails using Gmail services”
Dim msg As String = “Cloud computing is great!”
objMail.Body = msg
objMail.IsBodyHtml = True
Dim smtp As SmtpClient = New SmtpClient ()
smtp.Host = “smtp.gmail.com” ‘Or any other SMTP Server
smtp.Credentials = New System.Net.NetworkCredential
(“yourname@gmail.com”, “password”)
smtp.EnableSsl = True
smtp.Send (objMail)
That’s it. You can copy and paste the code and use it in your own asp.net projects.
Yossi Sigura is a software engineer and a Cloud Computing expert with a passion for programming and programming languages. Yossi writes a few programming blogs among them are: http://www.codeanan.co.il and http://www.devschool.co.il
Article from articlesbase.com
Find More ASP.NET Articles
Learn more about E-mail
This article provides answers to frequently asked questions regarding the usage of email.
Categories: Internet Tutorials Tags: email, Internet