Hi
A few days back I wanted to send my mails using the Gmail
Account. The problem was that I wanted to automate the system and send mail
from my Gmail Account through my Application. For this purpose I wrote a class
that would send mail from Gmail Account. Here is the class that I wrote for
sending the mail from Gmail.
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Mail;
namespace SendMailFormGmail
{
class GmailEmail
{
public bool sendMail(string
FromEmail,string FromName,string ToList, string
Subject, string Body,string Password)
{
MailMessage Msg = new MailMessage();
//set
message sender
Msg.From =
new MailAddress(FromEmail, FromName);
//sets the
mail recipients //person(s)
who will receive an email
//(in this
case 2 person will be emailed)
Msg.To.Add(new MailAddress(ToList));
//set to
true if you want to use html in the body
Msg.IsBodyHtml = true;
Msg.Subject
= Subject;
//body of
the email message with html tag
Msg.Body =
Body;
//Allows
applications to send e-mail
// host
server : smtp.gmail.com
//port
number : 587
SmtpClient
objMail = new SmtpClient("smtp.gmail.com", 587);
//Some SMTP
servers require you to authenticate first
//gmail uses SSL
//info
object contains the gmail
//username,
and password
NetworkCredential info = new NetworkCredential(FromEmail, Password);
objMail.DeliveryMethod = SmtpDeliveryMethod.Network;
objMail.Credentials = info;
objMail.EnableSsl = true;
try
{
//final
step send email
objMail.Send(Msg);
return
true;
}
catch
{
return
false;
}
}
}
}
The function sendMail
returns a Boolean value on weather the mail was send
or not
Hope this helps
Thanks
Vikram