Click here to Skip to main content
16,019,043 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
/*i am trying to send email to many ...i am using a loop, but the code is not working..
and i used a text box where email addresses are bind to text on selection seperated by a comma ...i also want to know whether the Comma seperator will be creating a problem or not */









public partial class Mail : Form
{String[] email = new String[30];
private void btn_Send_Click(object sender, EventArgs e)
{

//String mailmsg = "text1"+rich_Msg.Text+"text2";
//String mailmsg = "" + rich_Msg.Text + "text2";


for (int i = 0; i < email.Length; i++)
{
SendEmail(email[i], "from@mail.com", txt_Subj.Text,mailmsg);


}
private void SendEmail(string sendTo, string sendFrom, string subject, string body)
{
try
{
client.Host = "smtp.gmail.com";
client.Port = 587;
client.UseDefaultCredentials = false;
client.Credentials = smtpcrede;
client.EnableSsl = true;
MailAddress to = new MailAddress(sendTo);
MailAddress from = new MailAddress(sendFrom);
msg.IsBodyHtml = true;

msg.Subject = subject;
msg.Body = body;
msg.From = from;
msg.To.Add(to);
client.Send(msg);

}



}
Posted

1 solution

Depends - if you are passing the commas separated list through to you SendEmail routine, then it may well be. If so, then just use string.Split to break it up, and add each address to the To list:

C#
string[] addrs = sendTo.Split(',');
foreach (string addr in addrs)
   {
   MailAddress to = new MailAddress(addr);
   msg.To.Add(to);
   }
 
Share this answer
 
Comments
MohRizwan 8-Jul-13 2:55am    
thanks Mr.Griif but it shows an error that " smtp Authentication needed"
OriginalGriff 8-Jul-13 3:49am    
That's probably down you your credentials
client.Credentials = smtpcrede;
Check them!
MohRizwan 8-Jul-13 7:24am    
public System.Net.NetworkCredential smtpcrede = new System.Net.NetworkCredential("mymail@gmail.com", " password");


this is my credential hope this code is right..?

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