C#一次在文本框中记录一段文字

时间:2017-01-05 20:07:59

标签: c# email text textbox take

我有一个文本框(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一次给他发电子邮件并发送电子邮件直到你结束。我该怎么办?

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分: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);
}