1

I have a textbox(textBox2) where are the email: thisemail@gmail.com,hello@yahoo.it,YesOrNo@gmail.com,etc..

I have a function that sends an email:

private void button1_Click(object sender, EventArgs e)
{
    var mail = new MailMessage();
    var smtpServer = new SmtpClient(textBox5.Text);
    mail.From = new MailAddress(textBox1.Text);
    mail.To.Add(textBox2.Text);
    mail.Subject = textBox6.Text;
    mail.Body = textBox7.Text;
    mail.IsBodyHtml = checkBox1.Checked;
    mail.Attachments.Add(new Attachment(textBox9.Text));
    var x = int.Parse(textBox8.Text);
    smtpServer.Port = x;
    smtpServer.Credentials = new System.Net.NetworkCredential(textBox3.Text, textBox4.Text);
    smtpServer.EnableSsl = checkBox2.Checked;
    smtpServer.Send(mail);
}

I want you send an email to each email separately. That is, when I press the button1 to take him an email at a time and send the email until you end up. How can I do?

D Stanley
  • 149,601
  • 11
  • 178
  • 240
SquizzyHc
  • 15
  • 1
  • 3
  • What do you mean with "an email to each email separately"? When there are multiple recipients in `textBox2.Text`? – Malte R Jan 05 '17 at 20:11
  • 2
    [split](https://msdn.microsoft.com/en-us/library/b873y76a(v=vs.110).aspx) the string then loop through the items in the array you get back. – D Stanley Jan 05 '17 at 20:11

2 Answers2

1

If you just don't want all the recipients to see the other addresses you could just use the blind carbon copy instead

mail.Bcc.Add(textBox2.Text);

If you really do want to send the same email multiple times you can just split the addresses on the comma and pass them to the code you already have in a separate method.

private void button1_Click(object sender, EventArgs e)
{
    foreach(var address in textBox2.Text.Split(","))
        SendMessage(address);
}

private void SendMessage(string address)
{
    var mail = new MailMessage();
    var smtpServer = new SmtpClient(textBox5.Text);
    mail.From = new MailAddress(textBox1.Text);
    mail.To.Add(address);
    mail.Subject = textBox6.Text;
    mail.Body = textBox7.Text;
    mail.IsBodyHtml = checkBox1.Checked;
    mail.Attachments.Add(new Attachment(textBox9.Text));
    var x = int.Parse(textBox8.Text);
    smtpServer.Port = x;
    smtpServer.Credentials = new System.Net.NetworkCredential(textBox3.Text, textBox4.Text);
    smtpServer.EnableSsl = checkBox2.Checked;
    smtpServer.Send(mail);
}
juharr
  • 31,741
  • 4
  • 58
  • 93
0

Try this code:

string emailList = "thisemail@gmail.com,hello@yahoo.it,YesOrNo@gmail.com";
string[] emails = emailList.Split(","); // replace this text with your source :)

foreach(string s in emails)
{
var mail = new MailMessage();
            var smtpServer = new SmtpClient(textBox5.Text);
            mail.From = new MailAddress(textBox1.Text);
            mail.To.Add(s);
            mail.Subject = textBox6.Text;
            mail.Body = textBox7.Text;
            mail.IsBodyHtml = checkBox1.Checked;
            mail.Attachments.Add(new Attachment(textBox9.Text));
            var x = int.Parse(textBox8.Text);
            smtpServer.Port = x;
            smtpServer.Credentials = new System.Net.NetworkCredential(textBox3.Text, textBox4.Text);
            smtpServer.EnableSsl = checkBox2.Checked;
            smtpServer.Send(mail);
}
NicoRiff
  • 4,803
  • 3
  • 25
  • 54