views:

41

answers:

2

Hi
I am trying to sent an email to some addresses
i did that using the System.Net.mail
the problem is that i need to make the mail message different for each recipient
because i need to put a link inside the email that contain the id this user, the problem is
the large number of recipient that i cant use a loop to invoke sending function for each usesr
like: for (int i=0;i<count;i++)
{moify message(msg); client.Send(msg);}
thanks

+1  A: 

You can add the recipients directly into your MailMessage like this:

MailMessage message = new MailMessage();

        for (int i = 0; i < count; i++)
        {
            message.To.Add("email");
        }

SmtpClient client = new SmtpClient();
client.Send(message);

You can also add the recipients into a single string separating emails with a comma.

Then you can send only one MailMessage.

AS-CII
Yes, i have already done that; the problem is to change the message foe every recipient
3bd
Sorry, I hadn't read the question carefully. Well, in that case I think you have to send a different message for each person. Why you can't use the loop ?
AS-CII
The send function takes long time for sending email to some one if i use the loop it will open the connection each time
3bd
Maybe you can send the message asynchronously with SmtpClient.SendAsync?
AS-CII
it still need to open the connection each time i am trying to avoid that if possible,but it seem to be better solution
3bd
I think it is the only solution.
AS-CII
@ as-cii - comma or semi-colon?
Monkieboy
+1  A: 

You are sending multiple mails, so I don't believe what you want to achieve is possible. Maybe you can try sending them asynchronously, so you don't have to wait.

the code would be something like this

foreach (var message in messages)
{
    var mail = new MailMessage("from", "to");
    ThreadPool.QueueUserWorkItem(x => client.Send(mail));
}

I'm not sure if SmtpClient supports sending multiple mails at once, if that's the case you will need to have several SmtpClients and send through the one that is inactive

hope it helps

Sebastian Piu