.Net 2.0发邮件示例
using System.Net.Mail;
using System.IO;
SmtpClient mySC = new SmtpClient();
mySC.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
mySC.Host = "smtp.from.com";//指定SMTP服务器
mySC.Credentials = new System.Net.NetworkCredential("myAccount","myPassword");//用户名和密码
MailMessage myMM = new MailMessage("from@from.com","to@to.com");
myMM.Subject = title;//主题
myMM.Body = content;//内容
myMM.BodyEncoding = System.Text.Encoding.UTF8;//正文编码
myMM.IsBodyHtml = true;//设置为HTML格式
myMM.Priority = MailPriority.High;//优先级
try
{
mySC.Send(myMM);//发送邮件
}
catch (Exception error)
{
using (StreamWriter sw = new StreamWriter(logPath, true, System.Text.Encoding.UTF8))
{
//写入错误日志
sw.WriteLine(error.ToString());
sw.Close();
}
}
using System.IO;
SmtpClient mySC = new SmtpClient();
mySC.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
mySC.Host = "smtp.from.com";//指定SMTP服务器
mySC.Credentials = new System.Net.NetworkCredential("myAccount","myPassword");//用户名和密码
MailMessage myMM = new MailMessage("from@from.com","to@to.com");
myMM.Subject = title;//主题
myMM.Body = content;//内容
myMM.BodyEncoding = System.Text.Encoding.UTF8;//正文编码
myMM.IsBodyHtml = true;//设置为HTML格式
myMM.Priority = MailPriority.High;//优先级
try
{
mySC.Send(myMM);//发送邮件
}
catch (Exception error)
{
using (StreamWriter sw = new StreamWriter(logPath, true, System.Text.Encoding.UTF8))
{
//写入错误日志
sw.WriteLine(error.ToString());
sw.Close();
}
}