1

SmtpFailedRecipientsException.InnerExceptions is an array of SmtpFailedRecipientException. I'd like to build a string that looks like:

"The following recipients failed: [[email protected], [email protected]]. The email was sent to all other recipients succesfully"

SmtpFailedRecipientException.FailedRecipient holds the email address.

I'm trying to work out if there's a way of using LINQ and/or lambda functions to effectively do a join on this array, maybe converting it to a string[] by reading SmtpFailedRecipientException.Message or something along those lines, rather than writing a C-style for-loop?

This question (Getting all messages from InnerException(s)?) addresses the more general case of hierarchical nested exceptions, but that is not what I'm after... the asnwers there are significantly more complex than I need (as answers here demonstrate).

12
  • Something like string.Join(", ", ex.InnerExceptions.Select(e => e.Message))? Or can your InnerExceptions have InnerExceptions themselves? Have you tried searching? Commented Nov 10, 2015 at 11:09
  • further levels of nesting is not too important... though a generic way to convert anything to a string[] would let me address more complex scenarios if needed. Commented Nov 10, 2015 at 11:11
  • I don't see how the linked question is a duplicate. I am after a simpler answer to a simpler scenario, not a general case. The answers given here are clearly different to the ones in that question... Commented Nov 10, 2015 at 11:13
  • 1
    I think I agree with CodeCaster in that this question isn't doing anything complicated. There are three stages, extracting the string into a list of strings from a list of objects, joining those strings, putting that string into a message. It is not clear which of these you are having trouble with (my initial assumption was that you didn't know how to get the email address but that seems to be false). I suspect each of these three parts is answered somewhere on the site and it is not actually very clear which one you are having problems with... Commented Nov 10, 2015 at 11:26
  • 1
    @Mr.Boy: I can't comment on any other questions you are asking and since I am very familiar with linq its hard for me to take a step back and ask whether you were in a position to split it into the three steps that I outlined above in order to be able to say which you had a problem with. That having been said I did a google for "concatenating properties of list of objects" and the top hit was stackoverflow.com/questions/5822716/… which seems to answer your question. Commented Nov 10, 2015 at 11:45

2 Answers 2

2
string[] exceptionMessages = yourSmtpFailedRecipientsException.InnerExceptions
    .Select(ex => ex.Message)
    .ToArray();

if you want to ouput it comma separated you can use String.Join:

Console.Write(String.Join(",", exceptionMessages));
Sign up to request clarification or add additional context in comments.

1 Comment

ex.FailedRecipient is actually the property I'm interested in, though that's a minor detail, the approach is clear from your answer.
1

The property you are interested in is the SmtpFailedRecipientException.FailedRecipient property which according to the documentation "Indicates the e-mail address with delivery difficulties".

To get a list of the failed addresses you could do:

IEnumerable<string> emailAddresses = SmtpFailedRecipientsException.InnerExceptions.Select(x=>x.FailedRecipient);
string joinedAddresses = String.Join(", ", emailAddresses);
string message = String.Format("The following recipients failed: [{0}]. The email was sent to all other recipients succesfully", joinedAddresses );

This uses some local variables you could skip over if you wanted, I used them mainly for readability and to make it clear what I was doing.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.