c# - Resending email in SendCompleted Event -


i have class called schedulerservice, , in have sendmail function that's called when email needs sent @ time. when call sendmail function, pass in object holds information send email , email from. now, i've added sendcompleted handler can possibly resend email, in case happens causes not to. have code sends mail:

    var recipients = emailto.split(',').tolist();     if (string.isnullorempty(emailfrom))         emailfrom = recipients[0];      using (mailmessage message = new mailmessage())       {         message.from = new mailaddress(emailfrom);         recipients.foreach(a => message.to.add(new mailaddress(a)));         message.attachments.add(new attachment(locationofresults));         message.subject = string.format("{0:mm-dd-yyyy} results task: {1}.", datetime.now, description);         message.body = "attached results file specified task: " + description;         smtpclient.sendcompleted += new sendcompletedeventhandler(sendcompletedcallback);         smtpclient.usedefaultcredentials = true;         smtpclient.sendasync(message, null);                  } 

this event handler

    private void sendcompletedcallback(object sender, asynccompletedeventargs e)     {         if (e.cancelled)         {             mailmessage mail = (mailmessage)e.userstate;             using (mail)             {                  smtpclient.send(mail);             }         }         if (e.error != null)         {             log(e.error.tostring() + " in sendcompletedhandlerevent", eventlogentrytype.error);         }      } 

problem is, discovered doing doesn't work, , fields empty, causes error when sending email. how should go reclaiming to/from fields email failed send?

if you'd use asynchronius sending, should rid of using block, it'll dispose initialized message variable. instead, may call dispose() sendcompleted eventhandler or call dispose initialization method:

 /*...*/  smtpclient.sendasync(message, null);  message.dispose(); 

you may find approach on this page msdn

also, should rewrite initialization somehow, make this:

smtpclient.credentials = new networkcredential("somemail@gmail.com", "pass");             smtpclient.port = 587;             smtpclient.host = "smtp.gmail.com";             smtpclient.sendasync(message, null);             smtpclient.sendcompleted  += new sendcompletedeventhandler(smtpclient_sendcompleted); 

that is, mean sendasync credentails , other parameters needed perform operation should set first.

upd it'll solve problem of disposing original message variable, cause that's why "to , fields empty".

about resending - provide please more code or example of situation regardless how , going avoid message sending cancel , resending it.

also mean under resending of cancelled message exactly? right want send cancelled message again? far understand it, have no possibility continue sending message cancelled using sendasynccancel() in case.

in case of sendasynccancel executed, still causes sendcompleted event raised, arguments passed indicate operation cancelled. so, can't escape it. may want see this page, , of course msdn.

if need sending message in case of troubles cancel being sent, send again:

  /*your sendcompleted eventhandler*/  if (e.canceled)   {     //if use using(message) {...} here, you'll objectdisposedexception again    smtpclient smtp;     smtp = new smtpclient();        smtp = getclient(smtp);  //method of smtpclient initialization     mailmessage mess = new mailmessage();     mess = getmessage(mess, smtp);  //method of mailmessage initialization      try     {         //sending message again         smtp.sendasync(mess, null);      }     catch (objectdisposedexception e)     {        messagebox.show("the email message not sent. see details:\n"+e.message,       "error sendiing message")       }   } 

i tested using sendasynccancel method hope biggest part of cancelling reasons taken in consideration.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -