ASP invokes dll written by C # to send mail

Source: Internet
Author: User
Tags foreach empty mail split mailmessage smtpclient

Always want to write an ASP can use to send mail server components, but with VC too troublesome, has not all hands.

Two days ago to see an article, that is how the ASP call C # written DLL, a try, really can, exultation, this is used to write a message to send the Dongdong simple bar.

Oh, very simple, a will be done, do not dare to enjoy alone, first look at the code:

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
One private string _to = String. Empty;
12
///<summary>
14///recipient address, multiple with "," number separated
///</summary>
public string to
17 {
set {_to = value;}
19}
20
private String _from = String. Empty;
22
///<summary>
24///Sender Address
///</summary>
Num public string from
27 {
set {_from = value;}
29}
30
to private string _fromname = String. Empty;
32
///<summary>
34///Sender Display Name
///</summary>
The public string FromName
37 {
set {_fromname = value;}
39}
40
A private string _cc = String. Empty;
42
///<summary>
44///cc, multiple with "," number separated
///</summary>
The public string CC
47 {
The set {_cc = value;}
49}
50
Wuyi private String _bcc = String. Empty;
52
///<summary>
54///, multiple with "," number separated
///</summary>
The public string BCC
57 {
The set {_BCC = value;}
59}
60
The private string _charset = "GB2312";
62
///<summary>
64///The message body code
///</summary>
Charset public string
67 {
The set {_charset = value;}
69}
70
The private string _contenttype = "html";
///<summary>
73///Message Format (HTML or TXT)
///</summary>
A public string ContentType
76 {
The set {_contenttype = value;}
78}
79
-Private String _subject = String. Empty;
Bayi///<summary>
82///Message Title
///</summary>
Subject public string
85 {
The set {_subject = value;}
87}
88
_body = string. Empty;
///<summary>
91///Mail Content
///</summary>
The public string body
94 {
The set {_body = value;}
96}
97
The private string _smtp;
///<summary>
///SMTP Server address
///</summary>
102 public String SMTP
103 {
The set {_smtp = value;}
105}
106
The private string _username;
108///<summary>
109///SMTP User name
///</summary>
The public string Username
112 {
113 set {_username = value;}
114}
///<summary>
116///SMTP Password
117///</summary>
118 private String _password;
119
Password public string
121 {
122 set {_password = value;}
123}
124
a private int _port = 25;
126///<summary>
127///SMTP Merchandise
128///</summary>
129 public int Port
130 {
131 set {_port = value;}
132}
133
134///<summary>
135///Send
136///</summary>
137 public void Send ()
138 {
139 MailAddress from = new MailAddress (_from, _fromname);
140 MailMessage message = new MailMessage ();
The message of the 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}
The 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;
The message. BODY = _body;
193
194 if (_contenttype = = "html" | | _contenttype = = string. Empty)
195 {
196 message. Isbodyhtml = true;
197}
198 Else
199 {
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;
The __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///return error message
///</summary>
The public string Error
227 {
228 get {return _error;}
229}
230///<summary>
231///Empty error message
232///</summary>
233 public void Clearerr ()
234 {
235 _error = string. Empty;
236}
237}
238}
239

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.