1

我有一个文本框(textBox2),电子邮件在哪里: thisemail@gmail.com,hello@yahoo.it,YesOrNo@gmail.com,etc..

我有一个发送电子邮件的功能:

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);
}

我希望您分别向每封电子邮件发送一封电子邮件。也就是说,当我按一次button1给他一封电子邮件并发送电子邮件直到你结束时。我能怎么做?

4

2 回答 2

1

如果您只是不希望所有收件人看到其他地址,则可以使用密件抄送代替

mail.Bcc.Add(textBox2.Text);

如果您确实想多次发送同一封电子邮件,您可以用逗号分隔地址并将它们传递给您已经在单独方法中拥有的代码。

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);
}
于 2017-01-05T20:11:25.937 回答
0

试试这个代码:

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);
}
于 2017-01-05T20:11:15.980 回答