http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail/32336#32336
Be sure to use System.Net.Mail
, not the deprecated System.Web.Mail
. Doing SSL withSystem.Web.Mail
is a gross mess of hacky extensions.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Diagnostics;
using System.Net;
namespace Common
{
public class SendEmail
{
public List<string> ToAddressList { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
public SendEmail()
{
}
public void Send()
{
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);
}
}
}
}