Solved

Sending Multiple Emails in C#

Posted on 2011-09-23
6
385 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

760 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

20 Experts available now in Live!

Get 1:1 Help Now