Hi All,
I was wondering if someone could help me. I've got a simple Suppliers table that has the Email Addresses for my Suppliers.
I need to loop through the email addresses 'SuppEmail' in the Suppliers table in the SQL Database 'SpecCars' and send them all the email below.
I've been at it for a few days now, looking on-line and trying many different variations, but no matter what I do, it only sends one email
to the first entry '[email protected]' in the table and that's it. I don't think my loop is correct, I'm putting the SuppEmail into
the variable emailnew, but it doesn't seem to work. If you could help, that would be fantastic. It's an ASP.NET C# Solution.
// This is the Suppliers Table, it just has two record in there:
..................................................
use SpecCars
Go
CREATE table Suppliers(
SuppId INT IDENTITY(1,1) PRIMARY KEY,
SuppName NVARCHAR(60) NOT NULL,
SuppAddress NVARCHAR(150) NOT NULL,
SuppSuburb NVARCHAR(60) NOT NULL,
SuppState NVARCHAR(30) NOT NULL,
SuppPost NVARCHAR(10) NOT NULL,
SuppPhone NVARCHAR(10) NOT NULL,
SuppEmail NVARCHAR(100) NOT NULL,
SuppCode NVARCHAR(10) NOT NULL
)
Go
Command(s) completed successfully.
Insert into Suppliers (SuppName, SuppAddress, SuppSuburb, SuppState, SuppPost, SuppPhone, SuppEmail, SuppCode) values ('Jacks Auto', '2 Jill Street', 'Belgrade', 'VIC', '3299', '9555 4457', '[email protected]', 'JACBLA')
Insert into Suppliers (SuppName, SuppAddress, SuppSuburb, SuppState, SuppPost, SuppPhone, SuppEmail, SuppCode) values ('Ultimate Lights', '205 Browns Road', 'Tullamarine', 'VIC', '3011', '9877 2255', '[email protected]', 'ULTTUL')
(2 row(s) affected)
..................................................
//This is the code snippet :
SqlDataReader sqlData;
SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=SpecCars;Integrated Security=True");
connection.Open();
sqlData = new SqlCommand("Select SuppEmail From Suppliers", connection).ExecuteReader();
int count = sqlData.FieldCount;
while (sqlData.Read())
{
for (int i = 0; i < count; i++)
{
string emailnew = sqlData[i].ToString();
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("myemail.com");
mailMessage.To.Add("myemail.com");
mailMessage.To.Add(emailnew);
//mailMessage.CC.Add(emailnew);
mailMessage.Subject = "Assembly Line Stop";
mailMessage.Priority = MailPriority.High;
mailMessage.Body = "Please be advised that the assembly line at Specialised Cars has STOPPED. You will be notified once the line has started again. Any Services between the LINE STOP and the LINE START will be carried out after 19:00 (7pm).";
mailMessage.IsBodyHtml = true;
SmtpClient smtpClient = new SmtpClient("smtp-mail.myprovider.com", 587);
smtpClient.EnableSsl = true;
smtpClient.Credentials = new System.Net.NetworkCredential("myemail.com", "password");
smtpClient.Send(mailMessage);
}
}
connection.Close();
..................................................
ali tuncerPosted Mar 31, 2016, 9:18 PM
Hi Charles,
What was the problem? what did you change?
Charles FrankPosted Apr 1, 2016, 3:00 AM
//-----------------
//Hi Ali, It wasn't looping through the MailMessage properly, now it puts each email address into the string variable 'emailnew', then adds it to the MailMessage 'message.To.Add(emailnew)' and sends the email one at a time using 'smtpClient.Send(message);'. It appears to be in the right order now. Thanks Heaps for your Help Ali, I really appreciate it.
//Trying8.aspx - Finally Works, it sends LINE STOP Email to every SuppEmail recipient in Suppliers Table
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Trying8.aspx.cs" Inherits="SpecCars.Admin.Trying8" %>
//-----------------
//Trying8.aspx.cs - Finally Works, it sends LINE STOP Email to every SuppEmail recipient in Suppliers Table
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Net;
using System.Net.Mail;
using System.Text;
namespace SpecCars.Admin
{
public partial class Trying8 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlDataReader sqlData;
SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=SpecCars;Integrated Security=True");
connection.Open();
sqlData = new SqlCommand("Select * From Suppliers", connection).ExecuteReader();
using (SmtpClient smtpClient = new SmtpClient("smtp-mail.provider.com", 587))
{
while (sqlData.Read())
{
string emailnew = sqlData["SuppEmail"].ToString();
Label1.Text = emailnew.ToString();
using (MailMessage message = new MailMessage())
{
try
{
message.From = new MailAddress("[email protected]");
// This doesn't Send (To:) [email protected]
MailAddress AddressTo = new MailAddress("[email protected]");
// This does Send (To:) to SuppEmail recipient in Suppliers Table
message.To.Add(emailnew);
//This does Send a (CC:) [email protected]
message.CC.Add("[email protected]");
message.Subject = "Assembly Line Stop";
message.Priority = MailPriority.High;
message.Body = "Please be advised that the assembly line at Specialised Cars has STOPPED. You will be notified once the line has started again. Any Services between the LINE STOP and the LINE START will be carried out after 19:00 (7pm).";
message.IsBodyHtml = true;
smtpClient.EnableSsl = true;
smtpClient.Credentials = new System.Net.NetworkCredential("[email protected]", "password");
smtpClient.Send(message);
// smtpClient.Dispose();
// message.Dispose();
}
catch (Exception ex)
{
//log exceptions here, you can write it to a txt file, or to a label in your form for testing purpose
//we are trying to see if you get an exception..
Label1.Text = ex.Message;
}
}
}
}
}
}
}
//-----------------
Charles FrankPosted Mar 31, 2016, 7:56 PM
-----------------
Hi All, just a quick update:
Trying8.aspx - Finally Works, it sends LINE STOP Email to every SuppEmail recipient in Suppliers Table
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Trying8.aspx.cs" Inherits="SpecCars.Admin.Trying8" %>
-----------------
EntCar.aspx.cs - Finally Works, it sends LINE STOP Email to every SuppEmail recipient in Suppliers Table
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Net;
using System.Net.Mail;
using System.Text;
namespace SpecCars.Admin
{
public partial class Trying8 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlDataReader sqlData;
SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=SpecCars;Integrated Security=True");
connection.Open();
sqlData = new SqlCommand("Select * From Suppliers", connection).ExecuteReader();
using (SmtpClient smtpClient = new SmtpClient("smtp-mail.provider.com", 587))
{
while (sqlData.Read())
{
string emailnew = sqlData["SuppEmail"].ToString();
Label1.Text = emailnew.ToString();
using (MailMessage message = new MailMessage())
{
try
{
message.From = new MailAddress("[email protected]");
// This doesn't Send (To:) [email protected]
MailAddress AddressTo = new MailAddress("[email protected]");
// This does Send (To:) [email protected]
// message.To.Add("[email protected]");
// This does Send (CC:) to SuppEmail recipient in Suppliers Table
message.CC.Add(emailnew);
message.Subject = "Assembly Line Stop";
message.Priority = MailPriority.High;
message.Body = "Please be advised that the assembly line at Specialised Cars has STOPPED. You will be notified once the line has started again. Any Services between the LINE STOP and the LINE START will be carried out after 19:00 (7pm).";
message.IsBodyHtml = true;
smtpClient.EnableSsl = true;
smtpClient.Credentials = new System.Net.NetworkCredential("[email protected]", "password");
smtpClient.Send(message);
// smtpClient.Dispose();
// message.Dispose();
}
catch (Exception ex)
{
//log exceptions here, you can write it to a txt file, or to a label in your form for testing purpose
//we are trying to see if you get an exception..
Label1.Text = ex.Message;
}
}
}
}
}
}
}
-----------------
ali tuncerPosted Mar 29, 2016, 3:49 AM
You could try asynchronous mail:
Charles FrankPosted Mar 29, 2016, 3:04 AM
Ali, Here is the code. With Dispose it still sends only one email and I put the Error in a Label - Cannot access a disposed object. Object name: 'System.Net.Mail.SmtpClient'.
If I don't use Dispose, it throws no Errors and still sends only one email.
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Net;
using System.Net.Mail;
using System.Text;
namespace SpecCars.Admin
{
public partial class Trying3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlDataReader sqlData;
SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=SpecCars;Integrated Security=True");
connection.Open();
sqlData = new SqlCommand("Select SuppEmail From Suppliers", connection).ExecuteReader();
using (SmtpClient smtpClient = new SmtpClient("smtp-mail.provider.com", 587))
{
while (sqlData.Read())
{
using (MailMessage message = new MailMessage())
{
try
{
string emailnew = sqlData["SuppEmail"].ToString();
message.From = new MailAddress("myemail.com");
message.To.Add(emailnew);
MailAddress AddressTo = new MailAddress("myemail.com");
message.To.Add("myemail.com");
//add rest of your code here
message.Subject = "Assembly Line Stop";
message.Priority = MailPriority.High;
message.Body = "Please be advised.";
message.IsBodyHtml = true;
smtpClient.EnableSsl = true;
smtpClient.Credentials = new System.Net.NetworkCredential("mymail.com", "password");
smtpClient.Send(message);
smtpClient.Dispose();
message.Dispose();
}
catch (Exception ex)
{
//log exceptions here, you can write it to a txt file, or to a label in your form for testing purpose
//we are trying to see if you get an exception..
Label1.Text = ex.Message;
}
}
}
}
}
}
}
ali tuncerPosted Mar 29, 2016, 2:27 AM
Hi Charles, I think you should be getting exception, if you don't, might be something wrong with email address, did you try different addresses?
SqlDataReader sqlData;
SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=SpecCars;Integrated Security=True");
connection.Open();
sqlData = new SqlCommand("Select SuppEmail From Suppliers", connection).ExecuteReader();
using (SmtpClient smtpClient = new SmtpClient())
{
while (sqlData.Read())
{
using (MailMessage message = new MailMessage())
{
try
{
string emailnew = sqlData["SuppEmail"].ToString();
message .From = new MailAddress("myemail.com");
message.To.Add(emailnew);
//add rest of your code here
smtpClient.EnableSsl = true;
smtpClient.Credentials = new System.Net.NetworkCredential("myemail.com", "password");
smtpClient.Send(mailMessage)
}
catch (Exception ex)
{
//log exceptions here, you can write it to a txt file, or to a label in your form for testing purpose
//we are trying to see if you get an exception..
//label1.text = ex.Message;
}
}
}
}
Charles FrankPosted Mar 29, 2016, 1:35 AM
Charles FrankPosted Mar 29, 2016, 1:32 AM
ali tuncerPosted Mar 29, 2016, 1:17 AM
Does it give error message?
You could try to dispose objects after you send.
mailMessage.Dispose();
Saillesh PawarPosted Mar 29, 2016, 1:07 AM
Charles FrankPosted Mar 29, 2016, 1:01 AM
ali tuncerPosted Mar 29, 2016, 12:58 AM
Also make sure email addresses are correct, you are inserting '[email protected]', it might be 'orders@ultimatelights.com.au'
Ideally, it should work with for loop too, but there is no need..
Charles FrankPosted Mar 29, 2016, 12:56 AM
Hi Ali, I tried the amendments below and it's still only sending to the first SuppEmail not the second. Is there anything else I could try.
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Net;
using System.Net.Mail;
using System.Text;
namespace SpecCars.Admin
{
public partial class Trying2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlDataReader sqlData;
SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=SpecCars;Integrated Security=True");
connection.Open();
sqlData = new SqlCommand("Select SuppEmail From Suppliers", connection).ExecuteReader();
// int count = sqlData.FieldCount;
while (sqlData.Read())
{
// for (int i = 0; i < count; i++)
// {
string emailnew = sqlData["SuppEmail"].ToString();
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("myemail.com");
// mailMessage.To.Add("myemail.com");
mailMessage.To.Add(emailnew);
//mailMessage.CC.Add(emailnew);
mailMessage.Subject = "Assembly Line Stop";
mailMessage.Priority = MailPriority.High;
mailMessage.Body = "Please be advised that the assembly line at Specialised Cars has STOPPED. You will be notified once the line has started again. Any Services between the LINE STOP and the LINE START will be carried out after 19:00 (7pm).";
mailMessage.IsBodyHtml = true;
SmtpClient smtpClient = new SmtpClient("smtp-mail.myprovider.com", 587);
smtpClient.EnableSsl = true;
smtpClient.Credentials = new System.Net.NetworkCredential("myemail.com", "password");
smtpClient.Send(mailMessage);
}
connection.Close();
}
}
}
ali tuncerPosted Mar 29, 2016, 12:27 AM
SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=SpecCars;Integrated Security=True");
connection.Open();
sqlData = new SqlCommand("Select SuppEmail From Suppliers", connection).ExecuteReader();
while (sqlData.Read())
{
string emailnew = sqlData["SuppEmail"].ToString();
//here you could check if emailnew is a valid email(optional)
MailMessage mailMessage = new MailMessage();
//send e-mail code here
}
connection.Close();
Saillesh PawarPosted Mar 29, 2016, 12:21 AM