Thursday, July 2, 2009

Send emails via google mail server in C#

Some time we need to send mails to others from our application, and fro this we need to use some free email server like GMAIL. here is the code using that we can use the gmail mail server to send the mails. I am simply specifying the gmail SMTP server name and the Port number in the code.. take a look…


MailMessage mailMessage = new MailMessage();
mailMessage.To.Add(”test@test.com”);
mailMessage.Subject = “Test”;
mailMessage.Body = “This is a test mail”;
mailMessage.IsBodyHtml = true;

// Create the credentials to login to the gmail account associated with my custom domain
string sendEmailsFrom = “abc@xyz.com”;
string sendEmailsFromPassword = “password”;

NetworkCredential cred = new NetworkCredential(sendEmailsFrom, sendEmailsFromPassword);
SmtpClient mailClient = new SmtpClient(”smtp.gmail.com”, 587);
mailClient.EnableSsl = true;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.UseDefaultCredentials = false;
mailClient.Timeout = 30000;
mailClient.Credentials = cred;
mailClient.Send(mailMessage);

No comments: