Solved

Mass Email

Posted on 2011-09-26
2
172 Views
Last Modified: 2012-05-12
Hey guys i am coding in c# and have a databse with email address, i want to send out newletter to them.

please help?
0
Comment
Question by:JCWEBHOST
2 Comments
 
LVL 16

Accepted Solution

by:
disrupt earned 250 total points
ID: 36599040
Take a look at this:

http://www.aspneter.com/2009/05/sending-massbulk-e-mail-using-aspnet/#more-90

Shows you how to attach emails from database and mass send.

code in c#:

protected void btnImport_Click(object sender, System.EventArgs e)
{
	SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings("MyConn").ConnectionString);
	try {
		connection.Open();
		foreach (string txt in Directory.GetFiles(Server.MapPath("Email_Lists"))) {
			string[] emails = System.IO.File.ReadAllLines(txt);
			System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
			foreach (string email in emails) {
				if (reg.IsMatch(email)) {
					SqlCommand cmd = new SqlCommand();
					cmd.Connection = connection;
					cmd.CommandText = "INSERT INTO EmailAddresses (Email) VALUES(@email)";
					cmd.CommandType = System.Data.CommandType.Text;
					cmd.Parameters.Add("@email", System.Data.SqlDbType.VarChar).Value = email;
					cmd.ExecuteNonQuery();
					cmd.Dispose();
				}
			}
		}
	} catch (Exception ex) {
		Response.Write(ex.Message);
	} finally {
		connection.Close();
	}
}

Open in new window


private void StartCampaign()
{
	SqlConnection connection = new SqlConnection("Data Source=xx.xxx.xxx.xxx,1433;" + "Initial Catalog=MyDB;" + "User ID=xxxxxx;Password=xxxxxxxx;");

	try {
		connection.Open();
		SqlCommand command = new SqlCommand("SELECT TOP(@count) Email FROM EmailAddresses", connection);
		command.Parameters.Add("@count", System.Data.SqlDbType.Int).Value = this.txtNumberOfMessages.Text;
		SqlDataReader reader = command.ExecuteReader;

		while (reader.Read) {
			// send new email to all returned addresses
			SendEmail(reader(0).ToString);
		}
		reader.Close();
	} catch (Exception ex) {
	// ToDo: something about the exception if any
	} finally {
		connection.Close();
	}

}

private void SendEmail(string email)
{
	// declare new mail message
	MailMessage message = new MailMessage();
	// if you plan to have html content then enable property IsBodyHtml
	message.IsBodyHtml = true;
	// set the message subject
	message.Subject = this.txtSubject.Text.Trim;
	message.From = new MailAddress("youremail@domain.com", "Your Company Name - Newsletter");
	message.To.Add(new MailAddress(email));
	// set the message body
	message.Body = this.txtMessage.Text;
	// declare new smtpclient to send our message
	SmtpClient mailClient = new SmtpClient(this.txtSmtp.Text, this.txtSmtpPort.Text);
	mailClient.Send(message);
	message.Dispose();
}

Open in new window

0
 
LVL 17

Assisted Solution

by:OriNetworks
OriNetworks earned 250 total points
ID: 36599341
The suggested code will work but may require some additions. The key to this is being able to limit how many people it sends to at one time to prevent issues with limits on your email server and spam filtering, which it does but requires manual intervention. Also being able to report which addresses already completed.
For example, if you had a table that loads the email addresses (EmailAddress varchar(100), SentOn smalldatetime), then 'select top(@Count) EmailAddress from MyTable where SentOn is null' gives you the next addresses then for each number of records that you want to send at a time (e.g. 50 recipients at a time) it sends the message then updates the record with the timestamp to move onto the next 50.  That would be the way I would go.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now