Solved

Mass Email

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

773 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