Thursday, January 31, 2008

Send a huge range of email (bulk)

if (e.CommandName == "Send")
{
DataSet _dsSubscribersList = new DataSet();
_dsSubscribersList = _objNL.GetSubscribersList();
int count = 0;
if (_dsSubscribersList.Tables.Count > 0 && _dsSubscribersList.Tables[0].Rows.Count > 0)
{
DataSet _dsNewsletter = new DataSet();
_dsNewsletter = _objNL.GetNewsLetterDetailsByNewsletterID(
Convert.ToInt32(e.CommandArgument));
if (_dsNewsletter.Tables.Count > 0 && _dsNewsletter.Tables[0].Rows.Count > 0)
{
try
{
string From = ConfigurationManager.AppSettings["NewslettersSender"].ToString();
string To = null;
string body = HttpUtility.HtmlDecode(_dsNewsletter.Tables[0].Rows[0]["NewsBody"].ToString());
for (int i = 0; i < _dsSubscribersList.Tables[0].Rows.Count; i++)
{
To = (
string)_dsSubscribersList.Tables[0].Rows[i]["Email"];
body = body.Replace("[Subscriber Name]", To);
body = body.Replace(
"[userid]", _dsSubscribersList.Tables[0].Rows[0]["ID"].ToString());
try
{
//System.Threading.Thread.Sleep(4000);
SendMail(To, From, (string)_dsNewsletter.Tables[0].Rows[0]["Subject"], body);
count++;
}
catch
{
count--;
}
}
}
catch //(Exception ex)
{
lblMsg1.Text =
"Sending newsletting failure. Please try again.";//ex.Message +"sri 1"; //
lblMsg1.ForeColor = Color.Red;
return;
}
int x = 0;
x = _objNL.Newsletters_SetHasSent(
Convert.ToInt32(e.CommandArgument));
if (x == 1)
{
BindNewsLetters();
lblMsg1.Text =
"Newsletter send successfully to " + count + " subscribers. ";
dvRepeaters.Style.Value =
"display:block";
dvProgress.Style.Value =
"display:none";
}
}
else
{
lblMsg1.Text = "No newsletters to send!!";
}
}
else
{
lblMsg1.Text =
"No subscriber to send newsletter!!";
}
}

-----------------------------------------------------------------------------------------
//send email

private void SendMail(string To, string From, string Subject, string body)
{
MailMessage _mm = new MailMessage(From, To, Subject, body);
_mm.IsBodyHtml =
true;
SmtpClient _sc = new SmtpClient();
_sc.Send(_mm);
}

No comments: