Click here to Skip to main content
15,885,914 members
Articles / Web Development / ASP.NET

Send 1000s of Emails Without Timeout

Rate me:
Please Sign up or sign in to vote.
4.53/5 (18 votes)
13 Dec 2009CPOL2 min read 61.2K   2.8K   83  
Now you can send more than 1000 emails with a simple website and a process
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using NewsletterService.Classes;

namespace NewsletterService
{
    class Program
    {
        private static DataTable MemberTable = new DataTable();
        private static string subject = "";
        protected static string body = "";
        protected static string smtp = "";
        protected static string senderEmail = "";
        protected static bool sendViaSql = false;

        static void Main(string[] args)
        {
            //build the datatable
            MemberTable.Columns.Add("Id", typeof(int));
            MemberTable.Columns.Add("EmailAddress", typeof(string));
            MemberTable.Columns.Add("Body", typeof(string));

            subject = args[0];
            body = args[1];
            smtp = args[2];
            senderEmail = args[3];
            sendViaSql = Convert.ToBoolean(args[4]);
            
            //get members from database
            DataTable members = Classes.EmailUtility.GetEmails();
            foreach (DataRow row in members.Rows)
            {
                DataRow memberRow = MemberTable.NewRow();
                memberRow["Id"] = row["Id"];
                memberRow["EmailAddress"] = row["Email"];
                memberRow["Body"] = body;
                MemberTable.Rows.Add(memberRow);
            }

            System.Web.Mail.MailMessage msg = new System.Web.Mail.MailMessage();

            msg.Subject = subject;
            msg.Body = body;
            msg.From = senderEmail;
            msg.BodyFormat = System.Web.Mail.MailFormat.Html;

            foreach (DataRow row in MemberTable.Rows)
            {
                try
                {
                    msg.To = row["EmailAddress"].ToString();
                    if (sendViaSql) //send through SQL Server
                    {
                        EmailUtility.SendEmail(msg.To, msg.Body, msg.Subject);
                    }
                    else
                    {
                        //send through via SMTP Server
                        System.Web.Mail.SmtpMail.SmtpServer = smtp;
                        System.Web.Mail.SmtpMail.Send(msg);
                    }
                }
                catch { }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader IDS
Lebanon Lebanon
Adore programming, interested in workflows, SharePoint and silverlight, Entity Framework.
My Blog: http://suhamneimne.wordpress.com

Comments and Discussions