user2730833
user2730833

Reputation: 183

sending email from g-mail host to five different email accounts using c#

I want to sent email to 5 different email accounts, my problem is in the following code whenever I active those line of code which has "----> this line" it works fine but when I deactivate those line it sends out five email to one email account and nothing to others.

does any one know what is wrong with my code ?

namespace WindowsFormsApplication9
{
    public partial class Form1 : Form
    {
        Thread t = null;
        MailMessage mailMessage;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
          //textBox1 is recipients email addresses 
            String[] to = textBox1.Text.Split(';');

            foreach (String s in to)
            {
                Object[] array = new Object[2];
                array[0] = (textBox4.Text.ToString());
                array[1] = (s.ToString());
               // MessageBox.Show(s.ToString()); -----> this line 
                t = new Thread(sentEmail);
                t.Start(array);
                //MessageBox.Show("from: " + array[0].ToString()); -----> this line 
               // MessageBox.Show("to: " + array[1].ToString()); ----->this line 
                Thread.Sleep(50);


            }       

        }


        void sentEmail(Object array)
        {
            Object[] o = array as Object[];
            SmtpClient client = new SmtpClient();
            client.EnableSsl = true;
            client.Host = "smtp.gmail.com";
            client.Port = 587;
            client.Credentials = new NetworkCredential(textBox4.Text, textBox5.Text);
            mailMessage = new MailMessage(new MailAddress(o[0].ToString()), new MailAddress(o[1].ToString()));
            mailMessage.Body = textBox3.Text;
            mailMessage.Subject = textBox2.Text;
            client.Send(mailMessage);


        }



    }
}

Upvotes: 2

Views: 132

Answers (2)

Matthew
Matthew

Reputation: 9949

You are reusing the mailMessage object. I suspect the lines you have commented out slow the processing down enough that the 5 distinct messages are sent correctly / the thread complete. When they are not there you are getting the weird behaviour as the threads are accessing the same object.

Was going to clean up the code here, but @D_Stanley has you covered.

Upvotes: 1

D Stanley
D Stanley

Reputation: 152596

You're storing the mailMessage as a property of the Form, and the address is getting changed by another thread before it's actually sent. Adding the MessageBox lets one thread finish befiore another one starts. Just change sentMail to create a new MailMessage instead of reusing the existing one and you should be fine:

public partial class Form1 : Form
{
    Thread t = null;
    //MailMessage mailMessage;  <-- take out this line

    void sentEmail(Object array)
    {
        Object[] o = array as Object[];
        SmtpClient client = new SmtpClient();
        client.EnableSsl = true;
        client.Host = "smtp.gmail.com";
        client.Port = 587;
        client.Credentials = new NetworkCredential(textBox4.Text, textBox5.Text);
        MailMessage mailMessage = new MailMessage(new MailAddress(o[0].ToString()), new MailAddress(o[1].ToString()));  // <-- don't use the Form property
        mailMessage.Body = textBox3.Text;
        mailMessage.Subject = textBox2.Text;
        client.Send(mailMessage);


    }

Upvotes: 2

Related Questions