SENDING EMAIL IN .NET THROUGH GMAIL

jayprakash sharma

Total Post:117

Points:821
Posted by  jayprakash sharma
C# 
C#
 958  View(s)
Ratings:
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. AVADHESH PATEL

    Post:604

    Points:4228
    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@gmail.com", "From Name");
    var toAddress = new MailAddress("to@example.com", "To Name");
    const string fromPassword = "fromPassword";
    const string subject = "Subject";
    const string body = "Body";

    var smtp = new SmtpClient
               {
                   Host = "smtp.gmail.com",
                   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
                         })
    {
        smtp.Send(message);
    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!