C# 在文本框中一次取一段文本
C# Take a piece of text at a time in a textbox
我有一个文本框(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
一次给他发一封电子邮件并发送电子邮件,直到你结束。我该怎么办?
试试这个代码:
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);
}
如果您不想让所有收件人看到其他地址,您可以使用密件抄送
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);
}
我有一个文本框(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
一次给他发一封电子邮件并发送电子邮件,直到你结束。我该怎么办?
试试这个代码:
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);
}
如果您不想让所有收件人看到其他地址,您可以使用密件抄送
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);
}