Click here to Skip to main content
15,885,278 members

Comments by Ramendra Kumar Sinha (Top 2 by date)

Ramendra Kumar Sinha 16-May-22 7:55am View    
Respected sir OroginalGriff, thank for your Reply i just upgraded my code too, Now 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 SendEmailToMany()
{
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
}





}
Ramendra Kumar Sinha 16-May-22 7:11am View    
Thanks for your reply i am adding something like this, but here i have to call send multiple times, i just want to send email from all Sender in one shot, i am getting confused here, and again thx for your kind support.
public void SendEmailToMany()
{
DataTable dtNewSMtp = new DataTable();
DataRow dr;
dtNewSMtp.Columns.AddRange(new DataColumn[2] { new DataColumn("Email"), new DataColumn("Pwd") });

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

dr = dtNewSMtp.NewRow();
dr[0] = "sender2@gmail.com";
dr[1] = "password";
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("firstReciver@gmail.com");
mail.Subject = "This is a Subject";
mail.Body = "i am a testing body How are you dear may i check you";
mail.IsBodyHtml = true;
mail.From = new System.Net.Mail.MailAddress(dtNewSMtp.Rows[i][0].ToString(), "Reciver1");
messages.Add(mail);



using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
{
//smtp.Credentials = new NetworkCredential("emailFromAddress", "password");
smtp.Credentials = new NetworkCredential(dtNewSMtp.Rows[i]["Email"].ToString(), dtNewSMtp.Rows[i]["Pwd"].ToString());
smtp.EnableSsl = true;
try
{


smtp.Send(mail);// Here please suggest me so that i can send emails from all sender in one shot
// issent = true;

}
catch (Exception ee)
{


}

}
}


}