I have the scheduler to read files(CSV) and send mail to the Emails, which is found from that document.
I am using the Scheduler with gmail's/our's smtp on local machine made no issues, while sending mail even with wrong "To:" mail id.
but in hosting when scheduler send mail to wrong mail id, scheduler stops at smtp.Send(mail) and Error caught.
public override void DoWork()
{
try
{
try
{
this.Progressing();
string[] files = System.IO.Directory.GetFiles(System.Web.Hosting.HostingEnvironment.MapPath("/NewCSVContactFiles"));
foreach (string filePath in files)
{
DataTable dt = ConvertCSVFileToDataTable(filePath)
sendMails(dt);
}
}
catch (Exception ex)
{
this.ScheduleHistoryItem.AddLogNote(" : " + ex.InnerException.ToString());
//Catch: HERE
}
this.ScheduleHistoryItem.Succeeded = true;
}
catch (Exception ex)
{
//--intimate the schedule mechanism to write log note in schedule history
this.ScheduleHistoryItem.Succeeded = false;
this.ScheduleHistoryItem.AddLogNote("FileProcessSchedule Start. Failed. " + ex.InnerException.ToString());
this.Errored(ref ex);
}
}
public sendMails(Datatable dt)
{
try
{
foreach (DataRow row in dt.Rows)
{
SendEmail(from,password,to,subj,content);
}
}
catch (Exception ex)
{
//Catch: sendMails
}
}
public SendEmail(string From,string Password,string ToMail,string Sub,string Content)
{
try
{
MailMessage mail = new MailMessage();
MailAddress fromMail = new MailAddress(From);
mail.From = fromMail;
mail.To.Add(ToMail);
SmtpClient client = new SmtpClient("smtpout.xxx.net");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Port = 25;
client.Host = "smtpout.xxx.net";
client.EnableSsl = false;
mail.Subject = strSubject;
mail.Body = strBody;
mail.IsBodyHtml = true;
client.Credentials = new System.Net.NetworkCredential(strFrom, Pass);
client.Send(mail); //Stops here but not catch by Catch: SendMails its catch by Catch: HERE and shows null refrence at sendMails(dt)
Only this happened, when To address is not existing (eg: karthikeyanmlp123789@gmail.com (not registered but valid mail format))
}
catch (Exception ex)
{
//Catch: SendMails
}
}
Note: I am not use throw inside any catch