这是一个可以发送Smtp的一个类,.Net Framework2.0 中的 System.Web.Mail 来开发虽然此命名空间中的类已被否决。但我就是它来开发的,System.Net.Mail 老是报错,只能用英文。所以我也是被逼的啊!!!!
类如下
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Collections;
5
using System.Web.Mail;
6![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
namespace MyClassLibrary
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
9![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
public class MySmtpMail
11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
13
/// 指定电子邮件具de优先级
14
/// </summary>
15
public enum emailPriorityenum
16![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
17![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
18
/// 指定电子邮件具有高优先级
19
/// </summary>
20
High = MailPriority.High,
21![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
22
/// 指定电子邮件具有低优先级
23
/// </summary>
24
Low = MailPriority.Low,
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
26
/// 指定电子邮件具有普通优先级
27
/// </summary>
28
Normal = MailPriority.Normal
29
}
30![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
属性#region 属性
31
string emailServerhost ="smtp.qq.com";
32![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
33
/// 设置用于发送电子邮件的Smtp服务器地址
34
/// </summary>
35
public string EmailServerhost
36![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
37![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return emailServerhost; }
38![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ emailServerhost = value; }
39
}
40
string emailUserpassword = "123456";
41![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
42
/// 设置发送电子邮件的用户密码
43
/// </summary>
44
public string EmailUserpassword
45![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
46![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ emailUserpassword = value; }
47
}
48
string emailUserName = "330514962@qq.com";
49![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
50
/// 获取或设置发送电子邮件的用户名
51
/// </summary>
52
public string EmailUserName
53![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
54![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return emailUserName; }
55![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ emailUserName = value; }
56
}
57![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
string fromemail = "330514962@qq.com";
59![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
60
/// 获取或设置发信人的电子邮件地址
61
/// </summary>
62
public string Fromemail
63![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
64![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return fromemail; }
65![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ fromemail = value; }
66
}
67![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
68
string emailto = "1029589450@qq.com";//"330514962@qq.com";
69![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
70
/// 获取或设置收件者邮箱多个以逗号隔开
71
/// </summary>
72
public string Emailto
73![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
74![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return emailto; }
75![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ emailto = value; }
76
}
77
string emailSubject = string.Empty;
78![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
79
/// 获取或设置电子邮件的主题
80
/// </summary>
81
public string EmailSubject
82![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
83![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return emailSubject; }
84![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ emailSubject = value; }
85
}
86
MailPriority emailPriority = MailPriority.Normal;
87![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
88
/// 获取或设置电子邮件的优先级
89
/// </summary>
90
public emailPriorityenum EmailPriority
91![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
92
get
93![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
94
if (emailPriority == MailPriority.High)
95![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
96
return emailPriorityenum.High;
97
}
98
else if (emailPriority == MailPriority.Low)
99![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
100
return emailPriorityenum.Low;
101
}
102
else
103![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
104
return emailPriorityenum.Normal;
105
}
106
}
107![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ emailPriority = (MailPriority)value; }
108
}
109![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
110
static string emailACopyUser;
111![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
112
/// 获取或设置邮件抄送人,多个以逗号隔开
113
/// </summary>
114
public string EmailACopyUser
115![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
116![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return emailACopyUser; }
117![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ emailACopyUser = value; }
118
}
119
Encoding bodyEncoding = Encoding.UTF8;
120![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
121
/// 获取或设置邮件的编码
122
/// </summary>
123
public Encoding BodyEncoding
124![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
125![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return bodyEncoding; }
126![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ bodyEncoding = value; }
127
}
128![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
129
string emailAttachment;
130![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
131
/// 获取或设置邮件所发送的附件的路径,多个以逗号隔开
132
/// </summary>
133
public string EmailAttachment
134![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
135![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return emailAttachment; }
136![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ emailAttachment = value; }
137
}
138![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
139
string emailBody = string.Empty;
140![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
141
/// 获取或设置将要发送的邮件的正文
142
/// </summary>
143
public string EmailBody
144![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
145![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return emailBody; }
146![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ emailBody = value; }
147
}
148
#endregion
149![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
150![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
151![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
邮件发送构造函数#region 邮件发送构造函数
152![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
153![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
154
/// 邮件发送
155
/// </summary>
156
public MySmtpMail()
157![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
158![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
159
}
160![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
161![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
162
/// 邮件发送
163
/// </summary>
164
/// <param name="NewEmailBody">设置邮件的正文</param>
165
public MySmtpMail(string NewEmailBody)
166![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
167
EmailBody = NewEmailBody;
168
}
169![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
170
/// 邮件发送
171
/// </summary>
172
/// <param name="NewEmailBody">设置邮件的正文</param>
173
/// <param name="To">收件者邮箱多个以逗号隔开</param>
174
public MySmtpMail(string NewEmailBody, string To)
175![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
176
EmailBody = NewEmailBody;
177
Emailto = To;
178
}
179![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
180
/// 邮件发送
181
/// </summary>
182
/// <param name="To">收件者邮箱多个以逗号隔开</param>
183
/// <param name="emailAttachment">邮件所发送的附件的路径,多个以逗号隔开</param>
184
public MySmtpMail(string NewEmailBody, string To, string newemailAttachment)
185![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
186
EmailBody = NewEmailBody;
187
Emailto = To;
188
EmailAttachment = newemailAttachment;
189
}
190
#endregion
191![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
192![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
发送E-Mail#region 发送E-Mail
193![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
194
/// 发送邮件
195
/// </summary>
196
/// <returns>如果返回为真 , 标示邮件发送成功,否则为失败</returns>
197
public bool SendMessage()
198![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
199
MailMessage message = new MailMessage();
200
message.From = Fromemail;
201
message.Body = EmailBody;
202
message.BodyEncoding = BodyEncoding;
203
message.Cc = EmailACopyUser;
204
message.Priority = emailPriority;
205
message.Subject = EmailSubject;
206
message.To = Emailto;
207
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
208
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", EmailUserName);
209
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", emailUserpassword);
210
if (EmailAttachment != null && EmailAttachment.Trim()!="")
211![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
212![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
char[] delim = new char[]
{ ',' };
213
foreach (string att in EmailAttachment.Trim().Split(delim))
214![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
215
MailAttachment myAttachment = new MailAttachment(att);
216
message.Attachments.Add(myAttachment);
217
}
218
}
219
try
220![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
221
SmtpMail.SmtpServer = EmailServerhost;
222
SmtpMail.Send(message);
223
}
224
catch (System.Web.HttpException ehttp)
225![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
226
return false;
227
}
228
return true;
229
}
230
#endregion
231
}
232
}
写的很垃圾 , 还望各位高手些指点下