I have a strange problem with an Index Out of Bounds exception when debugging an addin I am developing for MS Outlook 2010. I have a class to make message processing and in the constructor for that class, I pass a MailItem. I then intend to run through the Recipients list of the MailItem, and find all recipients registered in the To, CC, and BCC fields. For this I have the following code:
public MessageProcessor(Outlook.MailItem theMail)
{
_activeMailItem = theMail;
_activeMailDetails.Sender = theMail.SenderEmailAddress;
if (_activeMailItem.Recipients.Count > 0)
{
List<string> recipients = new List<string>();
List<string> cc = new List<string>();
List<string> bcc = new List<string>();
for (int i = 0; i < _activeMailItem.Recipients.Count; i++)
{
switch (_activeMailItem.Recipients[i].Type) <----- HERE
{
case (int)Outlook.OlMailRecipientType.olTo:
recipients.Add(_activeMailItem.Recipients[i].Address);
break;
case (int)Outlook.OlMailRecipientType.olCC:
cc.Add(_activeMailItem.Recipients[i].Address);
break;
case (int)Outlook.OlMailRecipientType.olBCC:
bcc.Add(_activeMailItem.Recipients[i].Address);
break;
}
}
}
}
However, I get the exception at the point marked "HERE". When I debug and look at the value of the Recipients.Count property, it shows 1. However, the problem occurs when the "i" index is 0 (which should be a valid index - and, in this case, the only valid index). When I try to look at the _activeMailItem.Recipients collection, I see the Count of 1; however, when I try to track further down in the structure, I see some red crosses, and I can't inspect the values below that.
Does anyone have a clue about what can be wrong?
Thanks in advance.
All collections in Outlook (including Recipients) are 1 based, not 0:
for (int i = 1; i <= _activeMailItem.Recipients.Count; i++)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With