Reputation: 55
i need to send mails to 60 to 100 students at a time when i click on Send Button. It is working good if i have 6 to 7 students but when i start sending to 60 students my code shows the following error
Insufficient system storage. The server response was: Too many emails per connection.
Here is My code
protected void btnsend_Click(object sender, EventArgs e) {
if (drptopics.SelectedValue == "-1")
{
Response.Write(@"<script language='javascript'>alert('Please select one Topic');</script>");
}
else
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=xxxx; User Id=sa; Password=xxxx; Initial Catalog=xxxx; Integrated Security=SSPI;";
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select EMail_1 from Student_Info where Branch_Code='ap' and Student_ID in(select Student_ID from Admissions where Branch_Code='ap' and Batch_ID='" + txtbatchid.Text + "')";
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows[0][0] != null)
{
int count = ds.Tables[0].Rows.Count;
for (int i = 0; i < count; i++)
{
MailMessage mm = new MailMessage();
mm.To.Add(new MailAddress(ds.Tables[0].Rows[i][0].ToString()));
mm.From = new MailAddress("[email protected]");
mm.Subject = "SMS and Interview Questions of Oracle 11g DBA";
mm.IsBodyHtml = true;
//Day 1 Architecture 1
if (drptopics.SelectedValue == "1")
{
Attachment attachment1 = new Attachment(Server.MapPath("~/Oracle 11g DBA/Day 1/Architecture-1-I.doc")); //create the attachment
mm.Attachments.Add(attachment1);
Attachment attachment2 = new Attachment(Server.MapPath("~/Oracle 11g DBA/Day 1/Architecture1-S.doc")); //create the attachment
mm.Attachments.Add(attachment2);
}
//Day 2 Architecture 2
else if (drptopics.SelectedValue == "2")
{
Attachment attachment1 = new Attachment(Server.MapPath("~/Oracle 11g DBA/Day 2/Architecture-2-I.doc")); //create the attachment
mm.Attachments.Add(attachment1);
Attachment attachment2 = new Attachment(Server.MapPath("~/Oracle 11g DBA/Day 2/Architecture2-S.doc")); //create the attachment
mm.Attachments.Add(attachment2);
}
mm.Body = "<html><head><body><h1>Thank you for choosing Wilshire</h1><br><b>Team Wilshire<b></body></head></html>";
SmtpClient s = new SmtpClient("smtp.net4india.com");
s.Send(mm);
}
}
}
Please tell me the solution........................
Upvotes: 3
Views: 988
Reputation: 12721
SmtpClient.SendAsync()
could lead to other problems (SMTP servers set a maximum mail number per minute to avoid spam)!
Since the mails are identical, the solution is 1 single mail with all recipients as hidden recipients (Bcc
)!
MailMessage mm = new MailMessage();
for (int i = 0; i < count; i++)
{
mm.Bcc.Add(new MailAddress(ds.Tables[0].Rows[i][0].ToString()));
}
mm.To.Add(new MailAddress("[email protected]");
mm.From = new MailAddress("[email protected]");
mm.Subject = "SMS and Interview Questions of Oracle 11g DBA";
....
SmtpClient s = new SmtpClient("smtp.net4india.com");
s.Send(mm);
Upvotes: 2
Reputation: 2607
May be You can also use ASP.NET4.5 New feature if you are working on the latest framework.
Refer Link:
http://www.codeproject.com/Articles/599756/Five-Great-NET-Framework-4-5-Features
Feature 1: async and await (code markers)
Upvotes: 1
Reputation: 4489
Use SmtpClient.SendAsync Method
to send mail it's sending mail background one by one.
Upvotes: 1
Reputation: 69959
The error says 'Too many emails per connection'. Why not send fewer emails per connection? Add a loop and dispose of the connection every few e-mails and then create a new one?
Upvotes: 1