Monday, April 5, 2010

Sending Email from ASP.NET (C# or CSharp and VB) webpage using your Gmail email account.

C#/CShapr CODE
using System.Net.Mail;


MailMessage mail = new MailMessage();
mail.To.Add("toEmail@gmail.com");
mail.From = new MailAddress("fromEmail@gmail.com");
mail.Subject = "Subject of Email" ;

string Body = "This is a body of Email";
mail.Body = Body ;
mail.IsBodyHtml = true ;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("fromEmail@gmail.com", "yourPassword");
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
try
{
smtp.Send(mail);
Label1.Text = "eMail Send OK... ";
}
catch (Exception ex)
{
Label1.Text = "Error Sending eMail: " + ex.Message.ToString();
}

--------------------
VB Code

Imports System.Net.Mail

Dim mail As New MailMessage
mail.To.Add("toEmail@gmail.com")
mail.From = New MailAddress("fromEmail@gmail.com")
mail.Subject = "Subject of Email"
Dim body As String
body = "This is a body of Email"
mail.Body = body
mail.IsBodyHtml = True
Dim smtp As New SmtpClient
smtp.Host = "smtp.gmail.com" ' //Or Your SMTP Server Address
smtp.Credentials = New System.Net.NetworkCredential _
("yourEmail@gmail.com", "yourPassword")
'Or your Smtp Email ID and Password
smtp.EnableSsl = True
Try
smtp.Send(mail)
Label1.Text = "eMail Send OK... "
Catch ex As Exception
Label1.Text = "Error Sending eMail: " + ex.Message
End Try