Advertise with us

Sending email in .NET through Gmail

Posted by  jayprakash sharma
 1449  View(s)
Rate this:
Hi Everyone!

Instead of relying on my host to send email, I was thinking of sending the messages though my Gmail account. The emails are personalized emails to the bands I play on my show. Is it possible to do?

Thanks in advance. 
  1. Re: Sending email in .NET through Gmail

    Hi Jayprakash!

    Be sure to use System.Net.Mail, not the deprecated System.Web.Mail. Doing SSL with System.Web.Mail is a gross mess of hacky extensions.

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

    var fromAddress = new MailAddress("", "From Name");
    var toAddress = new MailAddress("", "To Name");
    const string fromPassword = "fromPassword";
    const string subject = "Subject";
    const string body = "Body";

    var smtp = new SmtpClient
                   Host = "",
                   Port = 587,
                   EnableSsl = true,
                   DeliveryMethod = SmtpDeliveryMethod.Network,
                   UseDefaultCredentials = false,
                   Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
    using (var message = new MailMessage(fromAddress, toAddress)
                             Subject = subject,
                             Body = body