一直想写一个asp能用发送邮件的服务器组件,不过用VC太麻烦了,一直都没都手。
前两天看一篇文章,说是asp怎么调用C#写的DLL,一试之下,果然可以,大喜,这下用来写一个发送邮件的东东简单了吧。
呵呵,非常简单,一会就弄好了,不敢独享,先看代码:
一直想写一个asp能用发送邮件的服务器组件,不过用VC太麻烦了,一直都没都手。
前两天看一篇文章,说是asp怎么调用C#写的DLL,一试之下,果然可以,大喜,这下用来写一个发送邮件的东东简单了吧。
呵呵,非常简单,一会就弄好了,不敢独享,先看代码:
1 using System;
2 using System.Net.Mail;
3 using System.Text;
4
5 namespace IMELS
6 {
7 public class SendMail
8 {
9 public SendMail() { }
10
11 private string _to = string.Empty;
12
13 /// <summary>
14 /// 收件人地址,多个用“,”号隔开
15 /// </summary>
16 public string To
17 {
18 set { _to = value; }
19 }
20
21 private string _from = string.Empty;
22
23 /// <summary>
24 /// 发件人地址
25 /// </summary>
26 public string From
27 {
28 set { _from = value; }
29 }
30
31 private string _fromName = string.Empty;
32
33 /// <summary>
34 /// 发件人显示名称
35 /// </summary>
36 public string FromName
37 {
38 set { _fromName = value; }
39 }
40
41 private string _cc = string.Empty;
42
43 /// <summary>
44 /// 抄送,多个用“,”号隔开
45 /// </summary>
46 public string CC
47 {
48 set { _cc = value; }
49 }
50
51 private string _bcc = string.Empty;
52
53 /// <summary>
54 /// 密抄,多个用“,”号隔开
55 /// </summary>
56 public string BCC
57 {
58 set { _bcc = value; }
59 }
60
61 private string _charset = "GB2312";
62
63 /// <summary>
64 /// 邮件正文的编码
65 /// </summary>
66 public string Charset
67 {
68 set { _charset = value; }
69 }
70
71 private string _contentType = "html";
72 /// <summary>
73 /// 邮件格式(html or txt)
74 /// </summary>
75 public string ContentType
76 {
77 set { _contentType = value; }
78 }
79
80 private string _subject = string.Empty;
81 /// <summary>
82 /// 邮件标题
83 /// </summary>
84 public string Subject
85 {
86 set { _subject = value; }
87 }
88
89 private string _body = string.Empty;
90 /// <summary>
91 /// 邮件内容
92 /// </summary>
93 public string Body
94 {
95 set { _body = value; }
96 }
97
98 private string _smtp;
99 /// <summary>
100 /// SMTP服务器地址
101 /// </summary>
102 public string Smtp
103 {
104 set { _smtp = value; }
105 }
106
107 private string _username;
108 /// <summary>
109 /// SMTP用户名
110 /// </summary>
111 public string Username
112 {
113 set { _username = value; }
114 }
115 /// <summary>
116 /// SMTP密码
117 /// </summary>
118 private string _password;
119
120 public string Password
121 {
122 set { _password = value; }
123 }
124
125 private int _port = 25;
126 /// <summary>
127 /// SMTP商品
128 /// </summary>
129 public int Port
130 {
131 set { _port = value; }
132 }
133
134 /// <summary>
135 /// 发送
136 /// </summary>
137 public void Send()
138 {
139 MailAddress from = new MailAddress(_from, _fromName);
140 MailMessage message = new MailMessage();
141 message.From = from;
142
143 string[] toadd = _to.Split(',');
144 foreach (string _add in toadd)
145 {
146 try
147 {
148 message.To.Add(new MailAddress(_add));
149 }
150 catch(Exception e)
151 {
152 _error += "To Address Error : " + e.Message + "(" + _add + ");";
153 }
154 }
155
156 if (_cc != string.Empty)
157 {
158
159 string[] ccadd = _cc.Split(',');
160
161 foreach (string _add in ccadd)
162 {
163 try
164 {
165 message.CC.Add(new MailAddress(_add));
166 }
167 catch (Exception e)
168 {
169 _error += "CC Address Error : " + e.Message + "(" + _add + ");";
170 }
171 }
172 }
173 if (_bcc != string.Empty)
174 {
175 string[] bccadd = _bcc.Split(',');
176
177 foreach (string _add in bccadd)
178 {
179 try
180 {
181 message.Bcc.Add(new MailAddress(_add));
182 }
183 catch (Exception e)
184 {
185 _error += "BCC Address Error : " + e.Message + "(" + _add + ");";
186 }
187 }
188 }
189
190 message.Sender = from;
191 message.Subject = _subject;
192 message.Body = _body;
193
194 if (_contentType == "html" || _contentType == string.Empty)
195 {
196 message.IsBodyHtml = true;
197 }
198 else
199 {
200 message.IsBodyHtml = false;
201 }
202
203 message.BodyEncoding = Encoding.GetEncoding(_charset);
204 message.DeliveryNotificationOptions = DeliveryNotificationOptions.None;
205 SmtpClient __smtp = new SmtpClient();
206 __smtp.Host = _smtp;
207 __smtp.Port = _port;
208 __smtp.UseDefaultCredentials = false;
209 __smtp.Credentials = new System.Net.NetworkCredential(_username, _password);
210 __smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
211 try
212 {
213 __smtp.Send(message);
214 }
215 catch (SmtpException e)
216 {
217 _error += "SMTP Error:" + e.Message + ";";
218 }
219
220 }
221
222 private string _error = string.Empty;
223 /// <summary>
224 /// 返回错误信息
225 /// </summary>
226 public string Error
227 {
228 get { return _error; }
229 }
230 /// <summary>
231 /// 清空错误信息
232 /// </summary>
233 public void ClearErr()
234 {
235 _error = string.Empty;
236 }
237 }
238 }
239
2 using System.Net.Mail;
3 using System.Text;
4
5 namespace IMELS
6 {
7 public class SendMail
8 {
9 public SendMail() { }
10
11 private string _to = string.Empty;
12
13 /// <summary>
14 /// 收件人地址,多个用“,”号隔开
15 /// </summary>
16 public string To
17 {
18 set { _to = value; }
19 }
20
21 private string _from = string.Empty;
22
23 /// <summary>
24 /// 发件人地址
25 /// </summary>
26 public string From
27 {
28 set { _from = value; }
29 }
30
31 private string _fromName = string.Empty;
32
33 /// <summary>
34 /// 发件人显示名称
35 /// </summary>
36 public string FromName
37 {
38 set { _fromName = value; }
39 }
40
41 private string _cc = string.Empty;
42
43 /// <summary>
44 /// 抄送,多个用“,”号隔开
45 /// </summary>
46 public string CC
47 {
48 set { _cc = value; }
49 }
50
51 private string _bcc = string.Empty;
52
53 /// <summary>
54 /// 密抄,多个用“,”号隔开
55 /// </summary>
56 public string BCC
57 {
58 set { _bcc = value; }
59 }
60
61 private string _charset = "GB2312";
62
63 /// <summary>
64 /// 邮件正文的编码
65 /// </summary>
66 public string Charset
67 {
68 set { _charset = value; }
69 }
70
71 private string _contentType = "html";
72 /// <summary>
73 /// 邮件格式(html or txt)
74 /// </summary>
75 public string ContentType
76 {
77 set { _contentType = value; }
78 }
79
80 private string _subject = string.Empty;
81 /// <summary>
82 /// 邮件标题
83 /// </summary>
84 public string Subject
85 {
86 set { _subject = value; }
87 }
88
89 private string _body = string.Empty;
90 /// <summary>
91 /// 邮件内容
92 /// </summary>
93 public string Body
94 {
95 set { _body = value; }
96 }
97
98 private string _smtp;
99 /// <summary>
100 /// SMTP服务器地址
101 /// </summary>
102 public string Smtp
103 {
104 set { _smtp = value; }
105 }
106
107 private string _username;
108 /// <summary>
109 /// SMTP用户名
110 /// </summary>
111 public string Username
112 {
113 set { _username = value; }
114 }
115 /// <summary>
116 /// SMTP密码
117 /// </summary>
118 private string _password;
119
120 public string Password
121 {
122 set { _password = value; }
123 }
124
125 private int _port = 25;
126 /// <summary>
127 /// SMTP商品
128 /// </summary>
129 public int Port
130 {
131 set { _port = value; }
132 }
133
134 /// <summary>
135 /// 发送
136 /// </summary>
137 public void Send()
138 {
139 MailAddress from = new MailAddress(_from, _fromName);
140 MailMessage message = new MailMessage();
141 message.From = from;
142
143 string[] toadd = _to.Split(',');
144 foreach (string _add in toadd)
145 {
146 try
147 {
148 message.To.Add(new MailAddress(_add));
149 }
150 catch(Exception e)
151 {
152 _error += "To Address Error : " + e.Message + "(" + _add + ");";
153 }
154 }
155
156 if (_cc != string.Empty)
157 {
158
159 string[] ccadd = _cc.Split(',');
160
161 foreach (string _add in ccadd)
162 {
163 try
164 {
165 message.CC.Add(new MailAddress(_add));
166 }
167 catch (Exception e)
168 {
169 _error += "CC Address Error : " + e.Message + "(" + _add + ");";
170 }
171 }
172 }
173 if (_bcc != string.Empty)
174 {
175 string[] bccadd = _bcc.Split(',');
176
177 foreach (string _add in bccadd)
178 {
179 try
180 {
181 message.Bcc.Add(new MailAddress(_add));
182 }
183 catch (Exception e)
184 {
185 _error += "BCC Address Error : " + e.Message + "(" + _add + ");";
186 }
187 }
188 }
189
190 message.Sender = from;
191 message.Subject = _subject;
192 message.Body = _body;
193
194 if (_contentType == "html" || _contentType == string.Empty)
195 {
196 message.IsBodyHtml = true;
197 }
198 else
199 {
200 message.IsBodyHtml = false;
201 }
202
203 message.BodyEncoding = Encoding.GetEncoding(_charset);
204 message.DeliveryNotificationOptions = DeliveryNotificationOptions.None;
205 SmtpClient __smtp = new SmtpClient();
206 __smtp.Host = _smtp;
207 __smtp.Port = _port;
208 __smtp.UseDefaultCredentials = false;
209 __smtp.Credentials = new System.Net.NetworkCredential(_username, _password);
210 __smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
211 try
212 {
213 __smtp.Send(message);
214 }
215 catch (SmtpException e)
216 {
217 _error += "SMTP Error:" + e.Message + ";";
218 }
219
220 }
221
222 private string _error = string.Empty;
223 /// <summary>
224 /// 返回错误信息
225 /// </summary>
226 public string Error
227 {
228 get { return _error; }
229 }
230 /// <summary>
231 /// 清空错误信息
232 /// </summary>
233 public void ClearErr()
234 {
235 _error = string.Empty;
236 }
237 }
238 }
239
说一下具体实现过程:
1、首先新建一个类库项目;打开项目属性页,在“应用程序”标签设置程序集名称为“IMELS”(当然,这个你可以设置为你喜欢的名字),输出类型为类库,如图:
点击“程序集信息”,勾选“使程序集COM可见”,如图:
2、“签名”标签,勾选“为程序签名”,如图:
然后“在选择强名称密钥文件”下拉列表中选择密钥文件,如果没有密钥文件,就选择“新建”,这里我选择新建,如图:
在“密钥文件名称”栏里输入密钥的名称,你可以选择为密钥添加密码保护它,我这里没有使用密码。
然后为项目添加一个类“SendMail ”,代码就如上了。
3、代码完成后,生成DLL文件,把DLL放到D:盘或别的什么盘,不过最好不要放在系统盘,然后就是注册了,注册C#写的DLL是不能用regsvr32的,要用regasm,格式为:regasm /codebase d:\DLL\IMELS.dll。
这样DLL的编写和注册都已完成了,下面就是应用了,asp中调用方法如下:
1 <%
2 dim send
3 set send = Server.CreateObject("IMELS.SendMail")
4
5 send.From = "test@163.com"
6 send.FromName = "无问"
7 send.Smtp = "smtp.163.com"
8 send.Username = "用户名"
9 send.Password = "密码"
10 send.Subject = "asp调用C#编写的DLL发送邮件测试标题"
11 send.ContentType = "html"
12 send.Charset = "gb2312"
13 send.Body = "asp调用C#编写的DLL发送邮件测试正文"
14 send.To = "to@163.com"
15 send.CC = "抄送地址"
16 send.BCC = "密抄地址"
17 send.Send()
18 Response.Write(send.Error)
19 %>
20
2 dim send
3 set send = Server.CreateObject("IMELS.SendMail")
4
5 send.From = "test@163.com"
6 send.FromName = "无问"
7 send.Smtp = "smtp.163.com"
8 send.Username = "用户名"
9 send.Password = "密码"
10 send.Subject = "asp调用C#编写的DLL发送邮件测试标题"
11 send.ContentType = "html"
12 send.Charset = "gb2312"
13 send.Body = "asp调用C#编写的DLL发送邮件测试正文"
14 send.To = "to@163.com"
15 send.CC = "抄送地址"
16 send.BCC = "密抄地址"
17 send.Send()
18 Response.Write(send.Error)
19 %>
20
好了,大功告成!!