C#中发送邮件,包含Html代码 CDO.Message
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的更多相关文章
- jmeter+Jenkins 持续集成中发送邮件报错:MessagingException message: Exception reading response
已经配置好了发送邮件的相关信息,但是执行完脚本出现报错:MessagingException message: Exception reading response 1.查看Jenkins本次构建的控 ...
- MVC中提交包含HTML代码的页面处理方法(尤其是在使用kindeditor富文本编辑器的时候)
针对文本框中有HTML代码提交时,mvc的action默认会阻止提交,主要是出于安全考虑.如果有时候需求是要将HTML代码同表单一起提交,那么这时候我们可以采取以下两种办法实现: 1.给Control ...
- post 传递参数中包含 html 代码解决办法,js加密,.net解密
今天遇到一个问题,就是用post方式传递参数,程序在vs中完美调试,但是在iis中,就无法运行了,显示传递的参数获取不到,报错了,查看浏览器请求情况,错误500,服务器内部错误,当时第一想法是接收方式 ...
- VBScript使用CDO.Message发送邮件
Const Email_From = "from@163.com" Const Password = "password" Const Email_To = & ...
- C#利用CDO.Message发送邮件
如何引用CDO.Message? cod.message的引用位置: C:\Windows\System32\cdosys.dll CDO.Message objMail = new CDO.Mess ...
- 在Web Page中包含PHP代码
PHP代码可以出现在Web Page的任何位置,甚至在HTML的标签里面也可以.有4中方式在Web Page中包含PHP代码: 使用<?php ... ?>标签 <!doctype ...
- 判断字符串中是否包含Emoji表情代码
判断字符串中是否包含Emoji表情代码: + (BOOL)stringContainsEmoji:(NSString *)string { __block BOOL returnValue = NO; ...
- 一个意想不到的CDO.Message 错误
原文:一个意想不到的CDO.Message 错误 几个月之前,写了一个服务从MSMQ取消息发群发邮件的程序,一直也没时间测试,今日一试,出现发送邮件时报错,异常情况如下: "Syst ...
- 在vim中 安装php的xdebug和 vdebug插件, 在vim中进行调试php代码
在vim中 安装php的xdebug和 vdebug插件, 在vim中进行调试php代码 参考: http://www.cnblogs.com/qiantuwuliang/archive/2011/0 ...
随机推荐
- Spring MVC 原生API
原生API: Servlet环境中一些有用的对象: HttpServletRequest HttpServletResponse HttpSession Reader Writer InputStre ...
- centos 7 mount usb hard disk(ntfs format)
1. yum install -y epel-release* 2. yum install -y ntfs-3g 3. 命令:fdisk -l (查看磁盘分区信息) [root@devserverg ...
- Map 合并
比如说 qq.com 100 163.com 90 QQ.COM 10 Qq.Com 5 …… 如果统计的话,需要忽略大小写的,即 QQ邮箱总共是100+10+5,怎么写? 其实这个应该不难的,就 ...
- Ubuntu16安装QQ
安装教程: 一:安装依赖库 在终端输入sudo apt-get install libgtk2.0-0:i386 另外,如果是64位系统还要安装ia32-libs 这里我们选择安装lib32ncurs ...
- mavenProfile文件配置和简单入门
1什么是MavenProfile 在我们平常的java开发中,会经常使用到很多配制文件(xxx.properties,xxx.xml),而当我们在本地开发(dev),测试环境测试(test),线上生产 ...
- 19.XPath选择器
1.extract():提取数据 2./text() :获取节点内容文本 3./@href :获取节点href属性 4. @ :获取属性名称 需要注意问题: 用定义的规则那 ...
- sqlserver导入导出数据库结构及创建用户分配权限
1.创建用户分配权限 https://www.cnblogs.com/jennyjiang-00/p/5803140.html 2.sqlserver2008导出表结构和表数据 导出表结构 htt ...
- jap -文档 https://www.tutorialspoint.com/jpa/jpa_jpql.htm
参考网址:https://www.tutorialspoint.com/jpa/jpa_jpql.htm
- leetcode367
public class Solution { public bool IsPerfectSquare(int num) { , high = num; while (low <= high) ...
- vnc服务安装与配置
一.Redhat上VNC Server配置 本文以当前Linux系统未安装VNC服务器为基本,如果已安装请跳过第1节! 前提: 1.安装 TigerVNC Server # yum search ti ...