//发送电子邮件的方法
private void SendEmail(string mailServerName, string from, string password, string to, string subject, string body)
{
try
{
//编码暂硬性规定为GB2312
Encoding encoding = Encoding.GetEncoding("GB2312");
MailMessage Message = new MailMessage(
new MailAddress(from, "XX", encoding),//第一个是发信人的地址,第二个参数是发信人
new MailAddress(to));//收信人邮箱
Message.SubjectEncoding = encoding;
Message.Subject = subject;//标题
Message.BodyEncoding = encoding;
Message.Body = body; //body
SmtpClient smtpClient = new SmtpClient(mailServerName);//信箱服务器
smtpClient.Credentials = new NetworkCredential(from, password);//信箱的用户名和密码
smtpClient.Timeout = 999999;
smtpClient.Send(Message);
}
catch (FormatException ex)
{
Console.WriteLine(ex.Message);
}
catch (SmtpException ex)
{
Console.WriteLine(ex.Message);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}