Asked by:
send email asynchronously

Question
-
User-381059448 posted
hello ,
when sending a heavy email and I clicked in another browser window sending mail does not work so I have to wait for the mail loading....
i use thred to avoid this problem but it doesn't work .
help me please very urgent.
protected void SendRejectionsToFiliales()
{
RejectManagmentHelper rejectHelper = new RejectManagmentHelper(this._sSqlConnectionString);
PARAM_AFFILIATE_DAO affiliateDao = new PARAM_AFFILIATE_DAO(this._sSqlConnectionString);HiddenField1.Value = "";
List<ExcelPackage> exelFiles = rejectHelper.CreateLogOfRejectFilesByCompany(HiddenFiledSendRejectMailsLoopCondition.Value);
ServicePointManager.ServerCertificateValidationCallback =delegate (object s, X509Certificate certificate,X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
CONFIGURATION_DAO configDao = new CONFIGURATION_DAO(this._sSqlConnectionString);
CONFIGURATION config = configDao.Get("reject_mail_adress");
string sSender = config.value;
config = configDao.Get("reject_mail_title");
string sSubject = config.value;
config = configDao.Get("reject_mail_content");
string sMsgText = config.value;
config = configDao.Get("reject_mail_smtp_host");
string sSMTPHost = config.value;
config = configDao.Get("reject_mail_port");
string sSMTPPort = config.value;
config = configDao.Get("reject_mail_password");
string sSenderPassword = config.value;foreach (ExcelPackage exelFile in exelFiles)
{
FileInfo fo = exelFile.File;
string sCompanyCode = fo.Name.Substring(4, 3);
PARAM_AFFILIATE affiliate = affiliateDao.GetByCompanyCode(sCompanyCode);
string sMailTo = affiliate.affiliateEmailOfReject;
//HiddenFiledSendRejectMailsLoopCondition.Value = sCompanyCode;
MailMessage objMailMsg = new MailMessage(sSender, sMailTo);
objMailMsg.BodyEncoding = Encoding.UTF8;
objMailMsg.Subject = sSubject;
objMailMsg.Body = sMsgText;
objMailMsg.Priority = MailPriority.High;
objMailMsg.IsBodyHtml = true;
Attachment at = new Attachment(new MemoryStream(exelFile.GetAsByteArray()), fo.FullName);
objMailMsg.Attachments.Add(at);//prepare to send mail via SMTP transport
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Host = sSMTPHost;
System.Net.NetworkCredential basicauthenticationinfo = new System.Net.NetworkCredential(sSender, sSenderPassword);
client.Port = int.Parse(sSMTPPort);
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = basicauthenticationinfo;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
Thread email = new Thread(delegate ()
{
client.Send(objMailMsg);
});
email.IsBackground = true;
email.Start();
}
catch (Exception ex)
{}
}HiddenFiledSendRejectMailsLoopCondition.Value = "0";
}Friday, August 31, 2018 6:26 PM
All replies
-
User-943250815 posted
What about make a Sub with all mail data (as parameters) and call it using
System.Threading.Tasks.Task.Factory.StartNew(() => MySendMailRoutine(<put your paramters here>));Saturday, September 1, 2018 12:06 AM -
User2053451246 posted
hello ,
when sending a heavy email and I clicked in another browser window sending mail does not work so I have to wait for the mail loading....
i use thred to avoid this problem but it doesn't work .
help me please very urgent.
protected void SendRejectionsToFiliales()
{
RejectManagmentHelper rejectHelper = new RejectManagmentHelper(this._sSqlConnectionString);
PARAM_AFFILIATE_DAO affiliateDao = new PARAM_AFFILIATE_DAO(this._sSqlConnectionString);HiddenField1.Value = "";
List<ExcelPackage> exelFiles = rejectHelper.CreateLogOfRejectFilesByCompany(HiddenFiledSendRejectMailsLoopCondition.Value);
ServicePointManager.ServerCertificateValidationCallback =delegate (object s, X509Certificate certificate,X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
CONFIGURATION_DAO configDao = new CONFIGURATION_DAO(this._sSqlConnectionString);
CONFIGURATION config = configDao.Get("reject_mail_adress");
string sSender = config.value;
config = configDao.Get("reject_mail_title");
string sSubject = config.value;
config = configDao.Get("reject_mail_content");
string sMsgText = config.value;
config = configDao.Get("reject_mail_smtp_host");
string sSMTPHost = config.value;
config = configDao.Get("reject_mail_port");
string sSMTPPort = config.value;
config = configDao.Get("reject_mail_password");
string sSenderPassword = config.value;foreach (ExcelPackage exelFile in exelFiles)
{
FileInfo fo = exelFile.File;
string sCompanyCode = fo.Name.Substring(4, 3);
PARAM_AFFILIATE affiliate = affiliateDao.GetByCompanyCode(sCompanyCode);
string sMailTo = affiliate.affiliateEmailOfReject;
//HiddenFiledSendRejectMailsLoopCondition.Value = sCompanyCode;
MailMessage objMailMsg = new MailMessage(sSender, sMailTo);
objMailMsg.BodyEncoding = Encoding.UTF8;
objMailMsg.Subject = sSubject;
objMailMsg.Body = sMsgText;
objMailMsg.Priority = MailPriority.High;
objMailMsg.IsBodyHtml = true;
Attachment at = new Attachment(new MemoryStream(exelFile.GetAsByteArray()), fo.FullName);
objMailMsg.Attachments.Add(at);//prepare to send mail via SMTP transport
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Host = sSMTPHost;
System.Net.NetworkCredential basicauthenticationinfo = new System.Net.NetworkCredential(sSender, sSenderPassword);
client.Port = int.Parse(sSMTPPort);
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = basicauthenticationinfo;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
Thread email = new Thread(delegate ()
{
client.Send(objMailMsg);
});
email.IsBackground = true;
email.Start();
}
catch (Exception ex)
{}
}HiddenFiledSendRejectMailsLoopCondition.Value = "0";
}Saturday, September 1, 2018 5:14 AM -
User-381059448 posted
thanks ryanbesko
WHEN I CLICK ON THE BUTTON SEND (MAIL) I do not have to leave the page of sending since 3 menuises to receive a mail.
but I need to browse my site (leave the page to send) and receive a mail anyway.
and for that I use a threadhelp me please
Saturday, September 1, 2018 10:36 AM -
User-381059448 posted
thanks jzero but
WHEN I CLICK ON THE BUTTON SEND (MAIL) I do not have to leave the page of sending since 3 menuises to receive a mail.
but I need to browse my site (leave the page to send) and receive a mail anyway.
and for that I use a threadhelp me please
Saturday, September 1, 2018 10:37 AM -
User475983607 posted
That requires a background thread to handle the email process. Your web app makes a call to the background process and returns.
You have not mentioned the technology you are using so...
https://docs.microsoft.com/en-us/ef/core/saving/concurrency
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-2.1
Saturday, September 1, 2018 11:09 AM -
User-943250815 posted
ahmedshpt,
This is expected behavior, here you can find some explanation.
https://blogs.msdn.microsoft.com/amitsh/2007/07/31/why-cant-i-execute-two-requests-from-the-same-session-simultaneously-for-an-asp-net-application/In addition check here for some libraries to help with Background Tasks
https://www.hanselman.com/blog/HowToRunBackgroundTasksInASPNET.aspxSaturday, September 1, 2018 12:50 PM -
User-381059448 posted
I try to follow the tutorial and modify the code a lot of times but I can not solve my problem.
please help me
Saturday, September 1, 2018 8:12 PM -
User409696431 posted
First, as has been said, don't put things inside your for loop that can be defined once outside of it.
Second, you seem to be assuming that Send(objMailMsg) is where the time is being <g class="gr_ gr_14 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="14" data-gr-id="14">spen</g>t, as that is what you are trying to do asynchronously.
Is it? What if you comment out sending the mail, and just run the looping code that creates the mail messages. How long does that take? Is that where the time is being spent?
Monday, September 3, 2018 8:03 AM