最近因为用的发送邮件的地方,就查询了资料,总结以下几个方法
1、利用新浪邮箱发送
2、利用公司邮箱发送
3、利用CDO发送,这种方式要引用Interop.ADODB.dll(http://www.nodevice.com/dll/Interop_ADODB_dll/item20357.html)和Interop.CDO.dll()两个文件
具体代码如下:
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
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 ;
}
}
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。