Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6195155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:25:09+00:00 2026-05-24T03:25:09+00:00

I am making a program that import emails from txt files and send a

  • 0

I am making a program that import emails from txt files and send a message to them , but i am facing a problem , currently i am using a thread for the sending mail method to prevent the program to stop responding , the exact problem title is:

Invalid operationexception was handeled
>>> Cross-thread operation not valid:
    Control 'richTextBox1' accessed from a thread other than the thread it was created on.

here is the code

    int success = 0;
    int failed = 0;
    int total = 0;
    bool IsRunning;

List<string> list = new List<string>();

private void addmails()
    {
        string path = textBox2.Text;

        foreach (string line in File.ReadAllLines(path))
        {
            list.Add(line);
        }
        IsRunning = true;
    }
    private void sendmails(object sender, DoWorkEventArgs e)
    {
        if (IsRunning == true)
        {
            if (checkBox1.Checked != true)
            {
                SmtpClient client = new SmtpClient(comboBox1.Text);
                client.Credentials = new NetworkCredential(textBox6.Text, textBox7.Text);
                MailMessage message = new MailMessage();
                message.From = new MailAddress(textBox3.Text, textBox1.Text);
                message.Subject = textBox4.Text;
                //message.Body = richTextBox1.Text;
                if (textBox5.Text != "")
                {
                    message.Attachments.Add(new Attachment(textBox5.Text));
                }

                foreach (string eachmail in list)
                {
                    if (IsRunning == true)
                    {
                        try
                        {
                            message.To.Add(eachmail);
                            client.Send(message);
                            listBox1.Items.Add("Successfully sent the message to  : " + eachmail);
                            success++;
                        }
                        catch
                        {
                            listBox1.Items.Add("Failed to send the message to  : " + eachmail);
                            failed++;
                        }
                        message.To.Clear();

                        total++;

                        Thread.Sleep(15);

                        label18.Text = total.ToString();
                        label19.Text = success.ToString();
                        label21.Text = failed.ToString();

                    }
                    else
                    {
                        break;
                    }
                }

                IsRunning = false;
                button3.Text = "Send";
            }

        }
    }

    private void button3_Click(object sender, EventArgs e)
    {
        if (button3.Text == "Send")
        {
            tabControl1.SelectedTab = tabPage3;
            button3.Text = "Stop";

            addmails();

           // IsRunning = true;

            Thread t2 = new Thread(sendmails); // when using that thread i get a cross threading error
            t2.Start();

        }
        else
        {
            IsRunning = false;
            button3.Text = "Send";
            MessageBox.Show("Sending Mails Operation has been terminated","Abort",MessageBoxButtons.OK,MessageBoxIcon.Information);
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:25:10+00:00Added an answer on May 24, 2026 at 3:25 am

    You are using accessing UI members from a different thread rather than the thread that they are created on, you have to use Control.Invoke whenever you want to access control members or method from another thread.
    So, to get this to work you have to “I really would not do this but just answering your question”:

    if (IsRunning == true)
    {
        bool checkbox1Checked;
        string textBox6Text;
        string textBox7Text;
        string textBox3Text;
        string textBox1Text;
        string textBox4Text;
        string richTextBox1Text;
        string textBox5Text;
    
        MethodInvoker getValues = new MethodInvoker(delegate()
        {
            checkbox1Checked = checkbox1.Checked;
            textBox6Text = textBox6.Text;
            textBox7Text = textBox7.Text;
            textBox3Text = textBox3.Text;
            textBox1Text = textBox1.Text;
            textBox4Text = textBox4.Text;
            richTextBox1Text = richTextBox1.Text;
            textBox5Text = textBox5.Text;
        });
    
        if (this.InvokeRequired)
        {
            this.Invoke(getValues);
        }
        else
        {
            getValues();
        }
    
        if (checkBox1Checked != true)
        {
            SmtpClient client = new SmtpClient(comboBox1Text);
            client.Credentials = new NetworkCredential(textBox6Text, textBox7Text);
            MailMessage message = new MailMessage();
            message.From = new MailAddress(textBox3Text, textBox1Text);
            message.Subject = textBox4Text;
            //message.Body = richTextBox1Text;
            if (textBox5Text != "")
            {
                message.Attachments.Add(new Attachment(textBox5Text));
            }
    
            foreach (string eachmail in list)
            {
                if (IsRunning == true)
                {
                    try
                    {
                        message.To.Add(eachmail);
                        client.Send(message);
    
                        MethodInvoker addToListBox = new MethodInvoker(delegate()
                        {
                            listBox1.Items.Add("Successfully sent the message to  : " + eachmail);
                        }); 
                        if (listBox1.InvokeRequired)
                        {
                            listBox1.Invoke(addToListBox);
                        }
                        else
                        {
                            addToListBox();
                        }
    
                        success++;
                    }
                    catch
                    {
                        MethodInvoker addToListBox = new MethodInvoker(delegate()
                        {
                            listBox1.Items.Add("Failed to send the message to  : " + eachmail);
                        });
    
                        if (listBox1.InvokeRequired)
                        {
                            listBox1.Invoke(addToListBox);
                        }
                        else
                        {
                            addToListBox();
                        }
    
                        failed++;
                    }
                    message.To.Clear();
    
                    total++;
    
                    Thread.Sleep(15);
    
                    MethodInvoker updateSatatus = new MethodInvoker(delegate()
                    {
                        label18.Text = total.ToString();
                        label19.Text = success.ToString();
                        label21.Text = failed.ToString();
                    });
    
                    if (this.InvokeRequired)
                    {
                        this.Invoke(updateSatatus);
                    }
                    else
                    {
                        updateSatatus();
                    }
                }
                else
                {
                    break;
                }
            }
    
            IsRunning = false;
            if (button3.InvokeRequired)
            {
                button3.Invoke(new MethodInvoker(delegate() { button3.Text = "Send"; } ));
            }
            else
            {
                button3.Text = "Send";
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making a program that reads and stores data from Windows EventLog files
I'm experimenting with internationalization by making a Hello World program that uses properties files
I have been making a program that creates multiple CSV's from another source CSV
I'm making a program that connects to a website and downloads XML from it.
I'm making a program that will display a few images from a directory beside
I'm making a program that will move around quite huge sets of files, sometimes
I'm making a program that asks for an integer from the user, it then
I'm making a program that fits the wizard concept ideally; the user is walked
I'm making a program that retrieves decently large amounts of data through a python
I noticed while making a program that a lot of my int type variables

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.