【问题标题】:C# Take a piece of text at a time in a textboxC#在文本框中一次取一段文本
【发布时间】:2017-01-05 20:07:59
【问题描述】:

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

【问题讨论】:

  • “一封邮件到每封邮件”是什么意思?当textBox2.Text中有多个收件人时?
  • split 字符串然后循环遍历您返回的数组中的项目。

标签: c# email text textbox take


【解决方案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);
}

【讨论】:

    【解决方案2】:

    试试这个代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-18
      • 1970-01-01
      • 2021-09-02
      • 2021-05-08
      • 2017-01-12
      • 1970-01-01
      • 2014-12-10
      相关资源
      最近更新 更多