1

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

"The following recipients failed: [joe@domain1.com, steve@domain2.com]. 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).

Community
  • 1
  • 1
Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
  • Something like `string.Join(", ", ex.InnerExceptions.Select(e => e.Message))`? Or can your `InnerExceptions` have `InnerException`s themselves? Have you tried searching? – CodeCaster Nov 10 '15 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. – Mr. Boy Nov 10 '15 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... – Mr. Boy Nov 10 '15 at 11:13
  • My point is that this question has been answered tens of times. Try searching. The duplicate is the proper approach that does support hierarchies of InnerExceptions, whether you need it or not. – CodeCaster Nov 10 '15 at 11:14
  • @CodeCaster: The thing is here you don't really want to join all messages, you want to get the recipients off the exception and use that. I'm not 100% sure what the text of Message is but I am pretty sure that concatenating them won't give the output string the OP requested. Instead the correct answer to this should be discussing the `FailedRecipient` property on the exceptions in question. – Chris Nov 10 '15 at 11:16
  • @CodeCaster "here's something much more complicated than you need which will also solve your much simpler question" is _not_ a good answer. The answers given already show there is a simpler approach possible, which is fine for the question being asked. – Mr. Boy Nov 10 '15 at 11:17
  • @chris actually `SmtpFailedRecipientException.FailedRecipient` gives the email address in this scenario, I'll amend that to the question. – Mr. Boy Nov 10 '15 at 11:19
  • @Mr.Boy: Indeed, and you can then use linq to get the joined list of recipients and put it into a message. Is this not what you want? – Chris Nov 10 '15 at 11:20
  • Then perhaps you're looking for [Concat all strings inside a List using LINQ](http://stackoverflow.com/questions/559415/concat-all-strings-inside-a-liststring-using-linq). Again, try searching. – CodeCaster Nov 10 '15 at 11:21
  • 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... – Chris Nov 10 '15 at 11:26
  • @Chris the Linq part... but whenever I ask a more general question I get told off what not saying specifically what I am trying to achieve. And the issue is, I didn't know what to search _for_. – Mr. Boy Nov 10 '15 at 11:35
  • 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 http://stackoverflow.com/questions/5822716/concatenate-string-properties-of-an-object-with-lambda which seems to answer your question. – Chris Nov 10 '15 at 11:45

2 Answers2

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));
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • `ex.FailedRecipient` is actually the property I'm interested in, though that's a minor detail, the approach is clear from your answer. – Mr. Boy Nov 10 '15 at 11:20
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.

Chris
  • 27,210
  • 6
  • 71
  • 92