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
Solved

Sending Multiple Emails in C#

Posted on 2011-09-23
6
394 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

808 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