Answered by:
The connection has timed out The server at localhost is taking too long to respond.

Question
-
User-1693623980 posted
hi,
I have 480 email in database and want to send email to all of them in one click of button:
I fetch all email like below:
public DataTable GetAllEmail() { DataTable dt1 = new DataTable(); using (OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString)) { con.Open(); OleDbCommand cmd = new OleDbCommand("select * from emailBank where senf=@senf and email <> ''", con); cmd.Parameters.AddWithValue("@senf", drl_senf.SelectedValue); cmd.CommandTimeout = 0; OleDbDataAdapter da = new OleDbDataAdapter(cmd); da.Fill(dt1); con.Close(); return dt1; } }
and send email to them like below:
public bool SendMail(string pTo, string subject, string body, string atach) { string Host = Convert.ToString("mail.abc.ir"); string ReceiverEmailAddres = pTo; string SenderEmailAddresss = Convert.ToString("info@abc.ir"); string SenderEmailPassword = Convert.ToString("123"); SmtpClient MyMail = new SmtpClient(); MyMail.Timeout = 20000; MailMessage MyMsg = new MailMessage(); MyMail.Host = Host; MyMsg.To.Add(new MailAddress(ReceiverEmailAddres)); MyMsg.Subject = subject; if (fuAttachment.HasFile) { string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName); MyMsg.Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName)); } MyMsg.IsBodyHtml = true; MyMsg.From = new MailAddress(SenderEmailAddresss); MyMsg.Body = body; MyMail.UseDefaultCredentials = false; NetworkCredential MyCredentials = new NetworkCredential(SenderEmailAddresss, SenderEmailPassword); MyMail.Credentials = MyCredentials; try { MyMail.Send(MyMsg); return true; } catch { return false; } }
but after about 5 minutes I got this error (both in host and locallhost)
The connection has timed out
The server at localhost is taking too long to respond.
I tried
cmd.CommandTimeout = 0;
and
SmtpClient MyMail = new SmtpClient();
MyMail.Timeout = 20000;
but no success
also I tried Connect Timeout=1500 like below
<add name="ConStr" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/db1.mdb; Connect Timeout=1500;" providerName="System.Data.OleDb;" />
but it throws this error:
An exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll but was not handled in user code
Additional information: Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.
and when I change connect Timeout to connection Timeout it throws this error:
An exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll but was not handled in user code
Additional information: Could not find installable ISAM.any Idea? thanks
Tuesday, July 15, 2014 3:05 AM
Answers
-
User-1199946673 posted
so why I recive connection timeout error?It's the web server that times out! The process of sending all the emails takes too long, this shouldn't be done in a web page!
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, July 15, 2014 9:05 AM -
User-1199946673 posted
so do you mean I have to do it with a windows Service?Yes, I think that would be the best option....
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, July 16, 2014 7:47 AM
All replies
-
User831885415 posted
If you are using c# 5 then use Async
Tuesday, July 15, 2014 3:19 AM -
User-1693623980 posted
I have two question
* I fetch emails from database and know the connection is close because
Using {
}
know emails are in datatable and as I know datatable dont need to connection be open,
so why I recive connection timeout error? what's the relation?* also I think I misunderstand Connection timeout
based on what below link says: The time (in seconds) to wait for a connection to open. The default value is 15 seconds.
connection timeout error take place if the connection doesnt open in 15 seconds am I right?
so why when we fetch this data and show them in gridview, theres not any error?
so is it possible the problem not be related to fetching data from database?
cnuonline
If you are using c# 5 then use Async
do you mean something like this tutorial?
http://www.aspsnippets.com/Articles/How-to-send-email-Asynchronously-in-ASPNet-using-Background-Thread.aspx
Tuesday, July 15, 2014 6:55 AM -
User-1199946673 posted
so why I recive connection timeout error?It's the web server that times out! The process of sending all the emails takes too long, this shouldn't be done in a web page!
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, July 15, 2014 9:05 AM -
User-1693623980 posted
thanks hans,
hans_v
It's the web server that times out! The process of sending all the emails takes too long, this shouldn't be done in a web page!so do you mean I have to do it with a windows Service?
Is there any other choice?
Wednesday, July 16, 2014 1:04 AM -
User-1199946673 posted
so do you mean I have to do it with a windows Service?Yes, I think that would be the best option....
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, July 16, 2014 7:47 AM -
User-1693623980 posted
I found another way:
Quartz.net
I test it with about 60 emails, and it worked.
I made a class like this
using System; using System.Data; using System.Data.OleDb; using Quartz; using System.Text.RegularExpressions; /// <summary> /// Summary description for SendSMS2 /// </summary> public class SendSMS2 : IJob { public void Execute(IJobExecutionContext context) { //I called SendMail method for each email in datatable
} public bool SendMail(string pTo, string subject, string body, string atach) { } public DataTable GetAllEmail() { } }
and In global.asaxpublic static void ConfigureQuartzJobs2() { // construct a scheduler factory ISchedulerFactory schedFact = new StdSchedulerFactory(); // get a scheduler IScheduler sched = schedFact.GetScheduler(); sched.Start(); IJobDetail job = JobBuilder.Create<SendSMS2>() .WithIdentity("SendJob") .Build(); var trigger = TriggerBuilder.Create() .WithIdentity("SendTrigger") .WithSimpleSchedule(x => x.WithRepeatCount(0)) //.StartAt(startTime) .StartNow() .Build(); sched.ScheduleJob(job, trigger); }
and I called above method in
void Application_Start(object sender, EventArgs e) { // Code that runs on application startup //ConfigureQuartzJobs(); ConfigureQuartzJobs2(); }
Thanks
resource: http://jalil.faalkhah.com/?m=20131221
only I changed
.WithSimpleSchedule(x => x.WithIntervalInMinutes(1).RepeatForever())
like this:
.WithSimpleSchedule(x => x.WithRepeatCount(0))
becasuse I needed to execute method one times
if anyone see some wrong things in my codes please let me know!
thanks
Thursday, July 17, 2014 3:59 AM