C# SendMail 发送邮件
最近因为用的发送邮件的地方,就查询了资料,总结以下几个方法
1、利用新浪邮箱发送
2、利用公司邮箱发送
3、利用CDO发送,这种方式要引用Interop.ADODB.dll(http://www.nodevice.com/dll/Interop_ADODB_dll/item20357.html)和Interop.CDO.dll()两个文件
具体代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;
using System.Data; using CDO;
using ADODB;
namespace SendMailTest
{
class Program
{
static void Main(string[] args)
{
SendMail();
} public static string SendMsg()
{
DataTable dt = new DataTable();
dt.Columns.Add("name");
dt.Columns.Add("date");
dt.Columns.Add("area");
dt.Columns.Add("orgnizer");
dt.Columns.Add("keyword"); for (int i = ; i < ; i++)
{
DataRow dr = dt.NewRow();
dr["name"] = "北文中心影视产权交易平台•影视项目路演季---路演项目征集" + i;
dr["date"] = "2017-06-30";
dr["area"] = "北京市 北京电影学院文创园(平房园区)" + i;
dr["orgnizer"] = "北文中心影视产权交易" + i;
dr["keyword"] = "影视" + i;
dt.Rows.Add(dr); } string MailBody = "<p style=\"font-size: 10pt\">以下内容为系统自动发送,请勿直接回复,谢谢。</p><table cellspacing=\"1\" cellpadding=\"3\" border=\"0\" bgcolor=\"000000\" style=\"font-size: 10pt;line-height: 15px;\">";
MailBody += "<div align=\"center\">";
MailBody += "<tr>";
for (int hcol = ; hcol < dt.Columns.Count; hcol++)
{
MailBody += "<td bgcolor=\"999999\"> ";
MailBody += dt.Columns[hcol].ColumnName;
MailBody += " </td>";
}
MailBody += "</tr>"; for (int row = ; row < dt.Rows.Count; row++)
{
MailBody += "<tr>";
for (int col = ; col < dt.Columns.Count; col++)
{
MailBody += "<td bgcolor=\"dddddd\"> ";
MailBody += dt.Rows[row][col].ToString();
MailBody += " </td>";
}
MailBody += "</tr>";
}
MailBody += "</table>";
MailBody += "</div>";
return MailBody;
} public static void SendMail()
{
MailMessage msg = new MailMessage();
msg.To.Add("xxx@ctrchina.cn"); msg.CC.Add("xxxx@sina.com"); msg.From = new MailAddress("ffff@ctrchina.cn", "ffff", System.Text.Encoding.UTF8);
/* 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码*/
msg.Subject = "这是测试邮件";//邮件标题
msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
//msg.Body = "邮件内容";//邮件内容
msg.Body = SendMsg(); msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
msg.IsBodyHtml = true;//是否是HTML邮件
msg.Priority = MailPriority.High;//邮件优先级 SmtpClient client = new SmtpClient();
//client.Host = "smtp.ctrchina.cn";
client.Host = "210.77.136.200";
client.Port = ;
//client.EnableSsl = true;//经过ssl加密
client.Credentials = new System.Net.NetworkCredential("xxx@ctrchina.cn", "password");
object userState = msg;
try
{
//client.SendAsync(msg, userState);
client.Send(msg); }
catch (System.Net.Mail.SmtpException ex)
{
return;
}
} public static void SendSinaMail()
{
MailMessage msg = new MailMessage();
msg.To.Add("xxx@ctrchina.cn");
//msg.To.Add("xxx@sina.com"); msg.CC.Add("xxx@sina.com"); msg.From = new MailAddress("xxx@sina.com", "shao_sks", System.Text.Encoding.UTF8);
/* 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码*/
msg.Subject = "这是测试邮件";//邮件标题
msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
//msg.Body = "邮件内容";//邮件内容
msg.Body = SendMsg(); msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
msg.IsBodyHtml = true;//是否是HTML邮件
msg.Priority = MailPriority.High;//邮件优先级 SmtpClient client = new SmtpClient();
client.Host = "smtp.sina.com";
client.Port = ;
//client.EnableSsl = true;//经过ssl加密
client.Credentials = new System.Net.NetworkCredential("username", "password");
object userState = msg;
try
{
//client.SendAsync(msg, userState);
client.Send(msg); }
catch (System.Net.Mail.SmtpException ex)
{
return;
}
} public static void SenMail1()
{
try
{
CDO.Message oMsg = new CDO.Message(); Configuration MyConfig = new ConfigurationClass();
Fields MyFields = MyConfig.Fields;
MyFields[@"http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = ;
MyFields[@"http://schemas.microsoft.com/cdo/configuration/smtpserverport"].Value = ;
MyFields[@"http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = "210.77.136.200";
MyFields.Update(); oMsg.Configuration = MyConfig; oMsg.Subject = "Test SMTP2911111";
oMsg.HTMLBody = SendMsg(); oMsg.From = "shaoks@ctrchina.cn";
oMsg.To = "shaoks@ctrchina.cn"; oMsg.Send(); }
catch (Exception ex)
{
return;
}
}
}
}
C# SendMail 发送邮件的更多相关文章
- Linux sendmail发送邮件失败诊断案例(一)
在新服务器上测试sendmail发送邮件时,发现邮件发送不成功,检查日志文件发现如下错误(Notice:由于涉及公司服务器,邮箱等,故下面hostname.邮箱地址等信息使用xxx代替) tail - ...
- Linux SendMail发送邮件失败诊断案例(二)
Linux上Sendmail经常由于一些配置问题,导致邮件发送失败,下面整理.收集了一些邮件发送失败.异常的案例. 案例1:在新服务器上测试sendmail发送邮件时,发现邮件发送不成功,检查/var ...
- Linux SendMail发送邮件失败诊断案例(三)
一Linux服务器突然发送不出邮件,检查了很多地方都没有发现异常,检查/var/log/maillog发现如下具体信息: Apr 12 00:36:04 mylinux sendmail[4685]: ...
- linux利用sendmail发送邮件的方法
Linux利用sendmail发送邮件, 方法1 安装sendmail即可使用, mail -s "test" user@sohu.com bin/mail会默认使用本地sendm ...
- Linux SendMail发送邮件失败诊断案例(四)
最近又碰到一起Linux下SendMail发送邮件失败的案例,邮件发送后,邮箱收不到具体邮件, 查看日志/var/log/maillog 发现有"DSN: User unknown" ...
- Linux简单配置SendMail发送邮件
本文简单整理了一下如何在Linux服务器上安装.配置SendMail发送邮件的步骤,此文不是配置邮件服务器,具体测试环境为CentOS Linux release 7.2.1511 (Core) ,如 ...
- centos下如何使用sendmail发送邮件
最近在实施服务端日志监控脚本,需要对异常情况发送邮件通知相关责任人,记录下centos通过sendmail发送邮件的配置过程. 一.安装sendmail与mail 1.安装sendmail: 1) ...
- php 使用sendmail发送邮件
php 使用sendmail发送邮件 1.配置php.ini SMTP=smtp.163.com sendmail_from = 17760273453@163.com sendmail_path = ...
- Linux系统下sendmail发送邮件失败的问题
问题是:安装完sendmail,启动服务后,发送邮件第一次发送成功,后面再次无论怎么发送都不行,换邮箱也不行.在确认我的邮件发送格式正确无误后,想到查看邮件发送日志: [root@backu ...
- python SendMail 发送邮件
最近在学习python 时,用到了发送邮件的操作,通过整理总结如下: 1.普通文本邮件 普通文本邮件发送的实现,关键是要将MIMEText中_subtype设置为plain,首先导入smtplib和m ...
随机推荐
- C++ 迭代器容器学习
set的一个用法 . difference找差集 union合并set intersection找到交集 #include<iostream> #include<string> ...
- phpstudy2014没有mysqldumpslow.pl及其在性能优化的作用
mysqldumpslow.pl的作用是监控mysql的性能瓶颈的 1)在phpstudy2014中没有这个mysqldumpslow.pl所以需要去下载一个然后放置于mysql/bin的文件夹中,由 ...
- Webpack指南(一):安装,创建项目,配置文件,开发环境以及问题汇总
Webpack是一个现代 JavaScript 应用程序的静态模块打包器(module bundler).当 webpack 处理应用程序时,它会递归地构建一个依赖关系图(dependency gra ...
- Sikuli 安装使用之初体验(为Sikuli X指定jre路径)
Sikuli 是一种新颖的图形脚本语言,在实际的自动化测试中如果仅仅依靠selenium 还是远远不够的,selenium自动化本身是存在着诸多缺陷的,基于浏览器之外的控件 (windows 控件 等 ...
- selenium+python自动化77-autoit文件上传【转载】
前言 关于非input文件上传,点上传按钮后,这个弹出的windows的控件了,已经跳出三界之外了,不属于selenium的管辖范围(selenium不是万能的,只能操作web上元素).autoit工 ...
- hdu 1410(直线与矩形相交)
Intersection Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13528 Accepted: 3521 Des ...
- hdu 1513(滚动数组)
Palindrome Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 移动APP 支付宝快捷支付开发流程
[代码] [Java]代码 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ...
- Laravel5.5 生成测试数据
1.在database/factories/UserFactory.php 中添加 2.在tinker中生成数据 3.数据生成成功
- 将win平台上的mysql数据复制到linux上报错Can't write; duplicate key in table
将win平台上的mysql数据复制到linux上报错Can't write; duplicate key in table xxx 新年新气象,果然在新年的第一天就遇到了一个大坑,项目在win上跑的没 ...