C#代码:

     /// <summary>
/// 发送邮件
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public string SendMail(HttpContext context)
{
try
{
if (!string.IsNullOrEmpty(CookiesHelper.getCookie("send_mail_limit")))
{
return "-5";//每分钟只能发送一次
}
string email = context.Request["email"];
if (string.IsNullOrEmpty(email) || !CommonHelper.IsValidEmail(email))
{
return "-1";//传值为空
} //依据模板生成发送内容
string sendText = "";
string tempPath = context.Server.MapPath("~/EmailTemp/ModifyPwd.txt"); using (StreamReader sr = new StreamReader(tempPath))
{
sendText = sr.ReadToEnd();
}
sendText = sendText.Replace("{UserName_CH}", "星辰");
sendText = sendText.Replace("{UserName_EN}", "star");
sendText = sendText.Replace("{VCode}", "abks"); CommonHelper.SendEmail(email, sendText, Resource.Lang.RetrievePassword);
CookiesHelper.setCookie("send_mail_limit", "SendMail", 1.00);
return "1";//成功
}
catch (Exception)
{
return "-4";//异常
}
}

邮件模板:

亲爱的 <b>{UserName_CH}</b>,您好!
<br/>
您在本平台上提交了修改密码的请求。
<br/>
验证码为:<b>{VCode}</b>,注意区分大小写!
<br/>
请按照页面提示完成密码的修改。
<br/>
(系统邮件,请勿回复)
<br/>
<br/>
<br/>
Dear <b>{UserName_EN}</b> ,
<br/>
You have submitted a request to change the password on the platform.
<br/>
Verificationcode is <b>{VCode}</b> ,please note that the code is case sensitive!
<br/>
Enjoy your time !
<br/>
(Please do not reply.)

C#发送代码:

     /// <summary>
/// 发送邮件1
/// </summary>
/// <param name="AcceptEmail"></param>
/// <param name="sendText"></param>
public static void SendEmail(string AcceptEmail, string sendText, string title)
{
SendSMTPEMail(mail_smtp, mail_main, mail_pwd, AcceptEmail, title, sendText);
}
/// <summary>
/// 发送邮件2
/// </summary>
/// <param name="strSmtpServer"></param>
/// <param name="strFrom"></param>
/// <param name="strFromPass"></param>
/// <param name="strto"></param>
/// <param name="strSubject"></param>
/// <param name="strBody"></param>
public static void SendSMTPEMail(string strSmtpServer, string strFrom, string strFromPass, string strto, string strSubject, string strBody)
{
SmtpClient client = new SmtpClient(strSmtpServer);
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(strFrom, strFromPass);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Port = mail_port;
client.EnableSsl = mail_ssl == "yes"; MailMessage message = new MailMessage(strFrom, strto, strSubject, strBody);
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
client.Send(message);
}

C#配置代码:

     //邮件配置
public static string mail_smtp = System.Configuration.ConfigurationManager.AppSettings["mail_smtp"];
public static string mail_main = System.Configuration.ConfigurationManager.AppSettings["mail_main"];
public static string mail_pwd = System.Configuration.ConfigurationManager.AppSettings["mail_pwd"];
public static int mail_port = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["mail_port"]);
public static string mail_ssl = System.Configuration.ConfigurationManager.AppSettings["mail_ssl"];

web.config:

  <!--邮件配置-->
<add key="mail_smtp" value="smtp.ym.163.com"/>
<add key="mail_main" value="xxxxx@xxxxx.com"/>
<add key="mail_pwd" value="xxxxxx"/>
<add key="mail_port" value="25"/>
<add key="mail_ssl" value="no"/>

使用CDO.Message发送邮件:腾讯企业邮箱有点特别,以上的方法都发送不了,最后找到这个方法可以发送。要引用一个dll,地址:C:\Windows\System32\cdosys.dll

public static void SendMailByCDO()
{
CDO.Message objMail = new CDO.Message();
try
{
objMail.To = "xxx@qq.com";//要发送给哪个邮箱
objMail.From = "xxx@xxx.cn";//你的邮件服务邮箱
objMail.Subject = "这是标题";//邮件主题
objMail.HTMLBody = "这里可以填写html内容";//邮件内容 html
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"].Value = ;//设置端口
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = "smtp.exmail.qq.com";
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value = "xxx@xxx.cn";//发送邮件账户
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpuserreplyemailaddress"].Value = "xxx@xxx.cn";//发送邮件账户
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value = "xxx@xxx.cn";//发送邮件账户
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value = "xxx@xxx.cn";//发送邮件账户
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value = "xxx";//发送邮件账户密码
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = ;
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value = ;
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"].Value = "true";//是否使用ssl //防止中文乱码
objMail.HTMLBodyPart.Charset = "utf-8";
objMail.BodyPart.Charset = "utf-8"; objMail.Configuration.Fields.Update();
objMail.Send();
}
catch (Exception ex) { throw ex; }
finally { }
System.Runtime.InteropServices.Marshal.ReleaseComObject(objMail);
objMail = null;
}

System.Web.Mail:http://www.codingwhy.com/view/616.html

最新文章

  1. Win10 UWP开发系列:实现Master/Detail布局
  2. vert.x学习(三),Web开发之Thymeleaf模板的使用
  3. JAVA调用易信接口向指定好友推送消息(一)背景需求
  4. CCS3.3入门
  5. Werkzeug教程
  6. git add和被ignore的文件
  7. DataSnap
  8. Objective-C时间戳转换的转换和时间
  9. 面试题-Java基础-开发环境
  10. 使用python爬虫抓站的一些技巧总结:进阶篇
  11. select下拉二级联动
  12. 有点难度的JS面试题
  13. 让 CDN 更省流量的 Brotli 算法详解
  14. Python序列结构--列表(一)
  15. 廖雪峰Java5集合-1Java集合简介-1Java结合简介
  16. 在CentOS6.8下安装Docker
  17. 虚拟机环境下DPDK运行时的一些错误解决
  18. js时间戳转换日期格式和日期计算
  19. 【转】【Unity】DateTime各种时间字符串
  20. 【模考】2018.04.08 Connection

热门文章

  1. unity3d的碰撞检测及trigger
  2. php笔记篇(二)
  3. selenium+python自动化92-多线程启动多个不同浏览器
  4. stdio.h头文件中申明的基本函数
  5. 表单:checkbox、radio样式(用图片换掉默认样式)
  6. 制作keil5的pack
  7. JAVA时间进行比较和转换,时间加减得到天数
  8. cnapckSurround c++builder Region 代码折叠快捷键
  9. Mysql 获取当月和上个月第一天和最后一天的解决方案
  10. ansible进阶小技巧--tags