写了一个邮件发送小工具,和大家分享下。/*-------------------- btnSend点击事件-----------------------------*/
private void btnSend_Click(object sender, EventArgs e)
{
MailMessage mailMsg = new MailMessage();
//源邮件地址与发件人
mailMsg.From = new MailAddress(txtLoginId.Text.Trim() + "@qq.com", txtCallName.Text.Trim());
//邮件标题
mailMsg.Subject = txtSubject.Text.Trim();
//发送邮件的内容
mailMsg.Body = rtbContent.Text;
mailMsg.IsBodyHtml = true;
//使用简单邮件传输协议发送邮件 smtp.qq.com smtp.163.com等smtp服务器
SmtpClient client = new SmtpClient("smtp.qq.com");
//发件人信息凭证(用户名、密码)
client.Credentials = new NetworkCredential(txtLoginId.Text.Trim(), txtPassword.Text.Trim());
if (cbSendSeparate.Checked)
{
try
{
//创建一个线程(执行带多个参数的方法)
Thread thread = new Thread(SendEmailSeparate);
thread.IsBackground = true;
thread.Start(new List<object>() { mailMsg, client });
MessageBox.Show("邮件发送成功");
}
catch (Exception ex)
{
MessageBox.Show("邮件发送异常" + ex);
}
}
else
{
try
{
//创建一个线程
Thread thread = new Thread(SendEmail);
thread.IsBackground = true;
thread.Start(new List<object>() { mailMsg, client });
MessageBox.Show("邮件发送成功");
}
catch (Exception ex)
{
MessageBox.Show("邮件发送异常" + ex);
}
}
}
/*-------------------- 邮件发送(普通接收人,抄送人,密送人)-----------------------------*/
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="list"></param>
private void SendEmail(object list)
{
List<object> obj = list as List<object>;
MailMessage mailMsg = (MailMessage)obj[0];
SmtpClient client = (SmtpClient)obj[1];
//收件人(可以有多个收件人)
string address = "";
string displayName = "";
if (txtReceive.Text != "")
{
//普通收件人
string[] mailNames = (txtReceive.Text + ";").Split(';');
foreach (string name in mailNames)
{
if (name != string.Empty)
{
if (name.IndexOf('<') > 0)
{
displayName = name.Substring(0, name.IndexOf('<'));
address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
}
else
{
displayName = string.Empty;
address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
}
mailMsg.To.Add(new MailAddress(address, displayName));
}
}
}
if (txtCC.Text != "")
{
//抄送人
string[] mailCCNames = (txtCC.Text + ";").Split(';');
foreach (string name in mailCCNames)
{
if (name != string.Empty)
{
if (name.IndexOf('<') > 0)
{
displayName = name.Substring(0, name.IndexOf('<'));
address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
}
else
{
displayName = string.Empty;
address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
}
mailMsg.CC.Add(new MailAddress(address, displayName));
}
}
}
if (txtBcc.Text != "")
{
//密送人
string[] mailBccNames = (txtBcc.Text + ";").Split(';');
foreach (string name in mailBccNames)
{
if (name != string.Empty)
{
if (name.IndexOf('<') > 0)
{
displayName = name.Substring(0, name.IndexOf('<'));
address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
}
else
{
displayName = string.Empty;
address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
}
mailMsg.Bcc.Add(new MailAddress(address, displayName));
}
}
}
//发送
client.Send(mailMsg);
}
/*-------------------- 邮件单独发送-----------------------------*/
/// <summary>
/// 分别发送
/// </summary>
/// <param name="list"></param>
private void SendEmailSeparate(object list)
{
List<object> obj = list as List<object>;
MailMessage mailMsg = (MailMessage)obj[0];
SmtpClient client = (SmtpClient)obj[1];
//收件人(可以有多个收件人)
string address = "";
string displayName = "";
if (txtReceive.Text != "")
{
//普通收件人
string[] mailNames = (txtReceive.Text + ";").Split(';');
foreach (string name in mailNames)
{
if (name != string.Empty)
{
if (name.IndexOf('<') > 0)
{
displayName = name.Substring(0, name.IndexOf('<'));
address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
}
else
{
displayName = string.Empty;
address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
}
mailMsg.To.Add(new MailAddress(address, displayName));
client.Send(mailMsg);
mailMsg.To.Clear();
}
}
}
}
|
|