novacara
novacara

Reputation: 2257

MailMessage won't send to more than 2 recipients?

In ASP.NET I am sending a MailMessage but it won't go through. The code I am using is

message.To.Add(email1 + ", " + email2 + ", " + email3);

When I do this I never receive my mail. However if I use this code:

message.To.Add(email1 + ", " + email2);

It sends just fine every time. Anybody know what is going on here? All 3 emails are the same (for testing purposes) and have been verified to be correct while debugging. I tried inserting a different email address for the third and still nothing went through. I may be missing something obvious...

EDIT: Everyone is telling me to add them individually which may well be good advice if everyone agrees upon it. The reason I didn't do this previously and I just tried it again with three seperate addresses and none of them were sent. Maybe I have another issue entirely if that is supposed to work?

EDIT: For anyone with the same problem in the future here is what I did. When creating the MailMessage I didn't create it with any parameters and instead specified the From parameter seperately. I wrapped the From and all To emails in new MailAddress() and the combination of all those changes appeared to work.

Upvotes: 0

Views: 401

Answers (5)

Harv
Harv

Reputation: 523

I store message recipients in the web.config file and then handle it like this

 string lstrDistributitionList = ConfigurationSettings.AppSettings["SMTP_DISTRIBUTION_LIST"];
                    string[] lastrDistributitionList = lstrDistributitionList.Split(';');

                    for (Int32 loopCounter = 0; loopCounter < lastrDistributitionList.Length; loopCounter++)
                    {
                        msg.To.Add(lastrDistributitionList[loopCounter]);
                    }

Harvey Sather

Upvotes: 0

Harv
Harv

Reputation: 523

Try

message.to.add(email1);
message.to.add(email2);
message.to.add(email3);
message.to.add(email4);

Hope this helps

Harvey Sather

Upvotes: 2

David
David

Reputation: 73554

Instead of contencating mail addressed into a single Add statement, you should be adding them one at a time:

message.To.Add(email1);
message.To.Add(email2);
message.To.Add(email3);

Since you're adding to a collection.

Also, if the addresses are the same, the function usually doesn't add it twice in my experience. This may be a behavior of the Mailmessage.To.Add function, or it could be that when it gets to me Outlook has stripped out duplicates, but it looks to me like it filters out duplicates. You may be seeing the same on your system.

Upvotes: 2

DaveRead
DaveRead

Reputation: 3413

The To property of MailMessage is a collection, so you should call message.To.Add 3 times if want to send to 3 email addresses.

Upvotes: 2

Daniel A. White
Daniel A. White

Reputation: 190897

Just call Add multiple times.

Upvotes: 4

Related Questions