Solved

Sending Multiple Emails in C#

Posted on 2011-09-23
6
388 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

867 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now