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

C#中发送邮件,包含Html代码 CDO.Message的更多相关文章

  1. jmeter+Jenkins 持续集成中发送邮件报错:MessagingException message: Exception reading response

    已经配置好了发送邮件的相关信息,但是执行完脚本出现报错:MessagingException message: Exception reading response 1.查看Jenkins本次构建的控 ...

  2. MVC中提交包含HTML代码的页面处理方法(尤其是在使用kindeditor富文本编辑器的时候)

    针对文本框中有HTML代码提交时,mvc的action默认会阻止提交,主要是出于安全考虑.如果有时候需求是要将HTML代码同表单一起提交,那么这时候我们可以采取以下两种办法实现: 1.给Control ...

  3. post 传递参数中包含 html 代码解决办法,js加密,.net解密

    今天遇到一个问题,就是用post方式传递参数,程序在vs中完美调试,但是在iis中,就无法运行了,显示传递的参数获取不到,报错了,查看浏览器请求情况,错误500,服务器内部错误,当时第一想法是接收方式 ...

  4. VBScript使用CDO.Message发送邮件

    Const Email_From = "from@163.com" Const Password = "password" Const Email_To = & ...

  5. C#利用CDO.Message发送邮件

    如何引用CDO.Message? cod.message的引用位置: C:\Windows\System32\cdosys.dll CDO.Message objMail = new CDO.Mess ...

  6. 在Web Page中包含PHP代码

    PHP代码可以出现在Web Page的任何位置,甚至在HTML的标签里面也可以.有4中方式在Web Page中包含PHP代码: 使用<?php ... ?>标签 <!doctype ...

  7. 判断字符串中是否包含Emoji表情代码

    判断字符串中是否包含Emoji表情代码: + (BOOL)stringContainsEmoji:(NSString *)string { __block BOOL returnValue = NO; ...

  8. 一个意想不到的CDO.Message 错误

    原文:一个意想不到的CDO.Message 错误   几个月之前,写了一个服务从MSMQ取消息发群发邮件的程序,一直也没时间测试,今日一试,出现发送邮件时报错,异常情况如下:   "Syst ...

  9. 在vim中 安装php的xdebug和 vdebug插件, 在vim中进行调试php代码

    在vim中 安装php的xdebug和 vdebug插件, 在vim中进行调试php代码 参考: http://www.cnblogs.com/qiantuwuliang/archive/2011/0 ...

随机推荐

  1. 【BZOJ】1257: [CQOI2007]余数之和(除法分块)

    题目 传送门:QWQ 分析 大佬和我说本题是除法分块,莫比乌斯反演中也有用到. QwQ我不会莫比乌斯反演啊~ 题目让我们求  $ \sum_{i=1}^n  k\mod n $ 然后根据$ a \mo ...

  2. Hive基础之Hive环境搭建

    Hive默认元数据信息存储在Derby里,Derby内置的关系型数据库.单Session的(只支持单客户端连接,两个客户端连接过去会报错): Hive支持将元数据存储在关系型数据库中,比如:Mysql ...

  3. Windows Storage Stack

  4. 网络虚拟化中的 offload 技术:LSO/LRO、GSO/GRO、TSO/UFO、RSS、VXLAN

    offload offload特性,主要是指将本来在操作系统协议栈中进行的一些数据包处理(如IP分片.TCP分片.重组.checksum校验等)放到网卡硬件中去做,降低系统 CPU 消耗,提高处理的性 ...

  5. solr入门之权重排序方法初探之使用edismax改变权重

    做搜索引擎避免不了排序问题,当排序没有要求时,solr有自己的排序打分机制及sorce字段 1.无特殊排序要求时,根据查询相关度来进行排序(solr自身规则) 2.当涉及到一个字段来进行相关度排序时, ...

  6. UVA196

    #include<stdio.h> #include<iostream> #include <strstream> using namespace std; #de ...

  7. Win10交换Ctrl和大写键

    打开注册表 [HKEY_LOCAL_MacHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout] "Scancode Map" ...

  8. aix系统使用随笔

    在 Aix操作系统 中,常用的文档编辑命令是 vi.下面,我们就来学习一下有关vi的使用决窍. 在vi中,必须牢记它是有两个状态的 ---- 输入状态与命令状态.由输入状态切换 到命令状态,必须ESC ...

  9. jpa-jpql-basic-test

    jpql 基本测试 //可以使用 JPQL 完成 UPDATE 和 DELETE 操作. @Test public void testExecuteUpdate(){ String jpql = &q ...

  10. eclipse启动tomcat无法访问的解决方法

    转自:https://www.cnblogs.com/longshiyVip/p/4637680.html 问题:: tomcat在eclipse里面能正常启动,但在浏览器中访问http://loca ...