This is the class I use:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Configuration;
using System.Web;
using System.Net.Mail;
using System.ComponentModel;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
public class EmailRelay
{
private string smtpServer;
private string userName;
private string password;
private SmtpClient smtp;
private int connectionLimit;
public class threadStatus
{
public string msg;
public bool succeeded;
public string id;
public string subject;
}
public EmailRelay(string _smtpServer, string _userName, string _password, bool useSSL = true, Int16 port = 0, int _connectionLimit = 6)
{
smtpServer = _smtpServer;
userName = _userName;
password = _password;
connectionLimit = _connectionLimit;
NetworkCredential NetworkCred = new System.Net.NetworkCredential(userName , password);
if (useSSL)
{ if (port == 0) { port = 587; } }
else
{ if (port == 0) { port = 25; } }
System.Net.ServicePointManager.MaxServicePoints = 100;
smtp = new SmtpClient(smtpServer, port);
smtp.EnableSsl = useSSL;
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
if (connectionLimit < 10)
{
smtp.ServicePoint.MaxIdleTime = (connectionLimit * 6);
}
else if (connectionLimit < 20)
{
smtp.ServicePoint.MaxIdleTime = (connectionLimit * 2000);
}
else
{
smtp.ServicePoint.MaxIdleTime = 60000;
}
smtp.ServicePoint.ConnectionLimit = connectionLimit;
}
public bool ServerCertificateValidationCallback(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return sslPolicyErrors == System.Net.Security.SslPolicyErrors.None;
}
public void ConnectionLimit(int value)
{
connectionLimit = value;
smtp.ServicePoint.ConnectionLimit = connectionLimit;
}
public bool SendMail(string fromAddress, string toAddress, string subject,
string body, ref string errMsg, string fromEmailName = "")
{
MailMessage mm = new MailMessage();
MailAddress emailFrom = new MailAddress(fromAddress, fromEmailName);
try
{
mm.To.Add(new MailAddress(toAddress));
mm.From = emailFrom;
mm.Subject = subject;
mm.Body = body;
mm.IsBodyHtml = true;
smtp.Send(mm);
mm.Dispose();
}
catch (Exception ex)
{
errMsg = ex.Message;
if (!(ex.InnerException == null)) { errMsg += " (" + ex.InnerException.Message + ")"; }
return false;
}
return true;
}
public void Close()
{
smtp.Dispose();
}
}
Used like this:
EmailRelay thisMailer = new EmailRelay("server name", "user@gmail.com", "password", useSslOrNotBool, portNumber);
thisMailer.SendMail("from@whatever.com", "to@whatever.com", "Email subject", "Email Body - use html", ref errMsg, "From friendly name");
Set
_connectionLimit
higher then 6 in the constructor if you are using multithreding to increase performance while sending multiple mails. The higher
_connectionLimit
is, the faster you will be able to send mails.
But fair warning: gmail has sending limits, and according to their documentation will accept about 1 mail per second. If you send too many faster then that, they will cut you off for a while.
-Pete