Monday, December 14, 2009

Send Emails Using Gmail As SMTP Server


GmailSmtp

We can use gmail as our SMTP server for .NET development purposes.
C#.Net Code for send mail as follows 
Main thing is you need to define the Port 587 and set  Enable the SSL for SmtpClient

int Port = 587;
string MailServer = "smtp.gmail.com";
string UserName = "gmailUserName";
string Password = "gmailPassword";

System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();
mailMessage.From = new System.Net.Mail.MailAddress("FromAddress");
mailMessage.To.Add(new System.Net.Mail.MailAddress("ToAddress"));

mailMessage.Subject = "Subject";
mailMessage.Body = "Message";
mailMessage.IsBodyHtml = true;

System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(MailServer, Port);

smtpClient.Credentials = new System.Net.NetworkCredential(UserName, Password);
smtpClient.EnableSsl = true;

try
 {
    smtpClient.Send(mailMessage);

 }
catch (Exception Ex)
 {
      string str = Ex.Message;

 }







No comments:

Post a Comment