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. 全国城市空气质量实时数据(PM2.5)实时下载

    国家公布了“http://113.108.142.147:20035/emcpublish/”空气质量实时发布平台,WCF服务地址为“http://113.108.142.147:20035/EnvP ...

  2. Java 中 HashMap 初始化时赋值

      1.HashMap 初始化的文艺写法 HashMap 是一种常用的数据结构,一般用来做数据字典或者 Hash 查找的容器.普通青年一般会这么初始化:HashMap<String, Strin ...

  3. 使用Python调用动态库

    我个人在日常使用电脑时,经常需要使用Google,于是就要切换代理,基本上是一会儿切换为代理,一会儿切换成直连,老是打开internet 选项去设置,很不方便,于是我萌生了一个想法: 做一个开关,我想 ...

  4. 解决Specifying a namespace in include()withou providing an app_name

    python3 Django 环境下,如果你遇到namespace没有注册以及在根目录下urls.py中的include方法的第二个参数namespace添加之后就出错的问题.请在[app_name] ...

  5. C宏定义

    宏定义中宏名一般用大写,用以和一般的变量名区分开来,但是用宏名用小写也没有错; 对程序中用双引号括起来的字符串内的字符,不进行宏替换操作, #include<stdio.h> #defin ...

  6. linux实时流量监控

    在类Unix系统中可以使用top查看系统资源.进程.内存占用等信息.查看网络状态可以使用netstat.nmap等工具.若要查看实时的网络流量,监控TCP/IP连接等,则可以使用iftop. 一.if ...

  7. HTML5 移动端 自定义点击事件

    /* 封装的TAP事件 */ (function () { /** * IOS 和 PC 端 只需要创建一次就能一直使用 * Android 手机 每次使用的时候都需要从新创建 */ function ...

  8. leetcode925

    public class Solution { public bool IsLongPressedName(string name, string typed) { var list1 = new L ...

  9. as3 文本竖排效果实现

    import flash.text.engine.TextBlock; import flash.text.engine.ElementFormat; import flash.text.engine ...

  10. cv::circle《转》

    void circle(CV_IN_OUT Mat& img, Point center, int radius, const Scalar& color, int thickness ...