最近因为用的发送邮件的地方,就查询了资料,总结以下几个方法

  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\">&nbsp;&nbsp;&nbsp;";
MailBody += dt.Columns[hcol].ColumnName;
MailBody += "&nbsp;&nbsp;&nbsp;</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\">&nbsp;&nbsp;&nbsp;";
MailBody += dt.Rows[row][col].ToString();
MailBody += "&nbsp;&nbsp;&nbsp;</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 发送邮件的更多相关文章

  1. Linux sendmail发送邮件失败诊断案例(一)

    在新服务器上测试sendmail发送邮件时,发现邮件发送不成功,检查日志文件发现如下错误(Notice:由于涉及公司服务器,邮箱等,故下面hostname.邮箱地址等信息使用xxx代替) tail - ...

  2. Linux SendMail发送邮件失败诊断案例(二)

    Linux上Sendmail经常由于一些配置问题,导致邮件发送失败,下面整理.收集了一些邮件发送失败.异常的案例. 案例1:在新服务器上测试sendmail发送邮件时,发现邮件发送不成功,检查/var ...

  3. Linux SendMail发送邮件失败诊断案例(三)

    一Linux服务器突然发送不出邮件,检查了很多地方都没有发现异常,检查/var/log/maillog发现如下具体信息: Apr 12 00:36:04 mylinux sendmail[4685]: ...

  4. linux利用sendmail发送邮件的方法

    Linux利用sendmail发送邮件, 方法1 安装sendmail即可使用, mail -s "test" user@sohu.com bin/mail会默认使用本地sendm ...

  5. Linux SendMail发送邮件失败诊断案例(四)

    最近又碰到一起Linux下SendMail发送邮件失败的案例,邮件发送后,邮箱收不到具体邮件, 查看日志/var/log/maillog 发现有"DSN: User unknown" ...

  6. Linux简单配置SendMail发送邮件

    本文简单整理了一下如何在Linux服务器上安装.配置SendMail发送邮件的步骤,此文不是配置邮件服务器,具体测试环境为CentOS Linux release 7.2.1511 (Core) ,如 ...

  7. centos下如何使用sendmail发送邮件

    最近在实施服务端日志监控脚本,需要对异常情况发送邮件通知相关责任人,记录下centos通过sendmail发送邮件的配置过程. 一.安装sendmail与mail 1.安装sendmail:  1) ...

  8. php 使用sendmail发送邮件

    php 使用sendmail发送邮件 1.配置php.ini SMTP=smtp.163.com sendmail_from = 17760273453@163.com sendmail_path = ...

  9. Linux系统下sendmail发送邮件失败的问题

         问题是:安装完sendmail,启动服务后,发送邮件第一次发送成功,后面再次无论怎么发送都不行,换邮箱也不行.在确认我的邮件发送格式正确无误后,想到查看邮件发送日志: [root@backu ...

  10. python SendMail 发送邮件

    最近在学习python 时,用到了发送邮件的操作,通过整理总结如下: 1.普通文本邮件 普通文本邮件发送的实现,关键是要将MIMEText中_subtype设置为plain,首先导入smtplib和m ...

随机推荐

  1. POJ2186 (强连通分量缩点后出度为0的分量内点个数)

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27820   Accepted: 11208 De ...

  2. socket中的函数遇见EINTR的处理【转】

    转自:http://blog.chinaunix.net/uid-21501855-id-4490453.html 这几天,写服务器代码过程当中,遇见EINRT信号的问题,我是借鉴 <unp & ...

  3. react native windows 搭建(完整版)

    声明:用windows 搭建的react native只能开发安卓应用 1.准备安装java jdk,以及Android SDK 传送门: java   JDK   Android SDK(自行寻找) ...

  4. Python selenium.webdriver.chrome.options.Options() Examples

    The following are 27 code examples for showing how to use selenium.webdriver.chrome.options.Options( ...

  5. PL/SQL 01 代码编写规则

    1.标识符命名规则当在 PL/SQL 中使用标识符定义变量.常量时,标识符名称必须以字符开始,并且长度不能超过 30 个字符.另外,为了提高程序的可读性,Oracle 建议用户按照以下规则定义各种标识 ...

  6. docker常用命令,安装常用实例,一步式安装mysql

    刚来公司,跟公司测试环境项目的服务器,环境是linux Centos7.2  所有的tomcat都挂载在docker容器下,所以也就学习了一些简单的docker指令(学习之前请了解什么是docker, ...

  7. python排序sorted与sort比较

    Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列. sorted(iterable,key=None,revers ...

  8. 深入理解Java的注解(Annotation):自定义注解入门(2)

    要深入学习注解,我们就必须能定义自己的注解,并使用注解,在定义自己的注解之前,我们就必须要了解Java为我们提供的元注解和相关定义注解的语法. 元注解: 元注解的作用就是负责注解其他注解.Java5. ...

  9. 发布message给其他包使用

    https://answers.ros.org/question/65716/which-is-the-correct-way-to-install-header-files-in-catkin-pa ...

  10. MATLAB求函数零点与极值

    1.      roots函数 针对多项式求零点(详见MATLAB多项式及多项式拟合) 2.      fzero函数 返回一元函数在某个区间内的的零点. x0 = fzero(@(x)x.^2-3* ...