Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Mass Email

Posted on 2011-09-26
2
Medium Priority
?
184 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 1000 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 1000 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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

618 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