hello to all i just started with c # and want to write a code for school project a mailer basically I want to send mails to all the emails saved in a txt file.
class Program
{
static void Main(string[] args)
{
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("myusername@gmail.com", "mypwd"),
EnableSsl = true
};
client.Send("myusername@gmail.com", "myusername@gmail.com", "test", "testbody");
Console.WriteLine("Sent");
Console.ReadLine();
}
}
with this code I can send only to a single e mail address i want a function to send e mails to all the email address i saved in txt file one by one with multi thread HOW CAN I DO THIS??
EDIT: Using comment from Ryan Wilson
Very easy to do using the following:
List<string> toAddresses = new List<string>(); // get from file using any chosen way
List<Task> tasks = new List<Task>();
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("myusername@gmail.com", "mypwd"),
EnableSsl = true
};
foreach (var item in toAddresses)
{
tasks.Add(client.SendMailAsync("myusername@gmail.com", item, "subject", "body"));
Console.WriteLine($"Email sent to {item}");
}
await Task.WhenAll(tasks);
Console.ReadLine();
Or you can make use of Parallel.ForEach from Task Parallel Library .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.