Hi Goran,
Actually I had not spotted the need to wait before sending next - seems a
bit odd for async. Howvever, I ammended my code and it works much better. But
I do get erros which are now caught by CallBack, BUT for no obvious reason.
If I immediately resend the same message it then works...
If have included code below: if you look at my log at the end, you can see I
try to send and email "TEST EMAIL NC 11:29:10" then I am blocked from sending
by my own code whilst I wait fr complete callback - which too a few minutes.
I get system error 4. Then I resend message and it works OK - see OnCompleted
code to ensure I am sending exact same message with only difference being
adding "#1" to front of subject.
Odd eh?
Lawrence
private static void SendNetEmail(st ring To, string From, string
Subject, string Body)
{
if (gWaitingForSen dToComplete)
{
AppendDebug("Wa iting for complete New Subject: " + Subject);
return;
}
System.Net.Mail .MailMessage Mailer = new
System.Net.Mail .MailMessage();
Mailer.From = new System.Net.Mail .MailAddress( From);
Mailer.To.Add(T o);
Mailer.Subject = Subject;
Mailer.Body = Body;
Mailer.IsBodyHt ml = false;
Mailer.Priority = System.Net.Mail .MailPriority.H igh;
SmtpClient SC = new SmtpClient("hid den");
SC.Credentials = new NetworkCredenti al("hidden", "hidden");
SC.SendComplete d += new
SendCompletedEv entHandler(Smtp Client_OnComple ted);
object UserState = Mailer;
try
{
AppendDebug("Tr ying to send: " + Subject);
SC.SendAsync(Ma iler, UserState);
gWaitingForSend ToComplete = true;
}
catch (Exception ex)
{
gWaitingForSend ToComplete = false;
Exception ex2 = ex;
string errorMessage = string.Empty;
while (ex2 != null)
{
errorMessage += ex2.ToString();
ex2 = ex2.InnerExcept ion;
}
AppendDebug(err orMessage);
}
}
public static void SmtpClient_OnCo mpleted(object sender,
AsyncCompletedE ventArgs e)
{
gWaitingForSend ToComplete = false;
System.Net.Mail .MailMessage mail =
(System.Net.Mai l.MailMessage)e .UserState;
AppendDebug("Ca llback: " + mail.Subject);
if (e.Error == null)
{
AppendDebug("No errors");
MessageBox.Show ("SNM SENT No CallBack Errors" + mail.Subject
);
}
else
{
AppendDebug("Er rors " + mail.Subject + " " +
e.Error.ToStrin g());
MessageBox.Show ("SNM CallBack ERRORS" + mail.Subject + " " +
e.Error.ToStrin g());
if (mail.Subject.S tartsWith("#"))
{
int Num = int.Parse(mail. Subject.Substri ng(1, 1));
if (Num >= 9)
{
AppendDebug("Ga ve up trying to send");
return;
}
Num++;
mail.Subject = "#" + Num + mail.Subject.Su bstring(2,
mail.Subject.Le ngth - 2);
}
else
{
mail.Subject = "#1" + mail.Subject;
}
Thread.Sleep(50 00);
SendNetEmail(ma il.To.ToString( ), mail.From.ToStr ing(),
mail.Subject, mail.Body);
}
}
public static void AppendDebug(str ing Txt)
{
long ts = DateTime.Now.Ti cks / TimeSpan.TicksP erMillisecond;
TextWriter TW = new StreamWriter("C :\\DebugLog.txt ", true);
TW.WriteLine(ts .ToString() + " " + Txt);
TW.Close();
}
My LOG from AppendDebug
63353618950380 Trying to send: TEST EMAIL NC 11:29:10
63353618957615 Sending mail
63353618957615 Waiting for complete New Subject: TEST EMAIL NC 11:29:17
63353618960771 Sending mail
63353618960771 Waiting for complete New Subject: TEST EMAIL NC 11:29:20
63353619032193 Callback: TEST EMAIL NC 11:29:10
63353619032193 Errors TEST EMAIL NC 11:29:10 System.Net.Mail .SmtpException:
Syntax error, command unrecognized. The server response was: System error 4
at System.Net.Mail .SendMailAsyncR esult.End(IAsyn cResult result)
at System.Net.Mail .SmtpTransport. EndSendMail(IAs yncResult result)
at System.Net.Mail .SmtpClient.Sen dMailCallback(I AsyncResult result)
63353619222350 Trying to send: #1TEST EMAIL NC 11:29:10
63353619223303 Callback: #1TEST EMAIL NC 11:29:10
63353619223303 No errors
"Göran Andersson" wrote:
Moon wrote:
Dear Goran,
I believe this thread is about SMTPClient exceptions. That is what my
question is about. I am trying to catch the exception from an async
SMTPCleint send via the callback sendcompleted functionality.
I felt that this was appropriate for this thread.
I apologise if you still think this is not the case.
Moon
I see. It seemed totally irrelevant as your question is about
asynchronous calls. An asynchronous process doesn't throw exceptions to
communicate back to the caller.
The documentation says that you need to wait for the previos send to
complete before sending again, does your code follow that restriction?
What you are talking about doesn't seem to be a common problem, so it's
probably related to your specific code. If you posted some of it, I (or
anyone else here) can take a look at it.
"Göran Andersson" wrote:
Moon wrote:
CallBack for Asynch does not work consistently. i.e. quite often it does not
get triggered. Can Anyone help?
Don't ask a totally unrelated question in an existing thread. Start a
new thread, and explain what it is that you are doing. There are several
classes with asynchronous methods, specify which one is it that you are
using.
--
Göran Andersson
_____
http://www.guffa.com
--
Göran Andersson
_____
http://www.guffa.com