2017-01-05 69 views
1

我有一個文本框(textBox2)其中是電子郵件: [email protected],[email protected],[email protected],etc..C#採取在一個時間片文本在文本框

我有發送電子郵件的功能:

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一次帶他一封電子郵件併發送電子郵件直到您結束。我能怎麼做?

+0

你是什麼意思與「分別給每封電子郵件的電子郵件」?當'textBox2.Text'中有多個收件人時? –

+2

[split](https://msdn.microsoft.com/en-us/library/b873y76a(v = vs.110).aspx),然後該字符串循環訪問數組中的項目。 –

回答

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

它的工作原理,謝謝你:) – SquizzyHc

0

試試這個代碼:

string emailList = "[email protected],[email protected],[email protected]"; 
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); 
} 
相關問題