Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have Multiple Smtp Senders , i just want to send Emails from all senders in One Shot, i ahve all SMtp in Datatable, adding all smtps in SmtpClient Which is ListType,and adding all reciver in another list which is MailMessage TYpe.All is going smoothly only i just want to send All Emails In One Shot ,without For Loop.
Or can we do this in SqlServer, Any help would be life Saver,Thx in advance

What I have tried:

Hi here i am adding SMTP also in the list but , i need to call it inside for loop to send, so means if i have 150 SMTp , i need to call (SMTP.send) 150 times and it will take too much of time , please Review this code

public void SendEmailFromAllSender()
{
DataTable dtNewSMtp = new DataTable();
// aRandom = Get16DigitRandom();
DataRow dr;
dtNewSMtp.Columns.AddRange(new DataColumn[2] { new DataColumn("Email"), new DataColumn("Pwd") });

dr = dtNewSMtp.NewRow();
dr[0] = "firstsender@gmail.com";
dr[1] = "password1";
dtNewSMtp.Rows.Add(dr);

dr = dtNewSMtp.NewRow();
dr[0] = "secondsender@gmail.com";
dr[1] = "password2";
dtNewSMtp.Rows.Add(dr);





int messagesNeeded = dtNewSMtp.Rows.Count;
List<mailmessage> messages = new List<mailmessage>(messagesNeeded);
List<smtpclient> SMTpClients = new List<smtpclient>(messagesNeeded);

for (int i = 0; i < messagesNeeded; i++)
{
MailMessage mail = new MailMessage();
mail.To.Add("reciver1@gmail.com");
mail.Subject = "This is a Subject";
mail.Body = "i am a testing body How are you dear may i check you" + i;
mail.IsBodyHtml = true;
mail.From = new System.Net.Mail.MailAddress(dtNewSMtp.Rows[i][0].ToString(), "CHeck EMail");
messages.Add(mail);

SmtpClient NewSmtp = new SmtpClient("smtp.gmail.com", 587);
NewSmtp.Credentials = new NetworkCredential(dtNewSMtp.Rows[i]["Email"].ToString(), dtNewSMtp.Rows[i]["Pwd"].ToString());
NewSmtp.EnableSsl = true;
SMTpClients.Add(NewSmtp);

}

for (int k = 0; k < messagesNeeded; k++)
{
SMTpClients[k].Send(messages[k]);// Please Tell me here how i can send all emails in one shot without the loop
}





}
Posted
Updated 17-May-22 23:04pm

You can't.
Each SMTP sender is a separate application, a separate service - and each must be specifically told to send a mail. A bit like sending a snail mail letter to multiple people telling them to "buy cheese": each person needs a separate envelope, a separate letter inside it, a separate message on the letter (even if all messages are the same), a separate stamp.
 
Share this answer
 
Reading between the lines, the problem you're trying to solve is that it's taking too long to send a message, wait for it to finish, then send the next message.

If you use the SmtpClient.SendMailAsync method[^], you can start all 150 "send" operations at once, and then wait for them all to complete:
C#
int messagesNeeded = dtNewSMtp.Rows.Count;
List<Task> sendTasks = new(messagesNeeded);
for (int i = 0; i < messagesNeeded; i++)
{
    MailMessage mail = new();
    mail.To.Add("reciver1@gmail.com");
    mail.Subject = "This is a Subject";
    mail.Body = "i am a testing body How are you dear may i check you" + i;
    mail.IsBodyHtml = true;
    mail.From = new(dtNewSMtp.Rows[i][0].ToString(), "CHeck EMail");
    messages.Add(mail);

    SmtpClient smtp = new("smtp.gmail.com", 587);
    smtp.Credentials = new(dtNewSMtp.Rows[i]["Email"].ToString(), dtNewSMtp.Rows[i]["Pwd"].ToString());
    smtp.EnableSsl = true;
    
    Task sendTask = smtp.SendMailAsync(mail);
    sendTasks.Add(sendTask);
}

Task.WaitAll(sendTasks.ToArray());

// Or, in an async method: 
// await Task.WhenAll(sendTasks);

NB: As I said to your colleague yesterday[^], this sort of code is highly likely to get your email account banned for spamming.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900