Solved

Mass Email

Posted on 2011-09-26
2
175 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.

830 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