C# SendMail发送邮件功能实现
作者:shaomine 时间:2022-05-02 19:40:43
最近因为用的发送邮件的地方,就查询了资料,总结以下几个方法
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 = 0; i < 10; 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 = 0; hcol < dt.Columns.Count; hcol++)
{
MailBody += "<td bgcolor=\"999999\"> ";
MailBody += dt.Columns[hcol].ColumnName;
MailBody += " </td>";
}
MailBody += "</tr>";
for (int row = 0; row < dt.Rows.Count; row++)
{
MailBody += "<tr>";
for (int col = 0; 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 = 465;
//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 = 25;
//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 = 2;
MyFields[@"http://schemas.microsoft.com/cdo/configuration/smtpserverport"].Value = 465;
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,发送邮件
0
投稿
猜你喜欢
java实现文件夹解压和压缩
2022-06-07 03:00:39
c# 三种方法调用WebService接口
2021-08-05 16:49:38
Java反射机制基础详解
2023-07-17 04:36:29
Android Apt之Activity Route的示例
2022-03-31 17:05:18
java8 如何实现分组计算数量和计算总数
2022-05-05 01:17:32
C#使用GDI绘制直线的方法
2022-08-16 09:45:17
Java毕业设计实战之教室预订管理系统的实现
2023-03-03 20:38:11
如何在C#中使用指针
2022-07-02 16:09:47
spring Roo安装使用简介
2023-04-10 09:31:50
Android 使用CoordinatorLayout实现滚动标题栏效果的实例
2021-11-16 07:47:09
Android实现摇一摇功能
2023-07-23 20:21:11
Android 自定义标题栏 显示网页加载进度的方法实例
2023-10-11 09:51:22
kotlin 协程上下文异常处理详解
2022-09-30 06:32:52
Java类中字段可以不赋予初始值的原因分析
2023-01-05 15:55:49
举例分析Python中设计模式之外观模式的运用
2021-11-24 01:54:48
Spring boot使用多线程过程步骤解析
2023-04-03 04:54:50
Java优秀类库Hutool使用示例
2021-12-09 18:12:06
Unity中协程IEnumerator的使用方法介绍详解
2023-03-31 13:28:44
android里TextView加下划线的几种方法总结
2022-03-25 10:42:18
C#中WPF颜色对话框控件的实现
2023-04-13 06:09:41