Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all, i just want to know that how i can declare object of MailMessage inside forloop C#, My main task is to send Emails From Multiple Sender to Single Recipient at one time, Any help will be appreciated.

What I have tried:

for(int i=0;i<=dtNewSMtp.Rows.Count-1;i++)
{
// string Msg = "Msg" + i;

MailMessage ram +"i" = new MailMessage();


}
Posted
Updated 15-May-22 20:28pm

1 solution

You can't create variable names at run-time - they are all used by the compiler so even if you could, you couldn't access them in your code!

Have you considered a List or an array?
C#
int messagesNeeded= dtNewSMtp.Rows.Count;
List<MailMessage> messages = new List<MailMessage>(messagesNeeded);
for (int i = 0; i < messagesNeeded; i++)
   {
   MailMessage msg = new MailMessage();
   messages.Add(msg);
   ...
   }
You can then address each message individually via the index as necessary
 
Share this answer
 
Comments
Ramendra Kumar Sinha 16-May-22 7:11am    
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)
{


}

}
}


}
Ramendra Kumar Sinha 16-May-22 7:55am    
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
}





}

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