Ayman Farouk

Microsoft Maniac Guy ..!
posts - 33, comments - 104, trackbacks - 58

My Links

News

Archives

Blogroll

Links

Thursday, March 09, 2006

System.net.mail with Gmail account

Here is a small code for sending an email from ASP.NET 2.0 page, using the namespace System.net.mail and a Gmail account.

First of all we have to import these :

Imports System.Net.Mail

Imports System.Net.Mail.MailMessage

Imports System.Net.NetworkCredential

Then,

    Dim mail As New MailMessage()

    Dim msgBody As String

    Dim smtp As New SmtpClient

    mail.From = New MailAddress(“ur-gmail-account@gmail.com, "display name")

    mail.To.Add(“ur-email@host.com“)

    mail.Subject = “Subject“

    mail.Body = “msgBody“

    mail.IsBodyHtml = True ' This is to enable HTML in your email body

    mail.ReplyTo = New MailAddress(“reply-to-email-address“)  ' This is optional, it allows you to add Reply To email address.

   smtp.Host = "smtp.gmail.com"

   smtp.Port = 25

   smtp.EnableSsl = True

   smtp.Credentials = New System.Net.NetworkCredential(“ur-gmail-account@gmail.com, "gmail-password")

   smtp.Send(mail)

   lblFlag.Text = "Your Message has beent sent."

Note: We can build this email form AJAX based, by putting the content in an UpdatePanel control, and add the button “send” as a trigger.

 

A.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Posted On Thursday, March 09, 2006 7:08 PM | Feedback (45) |

Powered by: