我想通过Gmail向C#中的多个收件人发送电子邮件。这是我的代码,但此代码仅向一个地址发送电子邮件。我应该如何修改它?
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || richTextBox1.Text == "")
{
MessageBox.Show("Please fill out the boxes!");
return;
}
try
{
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress("tamogatas.dolgozoadatbazis@gmail.com");
message.To.Add(new MailAddress(Form1.cimzett));
message.Subject = textBox1.Text;
message.Body = richTextBox1.Text + Environment.NewLine + "This message was sent from " + (Login.loginnev);
smtp.Port = 587;
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("USERNAME@gmail.com", "PASSWORD");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
MessageBox.Show("The mail was sent successfully!");
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex.Message);
}
}
感谢您的回答!
答案 0 :(得分:1)
String[] emails={"n1@gmail.com","n2@gmail.com","n3@gmail.com","n4@gmail.com"};
foreach(var items as emails)
{
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress(items);
message.To.Add(new MailAddress(Form1.cimzett));
message.Subject = textBox1.Text;
message.Body = richTextBox1.Text + Environment.NewLine + "This message was sent from " + (Login.loginnev);
smtp.Port = 587;
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("tamogatas.dolgozoadatbazis@gmail.com", "adminisztrator0");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
MessageBox.Show("The mail was sent successfully!");
}
答案 1 :(得分:1)
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || richTextBox1.Text == "")
{
MessageBox.Show("Please fill out the boxes!");
return;
}
try
{
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress("tamogatas.dolgozoadatbazis@gmail.com");
message.To.Add(new MailAddress(Form1.cimzett)); <---- THIS LINE
message.To.Add(new MailAddress("lalala@gmail.com"));
message.To.Add(new MailAddress("lalala3@gmail.com"));
message.To.Add(new MailAddress("lalala2@gmail.com"));
message.Subject = textBox1.Text;
message.Body = richTextBox1.Text + Environment.NewLine + "This message was sent from " + (Login.loginnev);
smtp.Port = 587;
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("USERNAME@gmail.com", "PASSWORD");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
MessageBox.Show("The mail was sent successfully!");
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex.Message);
}
}
答案 2 :(得分:0)
您的代码存在一些问题,但是为了解决主要问题,您似乎依赖richTextBox1.Text
来提供电子邮件,但这是一个假设
所以你在做什么:
message.To.Add(new MailAddress(Form1.cimzett));
,可能会将Form1.cimzett
更改为richTextBox1.Text
?
此外,您可以在Button逻辑之外声明所有SmtpClient
代码,而是在此类中声明它。