Solved

Sending Multiple Emails in C#

Posted on 2011-09-23
6
398 Views
Last Modified: 2013-12-17
Hello Experts

I have the following code which works good when I send single email.

private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

                mail.From = new MailAddress("xxxxxxxxx@gmail.com");
                mail.To.Add(textBox1.Text);
                mail.Subject = textBox2.Text;
                mail.Body = textBox3.Text;

                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential(username.Text, password.Text);
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);
                MessageBox.Show("mail Send");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

Open in new window


my question is what should I change here so I can send multiple emails?!

when I type in emails like:
xxx@gmail.com; yyy@gmail.com; zzz@gmail.com;

it always sends the emails to the last email in this case is zzz@gmail.com
0
Comment
Question by:barlet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 11

Expert Comment

by:brutaldev
ID: 36588482
You need to add more mail to the To collection:
 
mail.To.Add("xxx@gmail.com");
mail.To.Add("yyy@gmail.com");
mail.To.Add("zzz@gmail.com");

Open in new window

Or split the string that comes in from the text box and add each mail:
 
foreach (string email in textBox1.Text.Split(";"))
{
  mail.To.Add(email);
}

Open in new window

0
 
LVL 13

Expert Comment

by:jonnidip
ID: 36588554
Or you can even send one mail per recipient:
foreach (string email in textBox1.Text.Split(";"))
{
   try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

                mail.From = new MailAddress(email);  // This is the email address splitted in the foreach
                mail.To.Add(textBox1.Text);
                mail.Subject = textBox2.Text;
                mail.Body = textBox3.Text;

                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential(username.Text, password.Text);
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);
                MessageBox.Show("mail Send");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
}

Open in new window

0
 
LVL 7

Author Comment

by:barlet
ID: 36588904
Hi jonnidip and brutaldev,

Yes i would like in one textbox to write several emails and sent...
when I run your code (jonnidip) it gives me an error...
Please see the picture below...

Thank you for your replies
mail-error.png
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 11

Accepted Solution

by:
brutaldev earned 500 total points
ID: 36588959
Just replace the mail.To.Add line with the foreach loop:
...
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

mail.From = new MailAddress("xxxxxxxxx@gmail.com");
foreach (string email in textBox1.Text.Split(';'))
{
  mail.To.Add(email);
}
mail.Subject = textBox2.Text;
...

Open in new window


jonnidip's solution is very inefficient and will also cause multiple pop-ups.
0
 
LVL 7

Author Closing Comment

by:barlet
ID: 36589023
Thank you... Great work...
0
 
LVL 13

Expert Comment

by:jonnidip
ID: 36589028
It is not a problem of efficiency, but rather what the user needs. If it was a notifier, I would expect one mail for each recipient.

@barlet: please replace " with single quote.
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question