Posts

....
Technical Blog for .NET Developers ©

Thursday, November 28, 2013

Bulk eMail

When we are designing processes for sending bulk emails, we have to keep in mind anti-spam filters and its different policies

These are some of the key points to avoid our sent emails going into the spam folder:
- Launch the process from a server with a consistent IP address
- Keep valid reverse DNS records for the IP address(es) from which you send mail, pointing to your domain
- Add headers to identify the precedence of the email as bulk
- Provide a service to the end user can cancel the subscription
- Send emails one by one instead of collecting all recipients in the To property

You can find more information here (Google Bulk Senders Guidelines), and here (Tips for avoiding spam filters)

The code for sending emails repetitively with these specifications is the next

 
    private void sendEmails(List<emailToSend> emailsToSendList)
    {
        string _sender = ConfigurationManager.AppSettings["sender"];
        string _host = ConfigurationManager.AppSettings["host"];
        string _pswd = ConfigurationManager.AppSettings["pswd"];
        string _port = ConfigurationManager.AppSettings["port"];

        foreach (eMailToSend email in emailsToSendList) {

            try 
            {        
                System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();

                mail.From = new MailAddress("no-reply@yourdomain.com", "no reply");

                mail.To.Add(email.Address);
                mail.Subject = "bulk campaign";

                mail.Body = email.HTMLBody;

                mail.BodyEncoding = System.Text.Encoding.UTF8;
                mail.IsBodyHtml = true;

                mail.Headers.Add("Precedence", "bulk");
                mail.Headers.Add("Message-Id", string.Concat("<", 
                                 DateAndTime.Now.ToString("yyMMdd"), ".", 
                                 DateAndTime.Now.ToString("HHmmss"), "@yourdomain.com>"));

                mail.Priority = MailPriority.Low;

                SmtpClient mailClient = new SmtpClient();

                NetworkCredential basicAuthenticationInfo = 
                        new NetworkCredential(_sender, _pswd);
                mailClient.Credentials = basicAuthenticationInfo;
                mailClient.Port = _port;
                mailClient.Host = _host;

                mailClient.Send(mail);
            } 
            catch (Exception ex) 
            {
                logError(ex.Message + " >> " + ex.StackTrace);

                continue;
            }
        }
    }
        



<METHOD SOFTWARE © 2013>