Monday, October 01, 2007

How to send Email using Gmail SMTP server (c#)

What will you want?
valid gmail user account with password


Connects to
smtp.gmail.com, authenticate a valid gmail user account using SSL and sends an email.

Regular SSL port 465 won´t work. You´ll have to use an alternate port, 587.

c# code:

//You should include following namespace

using System.Net.Mail ;
using System.Net;

MailMessage MailObj = new MailMessage();

MailObj.To.Add ("GmailToAccount@gmail.com");
MailObj.From = new MailAddress("GmailFromAccount@gmail.com","From");
//you can use cc also

MailObj.IsBodyHtml = true;
MailObj.Priority = MailPriority.Normal ;
MailObj.Subject = "Subject";
MailObj.Body = "Test mail";
SmtpClient smtpcli = new SmtpClient("smtp.gmail.com", 587);
//use this PORT!

smtpcli.EnableSsl = true;
smtpcli.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpcli.Credentials = new NetworkCredential("GmailFromAccount@gmail.com ","GmailFromAccountPassword");

try
{
smtpcli.Send(MailObj);
}catch (Exception ex){
throw ex;
}

I am tying to send atachment and download emails from pop servers, I will tell it near future...

No comments: