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(string To, string From, string
Subject, string Body)
{
if (gWaitingForSendToComplete)
{
AppendDebug("Waiting 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(To);
Mailer.Subject = Subject;
Mailer.Body = Body;
Mailer.IsBodyHtml = false;
Mailer.Priority = System.Net.Mail.MailPriority.High;
SmtpClient SC = new SmtpClient("hidden");
SC.Credentials = new NetworkCredential("hidden", "hidden");
SC.SendCompleted += new
SendCompletedEventHandler(SmtpClient_OnCompleted);
object UserState = Mailer;
try
{
AppendDebug("Trying to send: " + Subject);
SC.SendAsync(Mailer, UserState);
gWaitingForSendToComplete = true;
}
catch (Exception ex)
{
gWaitingForSendToComplete = false;
Exception ex2 = ex;
string errorMessage = string.Empty;
while (ex2 != null)
{
errorMessage += ex2.ToString();
ex2 = ex2.InnerException;
}
AppendDebug(errorMessage);
}
}
public static void SmtpClient_OnCompleted(object sender,
AsyncCompletedEventArgs e)
{
gWaitingForSendToComplete = false;
System.Net.Mail.MailMessage mail =
(System.Net.Mail.MailMessage)e.UserState;
AppendDebug("Callback: " + mail.Subject);
if (e.Error == null)
{
AppendDebug("No errors");
MessageBox.Show("SNM SENT No CallBack Errors" + mail.Subject
);
}
else
{
AppendDebug("Errors " + mail.Subject + " " +
e.Error.ToString());
MessageBox.Show("SNM CallBack ERRORS" + mail.Subject + " " +
e.Error.ToString());
if (mail.Subject.StartsWith("#"))
{
int Num = int.Parse(mail.Subject.Substring(1, 1));
if (Num >= 9)
{
AppendDebug("Gave up trying to send");
return;
}
Num++;
mail.Subject = "#" + Num + mail.Subject.Substring(2,
mail.Subject.Length - 2);
}
else
{
mail.Subject = "#1" + mail.Subject;
}
Thread.Sleep(5000);
SendNetEmail(mail.To.ToString(), mail.From.ToString(),
mail.Subject, mail.Body);
}
}
public static void AppendDebug(string Txt)
{
long ts = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
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.SendMailAsyncResult.End(IAsyncResult result)
at System.Net.Mail.SmtpTransport.EndSendMail(IAsyncResult result)
at System.Net.Mail.SmtpClient.SendMailCallback(IAsyncResult result)
63353619222350 Trying to send: #1TEST EMAIL NC 11:29:10
63353619223303 Callback: #1TEST EMAIL NC 11:29:10
63353619223303 No errors