-1

I just started with c# and want to write a code for school project a mailer basically I want to send e-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've saved it in a txt file one by one with multi thread. HOW CAN I DO THIS ??

Vahid
  • 474
  • 2
  • 7
  • 20
  • you basically ask the community to do the whole task for you – CherryQuery Aug 02 '22 at 16:49
  • 1
    I believe your second parameter, can be a list of emails separated by a semicolon, so build a string with each email address in it separated by semicolon. And there is an `async` version of the send method [smtpclient-sendasync](https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient.sendasync?view=net-6.0#system-net-mail-smtpclient-sendasync(system-string-system-string-system-string-system-string-system-object)) – Ryan Wilson Aug 02 '22 at 16:51

1 Answers1

2

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.

Tagz97
  • 87
  • 5
  • Good point, didnt look to far into it so will adjust – Tagz97 Aug 02 '22 at 17:14
  • 1
    Looks like a viable solution. +1 – Ryan Wilson Aug 02 '22 at 17:24
  • Adjusted it to not have the old edit in and instead only show viable solution – Tagz97 Aug 02 '22 at 17:26
  • File.ReadLines(fileName) can i use this for the file to read the e mail addresses – Aman Choudhary Aug 03 '22 at 11:27
  • @Aman `File.ReadAllLines(fileName)` can be used. This [link](https://www.c-sharpcorner.com/UploadFile/mahesh/how-to-read-a-text-file-in-C-Sharp/) gives more information how you can approach reading a file – Tagz97 Aug 03 '22 at 11:33
  • I am really sorry to disturb the all masters i just want know how can i add a txt file path to the above code so that i can send e mails to all the users or addresses present in txt file if anyone can help me then please otherwise i need to do all in python i just want to do something new in my school – Aman Choudhary Aug 03 '22 at 11:39
  • `//Default file. MAKE SURE TO CHANGE THIS LOCATION AND FILE PATH TO YOUR FILE static readonly string textFile = @ "C:\Temp\Data\Authors.txt"; ` – Tagz97 Aug 03 '22 at 11:40