SMTP client StackOverflowException
本文关键字:StackOverflowException client SMTP | 更新日期: 2023-09-27 18:02:40
我基本上创建了一个群发程序,问题是,如果我没有线程,它显然会冻结,它需要能够向用户显示进度。不使用另一个类/对象,它工作得很好。我得到一个stackoverflowexception:
{Cannot evaluate expression because the current thread is in a stack overflow state.}
this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
注意:这可能会误导,因为我甚至还没有创建线程,但它仍然得到stackoverflowexception。
这是代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Windows;
using System.Net.Mail;
namespace Mass_Mail
{
public partial class Form1 : Form
{
Form2 frm = new Form2();
Worker work = new Worker();
public List<String> succed = new List<String>();
public List<String> failed = new List<String>();
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog(); //Skapa och definiera openfiledialog
openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); //sätt startmappen till skrivbordet
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; //textfiler.
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
textBox1.Text = openFileDialog1.FileName.ToString();
using (StreamReader sr = new StreamReader(openFileDialog1.FileName)) {
String line;
while ((line = sr.ReadLine()) != null)
{
textBox2.AppendText(line + Environment.NewLine);
}
}
}
catch (Exception ex) {
MessageBox.Show("Kunde inte öppna " + ex.Message);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
frm.Show();
}
private void Form1_Load(object sender, EventArgs e)
{
frm.comboBox1.SelectedIndex = 0;
}
private void button4_Click(object sender, EventArgs e)
{
// work.sendmail(textBox6.Text);
}
}
public class Worker
{
public void DoWork()
{
string[] lines = frm1.textBox2.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
for (int i = 0; i < lines.Length; i++)
{
sendmail(lines[i]);
}
}
Form1 frm1 = new Form1();
Form2 frm2 = new Form2();
public void sendmail(String mail)
{
try
{
SmtpClient client = new SmtpClient(frm2.textBox1.Text, int.Parse(frm2.textBox2.Text));
MailMessage msg = new MailMessage(frm1.textBox3.Text, mail);
client.Timeout = decimal.ToInt32(frm2.numericUpDown1.Value);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("", "");
switch (frm2.comboBox1.SelectedIndex)
{
case 0:
client.EnableSsl = true;
break;
case 1:
client.EnableSsl = false;
break;
}
msg.Subject = frm1.textBox4.Text;
msg.Body = frm1.textBox5.Text;
msg.BodyEncoding = UTF8Encoding.UTF8;
msg.IsBodyHtml = true;
client.Send(msg);
frm1.succed.Add(mail);
frm1.label6.Text = "Lyckades: " + frm1.succed.Count.ToString();
}
catch (System.Net.Mail.SmtpException ex)
{
MessageBox.Show("ERROR! " + ex);
frm1.failed.Add(mail);
frm1.label7.Text = "Misslyckades: " + frm1.failed.Count.ToString();
}
catch (System.FormatException e)
{
MessageBox.Show("ERROR! " + e);
frm1.failed.Add(mail);
frm1.label7.Text = "Misslyckades: " + frm1.failed.Count.ToString();
}
}
}
}
我要创建一个DoWork线程。我从来没有做过这样的事情,而且我知道目前的代码非常糟糕。任何形式的帮助都是非常感激的。我对c#和多线程也很陌生。Form2只包含包含SMTP设置的文本框等,没有代码。
编辑:如果我不创建一个新对象
Worker work = new Worker();
它工作得很好!
在表单中创建一个新的worker:
Worker work = new Worker();
但是当你创建一个worker时,你创建了一个新表单:
Form1 frm1 = new Form1();
它一直持续,直到它停止。
Remove Worker work = new Worker();从Form 1开始。像这样修改Form 1构造函数:
Worker _worker;
public Form1()
{
InitializeComponent();
_worker = new Worker(this);
}
像这样修改worker:
public class Worker
{
Form1 _form1;
public Worker(Form1 form1)
{
_form1 = form1;
}
...
并从worker中删除Form1 frm1 = new Form1()