smtp 多的不说,虽然已经不受欢迎了,可很多老项目还在使用,对于初学者来说还是很有借鉴意义的。下面展示这个发邮件的方法
using System.Net.Mail;//引入关键包
public static int SendMailsCompany(string to, string body, string subject)
{
//163发邮件
int nContain = 0;
///添加发件人地址
string from = "1234567@qq.com";
MailMessage mailMsg = new MailMessage();
mailMsg.From = new MailAddress(from, "邮件标题");
nContain += mailMsg.From.Address.Length;
///添加收件人地址
mailMsg.To.Add(to);
nContain += mailMsg.To.ToString().Length;
///添加邮件主题
mailMsg.Subject = subject;
mailMsg.SubjectEncoding = Encoding.UTF8;
nContain += mailMsg.Subject.Length;
///添加邮件内容
mailMsg.Body = body;
mailMsg.BodyEncoding = Encoding.UTF8;
mailMsg.IsBodyHtml = true;
nContain += mailMsg.Body.Length;
if (mailMsg.IsBodyHtml == true)
{
nContain += 100;
}
try
{
System.Net.Mail.SmtpClient sc = new System.Net.Mail.SmtpClient();//服务器发件箱的地址、端口
System.Net.NetworkCredential nc = new System.Net.NetworkCredential();//验证的用户名和密码
sc.Credentials = nc;
nc.UserName = "12345@qq.com";
nc.Password = "122334";
sc.Host = "mail.qq.com";
sc.Send(mailMsg);
log.Info("邮件发送成功");
return 1;
}
catch (Exception e)
{
log.Info(e.Message.ToString());
throw e;
}
}