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
views:
41answers:
2
+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
2010-10-19 08:28:16
Yes, i have already done that; the problem is to change the message foe every recipient
3bd
2010-10-19 08:31:35
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
2010-10-19 08:35:07
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
2010-10-19 08:37:30
Maybe you can send the message asynchronously with SmtpClient.SendAsync?
AS-CII
2010-10-19 08:42:29
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
2010-10-19 08:47:23
I think it is the only solution.
AS-CII
2010-10-19 09:04:36
@ as-cii - comma or semi-colon?
Monkieboy
2010-10-19 09:12:05
+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
2010-10-19 08:45:06