Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Sending Multiple Emails in C#

Posted on 2011-09-23
6
Medium Priority
?
406 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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 11

Accepted Solution

by:
brutaldev earned 2000 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

Google Certified Professional - Cloud Architect

This course (1 of 3) is designed to help students who are interested in Google Cloud Platform (GCP) to become familiar with the platform, navigate the console and learn its capabilities. It will also prepare students for the Google Cloud Architect certification exam.

Question has a verified solution.

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

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.   …
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

688 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