.NET发送邮件

/// <summary>
/// 发送邮件
/// </summary>
/// <param name="toEmail">当前操作用户的邮箱</param>
/// <param name="content">邮件内容</param>
/// <param name="Subject">邮件说明(找回密码&&注册)</param>
/// <param name="terraceType">平台(App&&Web)</param>
/// <returns></returns>
public bool SendEmail(string toEmail, string content, string Subject, string terraceType)
{
try
{
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.From = new MailAddress("c@163.com", "平台");
mail.To.Add(toEmail);
mail.Subject = "hr799邮箱" + Subject;
mail.BodyEncoding = Encoding.Default;

StringBuilder strBody = new StringBuilder();
strBody.Append(@"<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
strBody.Append(@"<html xmlns='http://www.w3.org/1999/xhtml/'>");
strBody.Append("<head>");
strBody.Append("<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />");
strBody.Append("<title>hr799邮箱" + Subject + "</title>");
strBody.Append("<style>");
strBody.Append("body{ font-family:"微软雅黑"; font-size:12px;}");
strBody.Append("*{ padding:0px; margin:0px;}");
strBody.Append("li{ list-style:none;}");
strBody.Append(".top{ 650px; height:100px; margin:0 auto; background:#009974;}");
strBody.Append(".top_left{ 140px; height:100px; float:left; line-height:100px; text-align: right; color:#fff; font-size:50px;}");
strBody.Append(".top_right{ 350px; height:65px; float:left; line-height:20px; color:#fff; padding-top:35px;}");
strBody.Append(".top_right p{ line-height:20px; font-size:20px;}");
strBody.Append(".con{ 620px; height:600px; margin:0 auto; padding-top:30px; padding-left:30px;}");
strBody.Append(".con p{ line-height:35px;}");
strBody.Append(".shenqing{620px; height:40px; border-bottom:1px #ccc solid; border-top:1px #CCC solid; margin-top:40px; line-height:20px; color:#CCC; padding:5px 0px;}");
strBody.Append(".ru{620px; height:40px; margin-top:10px; line-height:20px; color:#999; padding:5px 0px;}");
strBody.Append(".bao{620px; height:40px; margin-top:10px; text-align:right; line-height:20px; color:#000; padding:5px 0px;}");
strBody.Append("</style>");
strBody.Append("</head>");
strBody.Append("<body>");
strBody.Append("<div class="top">");
strBody.Append("<div class="top_left">职道网</div>");
strBody.Append("<div class="top_right"><p>专注互联网职业机会</p><p>www.hr799.com</p></div>");
strBody.Append("</div>");
strBody.Append("<div class="con">");
strBody.Append("<p>亲爱的 <a href="#">" + toEmail + "</a>, 您好</p>");
if (terraceType.ToLower() == "app")
{
strBody.Append("<p>您的验证码为: " + content + "</p>");
}
else
{
if (Subject.Contains("注册"))
{
//注册链接
strBody.Append("<p>您的验证码为: " + content + "</p>");
}
else
{
//找回密码链接
strBody.Append("<p>请点击链接找回密码: " + content + "</p>");
strBody.Append("<p>(本链接在1天后失效)</p>");
}
}

strBody.Append("<br> <p>HR799团队</p><p>" + DateTime.Now.ToString("yyyy-MM-dd") + "</p>");
strBody.Append("<div class="shenqing">");
strBody.Append(" </div>");
strBody.Append("<div class="ru">");
strBody.Append("如有任何问题,可以与我们联系,我们会尽快为你解答。<br>Email:<a href="#">capcence2014@163.com</a>");
strBody.Append(" </div>");
strBody.Append("<div class="bao">");
strBody.Append("为了保证邮箱正常接收,请将<a href="#">capcence2014@163.com</a>添加进你的通讯录 </div>");
strBody.Append("</div></body></html>");

mail.Priority = System.Net.Mail.MailPriority.High;
mail.Body = strBody.ToString();

mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("smtp.163.com", 25);
smtp.UseDefaultCredentials = true;
smtp.Credentials = new System.Net.NetworkCredential("capcence2014@163.com", "capcence@2014");

smtp.Timeout = 10000;
smtp.Send(mail);

return true;

}
catch (Exception ex)
{
throw ex;
}
}
#endregion

原文地址:https://www.cnblogs.com/liuruitao/p/4689862.html