简体   繁体   中英

Sending e-mail to multiple recipients

I want to send e-mail to multiple recipients in C# through Gmail. This is my code, but this code only send email to one address. How should I modify it?

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

Thanks for the answers!

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

There are a few problems with your code, however to address the primary issue it looks like you are relying on richTextBox1.Text to supply the email, however this is an assumption

So where you are doing: message.To.Add(new MailAddress(Form1.cimzett)); , maybe change Form1.cimzett to richTextBox1.Text ?

In addition you can declare all of the SmtpClient code outside of the Button logic, and instead declare it in this class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM