How to catch exceptions?

  • Thread starter Thread starter JackPot
  • Start date Start date
J

JackPot

Actually, I'm interested in learning where the different types of exceptions
are documented for a specific type of class, let us say for instance classes
in the System.Net.Mail Namespace. Shouldn't the types be documented on the
same page as the class, its members and properties?
 
Hi


http://msdn.microsoft.com/en-us/library/system.net.mail.aspx

SmtpException: Represents the exception that is thrown when the
SmtpClient is not able to complete a Send or SendAsync operation.
SmtpFailedRecipientException: Represents the exception that is thrown
when the SmtpClient is not able to complete a Send or SendAsync
operation to a particular recipient.
SmtpFailedRecipientsException: Infrastructure. The exception that is
thrown when e-mail is sent using an SmtpClient and cannot be delivered
to all recipients.

yes they are mentioned there...

Best of luck

-------
Munna

www.munna.shatkotha.com/blog
www.munna.shatkotha.com
www.shatkotha.com
 
Oh yea I see, exceptions are classes and listed in the section for classes
which will be typical for any namespace. Duh.
 
CallBack for Asynch does not work consistently. i.e. quite often it does not
get triggered. Can Anyone help?
 
Moon said:
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.
 
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
 
Moon said:
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.
 
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
 
Back
Top