Code
1 using System;
2
3 using System.Net;
4
5 using System.Net.Mail;
6
7 using System.Net.Mime;
8
9
10
11 namespace Fish
12
13 {
14
15 /// <summary>
16
17 /// 发送邮件的类
18
19 /// </summary>
20
21 public class SendMail
22
23 {
24
25 private MailMessage mailMessage;
26
27 private SmtpClient smtpClient;
28
29 private string password;//发件人密码
30
31
32
33 /// <summary>
34
35 /// 处审核后类的实例
36
37 /// </summary>
38
39 /// <param name="To">收件人地址</param>
40
41 /// <param name="From">发件人地址</param>
42
43 /// <param name="Body">邮件正文</param>
44
45 /// <param name="Title">邮件的主题</param>
46
47 /// <param name="Password">发件人密码</param>
48
49 public SendMail(string To, string From, string Body, string Title, string
50 Password)
51
52 {
53
54 mailMessage = new MailMessage();
55
56 mailMessage.To.Add(To);
57
58 mailMessage.From = new System.Net.Mail.MailAddress(From);
59
60 mailMessage.Subject = Title;
61
62 mailMessage.Body = Body;
63
64 mailMessage.IsBodyHtml = true;
65
66 mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
67
68 mailMessage.Priority = System.Net.Mail.MailPriority.Normal;
69
70 this.password = Password;
71
72 }
73
74
75
76 /// <summary>
77
78 /// 添加附件
79
80 /// </summary>
81
82 public void Attachments(string Path)
83
84 {
85
86 string[] path = Path.Split(',');
87
88
89
90 Attachment data;
91
92 ContentDisposition disposition;
93
94
95
96 for (int i = 0; i < path.Length; i++)
97
98 {
99
100 data = new Attachment(path[i], MediaTypeNames.Application.Octet);//实例化
101 附件
102
103 disposition = data.ContentDisposition;
104
105 disposition.CreationDate = System.IO.File.GetCreationTime(path[i]);//获取
106 附件的创建日期
107
108 disposition.ModificationDate = System.IO.File.GetLastWriteTime(path[i]);//
109 获取附件的修改日期
110
111 disposition.ReadDate = System.IO.File.GetLastAccessTime(path[i]);//获取附
112 件的读取日期
113
114 mailMessage.Attachments.Add(data);//添加到附件中
115
116 }
117
118 }
119
120
121
122
123
124 /// <summary>
125
126 /// 异步发送邮件
127
128 /// </summary>
129
130 /// <param name="CompletedMethod"></param>
131
132 public void SendAsync(SendCompletedEventHandler CompletedMethod)
133
134 {
135
136 if (mailMessage != null)
137
138 {
139
140 smtpClient = new SmtpClient();
141
142 smtpClient.Credentials = new System.Net.NetworkCredential
143 (mailMessage.From.Address, password);//设置发件人身份的票据
144
145 smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
146
147 smtpClient.Host = "smtp." + mailMessage.From.Host;
148
149 smtpClient.SendCompleted += new SendCompletedEventHandler
150 (CompletedMethod);//注册异步发送邮件完成时的事件
151
152 smtpClient.SendAsync(mailMessage, mailMessage.Body);
153
154 }
155
156 }
157
158
159
160 /// <summary>
161
162 /// 发送邮件
163
164 /// </summary>
165
166 public void Send()
167
168 {
169
170 if (mailMessage != null)
171
172 {
173
174 smtpClient = new SmtpClient();
175
176 smtpClient.Credentials = new System.Net.NetworkCredential
177 (mailMessage.From.Address, password);//设置发件人身份的票据
178
179 smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
180
181 smtpClient.Host = "smtp." + mailMessage.From.Host;
182
183 smtpClient.Send(mailMessage);
184
185 }
186
187 }
188
189 }
190
191 }
1 using System;
2
3 using System.Net;
4
5 using System.Net.Mail;
6
7 using System.Net.Mime;
8
9
10
11 namespace Fish
12
13 {
14
15 /// <summary>
16
17 /// 发送邮件的类
18
19 /// </summary>
20
21 public class SendMail
22
23 {
24
25 private MailMessage mailMessage;
26
27 private SmtpClient smtpClient;
28
29 private string password;//发件人密码
30
31
32
33 /// <summary>
34
35 /// 处审核后类的实例
36
37 /// </summary>
38
39 /// <param name="To">收件人地址</param>
40
41 /// <param name="From">发件人地址</param>
42
43 /// <param name="Body">邮件正文</param>
44
45 /// <param name="Title">邮件的主题</param>
46
47 /// <param name="Password">发件人密码</param>
48
49 public SendMail(string To, string From, string Body, string Title, string
50 Password)
51
52 {
53
54 mailMessage = new MailMessage();
55
56 mailMessage.To.Add(To);
57
58 mailMessage.From = new System.Net.Mail.MailAddress(From);
59
60 mailMessage.Subject = Title;
61
62 mailMessage.Body = Body;
63
64 mailMessage.IsBodyHtml = true;
65
66 mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
67
68 mailMessage.Priority = System.Net.Mail.MailPriority.Normal;
69
70 this.password = Password;
71
72 }
73
74
75
76 /// <summary>
77
78 /// 添加附件
79
80 /// </summary>
81
82 public void Attachments(string Path)
83
84 {
85
86 string[] path = Path.Split(',');
87
88
89
90 Attachment data;
91
92 ContentDisposition disposition;
93
94
95
96 for (int i = 0; i < path.Length; i++)
97
98 {
99
100 data = new Attachment(path[i], MediaTypeNames.Application.Octet);//实例化
101 附件
102
103 disposition = data.ContentDisposition;
104
105 disposition.CreationDate = System.IO.File.GetCreationTime(path[i]);//获取
106 附件的创建日期
107
108 disposition.ModificationDate = System.IO.File.GetLastWriteTime(path[i]);//
109 获取附件的修改日期
110
111 disposition.ReadDate = System.IO.File.GetLastAccessTime(path[i]);//获取附
112 件的读取日期
113
114 mailMessage.Attachments.Add(data);//添加到附件中
115
116 }
117
118 }
119
120
121
122
123
124 /// <summary>
125
126 /// 异步发送邮件
127
128 /// </summary>
129
130 /// <param name="CompletedMethod"></param>
131
132 public void SendAsync(SendCompletedEventHandler CompletedMethod)
133
134 {
135
136 if (mailMessage != null)
137
138 {
139
140 smtpClient = new SmtpClient();
141
142 smtpClient.Credentials = new System.Net.NetworkCredential
143 (mailMessage.From.Address, password);//设置发件人身份的票据
144
145 smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
146
147 smtpClient.Host = "smtp." + mailMessage.From.Host;
148
149 smtpClient.SendCompleted += new SendCompletedEventHandler
150 (CompletedMethod);//注册异步发送邮件完成时的事件
151
152 smtpClient.SendAsync(mailMessage, mailMessage.Body);
153
154 }
155
156 }
157
158
159
160 /// <summary>
161
162 /// 发送邮件
163
164 /// </summary>
165
166 public void Send()
167
168 {
169
170 if (mailMessage != null)
171
172 {
173
174 smtpClient = new SmtpClient();
175
176 smtpClient.Credentials = new System.Net.NetworkCredential
177 (mailMessage.From.Address, password);//设置发件人身份的票据
178
179 smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
180
181 smtpClient.Host = "smtp." + mailMessage.From.Host;
182
183 smtpClient.Send(mailMessage);
184
185 }
186
187 }
188
189 }
190
191 }