web 2.0

Sending Emails with Google Gmail Account over SSL with C#

Hi,

Today a quick Code Snippet in order to Send Emails using your Google Gmail Account with C#

static void Main(string[] args)
{
    SmtpClient smtp = new SmtpClient();
    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtp.EnableSsl = true;
    smtp.Host = "smtp.gmail.com";
    smtp.Port = 587;

    smtp.UseDefaultCredentials = false;
    smtp.Credentials = new NetworkCredential("MyAccount@gmail.com", "MyPassword");

    MailMessage mail = new MailMessage();
    MailAddress MailFrom = new MailAddress("me@me.com", "Me");
    MailAddress MailTo = new MailAddress("you@you.com", "You");

    mail.To.Add(MailTo);
    mail.From = MailFrom;
    mail.Sender = MailFrom;

    mail.Subject = "Mail Test Using Gmail";
    mail.IsBodyHtml = true;

    StringBuilder sb = new StringBuilder();
    sb.Append("<html><head></head><body>");
    sb.Append("<strong>Hello World !</strong>");
    sb.Append("</body></html>");

    mail.Body = sb.ToString();

    try
    {
        smtp.Send(mail);
        Console.WriteLine("Mail Sent !");
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}


Views(797)

kick it on DotNetKicks.com

Share/Save/Bookmark Subscribe

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

C#

Comments

Add comment


 

biuquote
Loading



Technorati Profile