//i have left out some parameters for readability
public static string MailForCandidate(string username, string FromAddr, string ToAddr, string strSubject)
{
try
{
MailMessage msg = new MailMessage();
string strMailBody = "Test body";
msg.To = ToAddr;
msg.From = FromAddr;
msg.BodyFormat = MailFormat.Html;
msg.Priority = MailPriority.High;
msg.Subject = strSubject;
msg.Body = strMailBody.ToString();
SmtpMail.SmtpServer = ConfigurationManager.AppSettings["mailServer"].ToString();
try
{
SmtpMail.Send(msg);
msg = null;
return "";
}
catch (Exception ex)
{
Util.Common.LogErrorMessage(ex);
msg = null;
return username;
}
}
catch (Exception ex)
{
throw ex;
}
}
I have the above function that i'm using to send emails from an IIS hosted .NET 3.5 asp.net application.
The emails are sent at the click of a button and the recipients are being read from a database table.
The problem i'm having is that some recipients get their emails with out any issue, others take far too long to receive their emails (sometimes after 24 hours) at which time the event they are supposed to participate in has expired (and this has legal implications to my company). And then finally, others do not receive the email completely.
The above MailForCandidate
function is being called from SendCandidateMail
below.
private void SendCandidateMail(int intEmailType)
{
try
{
ArrayList arrPending = new ArrayList();
ArrayList arrUnsent = new ArrayList();
string strCandidatename = string.Empty;
string stractualname = string.Empty;
int intUnsentCandCount = 0;
if (hdnUnsentNames.Value.Trim() != string.Empty)
{
arrUnsent.AddRange(hdnUnsentNames.Value.Split(','));
}
if (hdnPendingNames.Value.Trim() != string.Empty)
{
arrPending.AddRange(hdnPendingNames.Value.Split(','));
}
hdnUnsentNames.Value = string.Empty;
hdnPendingNames.Value = string.Empty;
if (!string.IsNullOrEmpty(hdnUnsent.Value) && !string.Empty.Equals(hdnUnsent.Value))
{
string[] strUnsIds = hdnUnsent.Value.Split('~');
for (int i = 0; i < strUnsIds.Length; i++)
{
DataSet dtsetCandidate = CandidatesListBL.GetCandidateDetails(Convert.ToInt32(strUnsIds[i]));
stractualname = arrUnsent[i].ToString();
if (dtsetCandidate.Tables[0].Rows.Count > 0)
{
if (dtsetCandidate.Tables[0].Rows[0]["Time"].ToString() != "0")
{
//i have left out some parameters for readability
strCandidatename = SendMail.MailForCandidate(dtsetCandidate.Tables[0].Rows[0]["User_Id"].ToString(), intEmailType);
}
else
strCandidatename = SendMail.MailForCandidateNoTime(dtsetCandidate.Tables[0].Rows[0]["User_Id"].ToString(), intEmailType);
if (strCandidatename.Trim().Equals(string.Empty))
{
hdnUnsentNames.Value = hdnUnsentNames.Value + stractualname + ",";
intUnsentCandCount = intUnsentCandCount + 1;
if (Convert.ToInt32(EnumMaster.EmailType.Customized) != intEmailType)
{
CandidatesListBL.UpdateCandidateStatus(Convert.ToInt32(strUnsIds[i]), "Sent");
CandidatesListBL.UpdateDateSent(Convert.ToInt32(strUnsIds[i]));
}
}
}
}
hdnUnsent.Value = string.Empty;
}
}
catch (Exception ex)
{
WESMessage.DisplayMessage(this, this.UpdatePanel1, DanielHac.TamperProofString.QueryStringEncode("MailFailed"), this.strinfo);
Common.LogErrorMessage(ex);
}
}
Below is what is being logged in the error log text file.
01/22/2017 3:23:04 PM ==> Exception Message: Thread was being aborted. 01/22/2017 3:23:04 PM ==> Exception Source: App_Code Exception Target Site: System.String MailForCandidate(System.String, System.String, System.String, System.Collections.ArrayList, System.String, System.String, System.String, System.String, System.String, System.String, UserSession, System.String, System.String, Int32, Int32) Exception Stack Trace: at WES.Util.SendMail.MailForCandidate(String strUserID, String username, String password, ArrayList alFiles, String FromName, String FromAddr, String ToAddr, String title, String strSubject, String strCustomMsg, UserSession UserObj, String strDeadLine, String strTime, Int32 intOfficeId, Int32 intMailType) at Pages_ExerciseDetails.SendCandidateMail(Int32 intEmailType) Exception To String: System.Threading.ThreadAbortException: Thread was being aborted. at WES.Util.SendMail.MailForCandidate(String strUserID, String username, String password, ArrayList alFiles, String FromName, String FromAddr, String ToAddr, String title, String strSubject, String strCustomMsg, UserSession UserObj, String strDeadLine, String strTime, Int32 intOfficeId, Int32 intMailType) at Pages_ExerciseDetails.SendCandidateMail(Int32 intEmailType)
And
01/22/2017 3:23:04 PM ==> Exception Message: Thread was being aborted. 01/22/2017 3:23:04 PM ==> Exception Source: App_Web_kxc2lbj5 Exception Target Site: Void SendCandidateMail(Int32) Exception Stack Trace: at Pages_ExerciseDetails.SendCandidateMail(Int32 intEmailType) at Pages_ExerciseDetails.ibtnSend_Click(Object sender, EventArgs e) Exception To String: System.Threading.ThreadAbortException: Thread was being aborted. at Pages_ExerciseDetails.SendCandidateMail(Int32 intEmailType) at Pages_ExerciseDetails.ibtnSend_Click(Object sender, EventArgs e)
Also
01/22/2017 3:23:04 PM ==> Exception Message: Request timed out. 101/22/2017 3:23:04 PM ==> Exception Source: Exception Target Site: Exception Stack Trace: Exception To String: System.Web.HttpException: Request timed out.
Then
101/22/2017 3:31:35 PM ==> Exception Message: Exception of type 'System.Exception' was thrown. 01/22/2017 3:31:35 PM ==> Exception Source: App_Web_kxc2lbj5 Exception Target Site: Void SendCandidateMail(Int32) Exception Stack Trace: at Pages_ExerciseDetails.SendCandidateMail(Int32 intEmailType) Exception To String: System.Exception: Exception of type 'System.Exception' was thrown. at Pages_ExerciseDetails.SendCandidateMail(Int32 intEmailType)
I'm find it hard to troubleshot because it is occurring in production ONLY. In QA and Dev, emails are just going fine using the same SMTP server.
I will just post my comment here as the answer.