Solved

Mass Email

Posted on 2011-09-26
2
178 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

724 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