>>>>Hi,
>>>>
>>>>I have code like this:
>>>>
>>>>
NetworkCredential cred = new NetworkCredential(smtpUser, smtpPassword);
>>>> NetworkCredential cred1 = new NetworkCredential(justUser + "1" + justCredDomain, smtpPassword);
>>>> NetworkCredential cred2 = new NetworkCredential(justUser + "2" + justCredDomain, smtpPassword);
>>>> NetworkCredential cred3 = new NetworkCredential(justUser + "3" + justCredDomain, smtpPassword);
>>>> NetworkCredential cred4 = new NetworkCredential(justUser + "4" + justCredDomain, smtpPassword);
>>>>
>>>>
>>>>How can I make this flexible where I can switch from 5 variables to 7 or whatever amount I want?
>>>>
>>>>I then need to use these in a loop where I send emails from different email address each pass through the loop. For example, first pass uses cred, second cred1, 3rd Cred2, etc and then when cred4 is reached the next loop would be cred again.
>>>>
>>>>I suppose some type of collection? Can anyone give me an example showing how to declare these and how to use them in later code? I am using C#.
>>>>
>>>>Thanks
>>>
>>>Try this:
>>>
>>>// Set CredentialCount to the number of credentials you want
>>>var credentials = new List<NetworkCredential>(CredentialCount + 1);
>>>credentials.Add(new NetworkCredential(smtpUser, smtpPassword));
>>>for(int index = 1; index <= CredentialCount; index++)
>>> credentials.Add(new NetworkCredential(justUser + index.ToString() + justCredDomain, smtpPassword));
>>>
>>>foreach(var credential in credentials)
>>>{
>>> // Send emails here.
>>>}
>>>
>>
>>Thanks. To turn the logic around a bit. How would I refer to the nth credential in the list of credentials?
>>
>>credentials[n]
>>
>>?
>
>You could refer to it with the indexer, like you posted, but with the foreach loop it would put each one into the credential variable for that iteration.
Using the indexer is exactly what I needed. I am looping through records in an entitylist using a foreach and using one of a list of email addresses to send an email. So if this is the first record I use the first email address, second use the second email address and so on, using the modulus of the record count to determine which email address to use.
Thanks for the help.