Home > DeveloperSection > Forums > Sending email in .NET through Gmail
jayprakash sharma
jayprakash sharma

Total Post:117

Points:821
Posted on    May-29-2013 2:02 AM

 C# C# 
Ratings:


 1 Reply(s)
 648  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. 


AVADHESH PATEL

Total Post:604

Points:4228
Posted on    May-29-2013 9:03 AM

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);
}

Don't want to miss updates? Please click the below button!

Follow MindStick