1

i am trying to avoid the wait time, taken by sending an email before exiting page.

the page/application is doing a sequence of actions before exiting as follows :

  1. database interaction
  2. Send Email (updates a manager about that transaction)
  3. Exit page / application .

code behind function :

    ExecEntryOnTbl(SQL);// <-- update / insert to database 
    sendMailNote(action);// <-- send mail with notification of update 

    exitTC(action, custid);<-- exit page.

    done via javascript :
    window.location.href = "someOtherPage.aspx"
    from code behind via 
    RegisterClientScriptBlock(...)

i would like to solve the issue :

how can i avoid waiting for the sendMailNote() to complete before following exitTC() could be executed. is that possible ?

  • update email class/method

    public static class mail
    {
        public static string aReciver, bSubject, cBody;
        public static void sendMailNoteExc()
        {
    
            string SmtpServer = "smtp.gmail.com";
            int port = 111;
            string sender = "aaa@bbb.com";
            string ReCiver = aReciver;
            string Subject = bSubject;
            string Body = cBody;
            string account = "acc@domain.com";
            string Pass = "a123456";
            Send(SmtpServer, port, account, Pass, sender, ReCiver, Subject, Body);
    
        }
        public static void Send(string smtpServer,int Port,string Account, string PassWord, string From, string To, string Subject, string Body)
        {
    
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient(smtpServer);
    
            mail.From = new MailAddress(From);
            mail.To.Add(To);
            mail.Subject = Subject;
            mail.Body = Body;
    
    
            SmtpServer.Port = Port;
            SmtpServer.Credentials = new System.Net.NetworkCredential(Account, PassWord);
            SmtpServer.EnableSsl = true;
    
            SmtpServer.Send(mail);
    
    
        }
    }
    
LoneXcoder
  • 2,121
  • 6
  • 38
  • 76

3 Answers3

3

As long as you are using C# 4.0 you can take advantage of Task Parallel Library and run sendMailNote(action) in the background thread like this:

Task.Factory.StartNew(() => sendMailNote(action));

Just make sure you add a code to handle any exceptions occured during sending an email. However the better solution would be to remove this logic from the user interface code altogether and execute it in a background task like windows service or cloud worker role.

Sergey Rybalkin
  • 3,004
  • 22
  • 26
  • did you see last answer `@Ehsan Aleem Avee` is it necessary to specify tasks properties inorder for them to act as specified? – LoneXcoder Oct 17 '12 at 19:04
  • or is this totally different class /methods he is using ? – LoneXcoder Oct 17 '12 at 19:06
  • The idea behind both answers is the same, but he is using a more low level API to use a background thread. If you are able to use TPL then I suggest you to do so as it is easier to use and will work faster as it is using .NET Framework thread pool instead of creating a new thread. – Sergey Rybalkin Oct 17 '12 at 21:46
2

Do it in another thread. as a result your main thread will not wait for email sending.

ExecEntryOnTbl(SQL);// <-- update / insert to database 

ThreadStart sendMail = delegate()
{
    sendMailNote(action)
};

Thread thread = new Thread(sendMail );
thread.IsBackground = true;
thread.Start();

exitTC(action, custid);<-- exit page.

done via javascript :
window.location.href = "someOtherPage.aspx"
from code behind via 
RegisterClientScriptBlock(...)
  • thanks , is there any difference to the answer that i've accepted as correct (it simply works ) your code tries setting thread properties... etc or did you not take into account the use of availble `Task parallel` when using c#4.0 – LoneXcoder Oct 17 '12 at 20:04
  • I do not have knowledge of Task.Factory.StartNew functionality. But i think it also creates a new thread to accomplish job in asynchrnous way.. – Ehsan Aleem Avee Oct 18 '12 at 04:45
0

You can persist the email info to a database, and have a background cron job that actually sends the mail.

D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283
  • can u please give a short example on how do you do it ? i will show you the actuall send mail function i am using , sound interesting (updating post) – LoneXcoder Oct 17 '12 at 18:54