public voidSendEmail()
{//string from = "13135692346@163.com";//发送方邮箱
string from = txtFrom.Text.ToString() + "@163.com";
MailMessage newEmail= newMailMessage();#region 发送方邮件newEmail.From= new MailAddress(from, from);#endregion
#region 发送对象,可群发
//newEmail.To.Add(new MailAddress("369608497@qq.com"));//接收方邮箱一//newEmail.To.Add(new MailAddress("13135692346@163.com"));//接收方邮箱二//newEmail.To.Add(new MailAddress("402852049@qq.com"));//369608497@qq.com;13135692346@163.com;402852049@qq.com
string to =txtTo.Text.ToString();string[]tolist=to.Split(';');for (int i = 0; i < tolist.Length; i++)
{
newEmail.To.Add(newMailAddress(tolist[i].ToString()));
}#endregion
#region SubjectnewEmail.Subject= txtSub.Text; //标题
#endregion
#region Body
string strBody = txtContent.Text.ToString(); //html格式,也可以是普通文本格式
newEmail.Body = strBody; //内容
#endregion
#region 上传附件
//Attachment MsgAttach = new Attachment(this.FileUpload1.PostedFile.FileName);//可通过一个FileUpload地址获取附件地址//newEmail.Attachments.Add(MsgAttach);
#endregion
#region DeploymentnewEmail.IsBodyHtml= true; //是否支持html
newEmail.Priority = MailPriority.High; //优先级
#endregion
//发送方服务器信息
SmtpClient smtpClient = newSmtpClient();
smtpClient.UseDefaultCredentials= false;
smtpClient.Credentials= newSystem.Net.NetworkCredential(txtFrom.Text.ToString(), txtPwd.Text.ToString());
smtpClient.Host= "smtp.163.com"; //主机//smtpClient.Send(newEmail);//同步发送,程序将被阻塞
#region 异步发送, 会进入回调函数SendCompletedCallback,来判断发送是否成功smtpClient.SendCompleted+= new SendCompletedEventHandler(SendCompletedCallback);//回调函数
string userState = "测试";
smtpClient.SendAsync(newEmail, userState);#endregion}private void SendCompletedCallback(objectsender, System.ComponentModel.AsyncCompletedEventArgs e)
{if (e.Cancelled) //邮件发送被取消
{//Console.WriteLine("发送被取消!");//Lmsg.Text = "发送被取消!";
Response.Write("alert('发送被取消!');");
}if (e.Error != null) //邮件发送失败
{//Console.WriteLine("发送失败!");//Lmsg.Text = "发送失败!";
Response.Write("alert('发送失败!');");
}else //发送成功
{//Console.WriteLine("发送成功!");//Lmsg.Text = "发送成功!";
Response.Write("alert('发送成功!');");
}
}