Advertisement

05.01.2008 at 02:21PM PDT, ID: 23370165
[x]
Attachment Details

System.Net.Mail smtp client not catching errors

Asked by metsey in Simple Mail Transfer Protocol (SMTP), Microsoft Visual C#.Net, .NET Framework 2.0

Tags: C#

I have written a windows service that reads a queue and sends emails with attachments via the system.net.mail object.  I am authenticating to our exchange server to handle this.  The problem is that when i try to catch errors (such as having a bad email address on the .send method) it is not catching the exceptions in my catch statement.  However, an undeliverable message will come back to the From address. So i know the mail server is recording the errors.

I have been researching this but to no avail.  Any advice would be greatly appreciated.  I have attached a code snippet.

Regards
Mark

Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
SmtpClient objEmail = new SmtpClient("mail.domain.local");
                objEmail.Credentials = new NetworkCredential("sender@smtp.relay", "*******");
                m.Bcc.Add("recipient@something.com");
                objEmail.DeliveryMethod = SmtpDeliveryMethod.Network;
 
                objEmail.Send(m);
                
                return true;
            }
            catch (SmtpFailedRecipientsException e)
            {
                SmtpStatusCode sc = e.StatusCode;
                Error += "||On Send To " + m.To.ToString().Trim() + " - Error Message: " + e.Message.ToString().Trim() + " - StatusCode: " + sc.ToString();
                return false;
            }
            catch (SmtpFailedRecipientException e)
            {
                SmtpStatusCode sc = e.StatusCode;
                Error += "||On Send To " + m.To.ToString().Trim() + " - Error Message: " + e.Message.ToString().Trim() + " - StatusCode: " + sc.ToString();
                return false;
            }
            catch (SmtpException e)
            {
                SmtpStatusCode sc = e.StatusCode;
                Error += "||On Send To " + m.To.ToString().Trim() + " - Error Message: " + e.Message.ToString().Trim() + " - StatusCode: " + sc.ToString();
                return false;
            }
            catch (Exception e)
            {
                Error += "||On Send To " + m.To.ToString().Trim() + " - Error Message: " + e.Message.ToString().Trim();
                return false;
            }
[+][-]05.01.2008 at 07:11PM PDT, ID: 21484140

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Simple Mail Transfer Protocol (SMTP), Microsoft Visual C#.Net, .NET Framework 2.0
Tags: C#
Sign Up Now!
Solution Provided By: dstanley9
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628