请教一个关于多线程的问题
现在我在一个Windows小程序中要实现若干个功能包括从数据库中导出Excel报表,发送邮件等,用到了多线程,现在我有个问题
我在发送邮件时,点击Button时开启了一个线程,请教高手怎么解决??????????????
private void btnSendEmail_Click(object sender, EventArgs e)
{
ThreadStart tsEmail = new ThreadStart(SendMail);
Thread thread = new Thread(tsEmail);
thread.Start();
}
调用了SendMail()发送邮件的方法,但我在测试用错误的格式输入邮箱名称时,程序提示仍是发送成功,我在检查时发现第一个邮箱确实发送成功了,但后面的邮箱确并没有成功。(我在处理群发邮件时是在文本框中输入多个邮箱地址按“;”号分割的,但我按“,”等非法输入时程序也提示成功 ,当我在关掉程序时,这个线程始终任务管理器重运行,怎么样销毁这个线程,而且我在走单步时,程序进入不了SendMail()方法,直接运行结果。
/// <summary>
/// 发送邮件的方法
/// </summary>
public void SendMail()
{
try
{
FileInfo file = new FileInfo(filename);
if (file.Exists)
{
if (txtEmail1.Text.Trim() != "")
{
textBox1.Text += "邮件发送中。。。。。。" + "\r\n";
MailAddress from = new MailAddress("jpcx@p-pass.com");
string [] strEmailTo= txtEmail1.Text.Trim().Split(';');
foreach (string emailto in strEmailTo)
{
MailMessage aMessage = new MailMessage();
MailAddress to = new MailAddress(emailto);
aMessage.From = from;
aMessage.To.Add(to);
aMessage.Subject = "今日报表传输成功,请查阅!";
Attachment data = new Attachment(filename, MediaTypeNames.Application.Octet);
aMessage.Attachments.Add(data);
SmtpClient client = new SmtpClient("mail.p-pass.com", 25);
System.Net.NetworkCredential login = new System.Net.NetworkCredential("jpcx", "2008jp123456");
client.Credentials = login;
client.Timeout = 600000;
client.Send(aMessage);
data.Dispose();
}
MessageBox.Show("邮件发送成功!");
textBox1.Text += "邮件发送成功!" + "\r\n";
}
else
{
MessageBox.Show("请输入您要发送的目标邮箱!");
}
}
else
{
MessageBox.Show("该附件不存在,请检查!");
}
}
catch (Exception ex)
{
textBox1.Text += "发送邮件失败" + "\r\n";
MessageBox.Show(ex.Message.ToString());
}
}