Reputation: 1463
I am sending emails to many people by Thunderbird by c#. I do it with loop. My problem is that when I run my application - for any email I send a new Thunderbird window is openning, and then I need to press "send" and so it is send. It is very inconvenient. How can I send all the emails at once?
My code:
string strCommand;
for(i=0;i<100;i++)
{
strCommand = " -compose to=" + (char)34 + astrRecip[i] + (char)34 + ",";
strCommand += "body=" + (char)34 + strMessage[i] + (char)34 + ",";
strCommand += "subject=" + (char)34 + strSubject + (char)34 + ",";
strCommand += "attachment=" + (char)34 + strAttachment[i] + (char)34;
Process.Start(@"C:\Program Files\Mozilla Thunderbird\thunderbird", strCommand);
}
I think that because I write "-compose" to every email so it is open a new window, maybe if I write one command-line it will be OK. I tried, but with no success.
Upvotes: 3
Views: 5968
Reputation: 17973
You can use multiple email addresses in one compose argument, as an example shows in Command line arguments - Thunderbird.
So, instead of starting the process in each loop, join all the email addresses and start one process afterwards. This is simple using string.Join.
string strCommand;
strCommand = " -compose to=" + (char)34 + string.Join(",", astrRecip) + (char)34 + ",";
strCommand += "body=" + (char)34 + strMessage[i] + (char)34 + ",";
strCommand += "subject=" + (char)34 + strSubject + (char)34 + ",";
strCommand += "attachment=" + (char)34 + strAttachment[i] + (char)34;
Process.Start(@"C:\Program Files\Mozilla Thunderbird\thunderbird", strCommand);
Edit: note that you can escape the " if you want using \". So instead of using (char)34
you can type
strCommand = " -compose to=\"" + string.Join(",", astrRecip) + "\",";
and, by using string format, this can be even easier to read.
strCommand = string.Format("-compose to=\"{0}\",", string.Join(",", astrRecip));
since you seperate the string from it's arguments.
Upvotes: 2
Reputation: 174457
You really should use the SmtpClient
class from the .NET framework.
Upvotes: 6