mail attachment repeating
-
Please anyone may help! I am trying a program to send a attachment(only one attachment) to 3 mail recepiants . After sending the mail (a success one) I am finding that the attachment sent to each of the recepient is attached thrice(same file is attached to the mail) whereas I am attaching only a single file. And more over I am getting the mail twice as if I clicked on the SEND button twice(infact I clicked only once). Please help me . I give my code as below :rose:thanks in advance :rose: protected void Button1_Click(object sender, EventArgs e) { string[] addresses = {"mailaddress1","mailaddress2","mailaddress3" }; SmtpClient smtpMail = new SmtpClient(); smtpMail.Host = "smtp.abc.com"; smtpMail.EnableSsl = true; MailMessage mail = new MailMessage(); mail.From = new MailAddress("abc@abc.com"); string fileName = Path.GetFullPath(FileUpload1.PostedFile.FileName); for (int i = 0; i < addresses.Length; i++) { if (FileUpload1.PostedFile != "") { try { mail.Attachments.Add(new Attachment(fileName)); } catch (Exception ex) { Response.Write(ex.Message); } } mail.To.Add(addresses[i]); Response.Write(addresses[i] + " "); mail.Subject = "HAI!!!!!!!"; mail.Body = "ATTACH TO ALL"; smtpMail.Credentials = new System.Net.NetworkCredential(username.Text, txtPassword.Text); try { smtpMail.Send(mail); Response.Write(" Mail Sent"); } catch (Exception ex) { Response.Write("Error " + ex.InnerException); Console.WriteLine(ex.InnerException); } } }//end of event click
Kovuru Sreedhar
-
Please anyone may help! I am trying a program to send a attachment(only one attachment) to 3 mail recepiants . After sending the mail (a success one) I am finding that the attachment sent to each of the recepient is attached thrice(same file is attached to the mail) whereas I am attaching only a single file. And more over I am getting the mail twice as if I clicked on the SEND button twice(infact I clicked only once). Please help me . I give my code as below :rose:thanks in advance :rose: protected void Button1_Click(object sender, EventArgs e) { string[] addresses = {"mailaddress1","mailaddress2","mailaddress3" }; SmtpClient smtpMail = new SmtpClient(); smtpMail.Host = "smtp.abc.com"; smtpMail.EnableSsl = true; MailMessage mail = new MailMessage(); mail.From = new MailAddress("abc@abc.com"); string fileName = Path.GetFullPath(FileUpload1.PostedFile.FileName); for (int i = 0; i < addresses.Length; i++) { if (FileUpload1.PostedFile != "") { try { mail.Attachments.Add(new Attachment(fileName)); } catch (Exception ex) { Response.Write(ex.Message); } } mail.To.Add(addresses[i]); Response.Write(addresses[i] + " "); mail.Subject = "HAI!!!!!!!"; mail.Body = "ATTACH TO ALL"; smtpMail.Credentials = new System.Net.NetworkCredential(username.Text, txtPassword.Text); try { smtpMail.Send(mail); Response.Write(" Mail Sent"); } catch (Exception ex) { Response.Write("Error " + ex.InnerException); Console.WriteLine(ex.InnerException); } } }//end of event click
Kovuru Sreedhar
Hi kvsreedhar The attachment is add 3 time because it is inside the
for (int i = 0; i < addresses.Length; i++)
block. If you remove the for loop it will be attached once. -
Hi kvsreedhar The attachment is add 3 time because it is inside the
for (int i = 0; i < addresses.Length; i++)
block. If you remove the for loop it will be attached once.thanks for your replay but without using the loop how can i send to 3 persons with just one click?
Kovuru Sreedhar
-
thanks for your replay but without using the loop how can i send to 3 persons with just one click?
Kovuru Sreedhar
You only need to create one
MailMessage
object, attach the attachment once and send it once. The only part that needs to be in the for loop ismail.To.Add(...` [www.whatUrunning.com](http://www.whatUrunning.com) `
-
You only need to create one
MailMessage
object, attach the attachment once and send it once. The only part that needs to be in the for loop ismail.To.Add(...` [www.whatUrunning.com](http://www.whatUrunning.com) `
:) thanks a lot it's working. :)
Kovuru Sreedhar
-
:) thanks a lot it's working. :)
Kovuru Sreedhar
You are welcome :)