ASP調(diào)用C# DLL發(fā)送郵件方法共享
一直想寫一個asp能發(fā)送郵件的服務(wù)器組件,不過用VC太麻煩了,一直都沒都手。
前兩天看一篇文章,說是asp怎么調(diào)用C#寫的DLL,一試之下,果然可以,大喜,這下用來寫一個發(fā)送郵件的東東簡單了吧。
呵呵,非常簡單,一會就弄好了,不敢獨享,先看代碼:
- using System;
- using System.Net.Mail;
- using System.Text;
- namespace IMELS
- {
- public class SendMail
- {
- public SendMail() { }
- private string _to = string.Empty;
- /// < summary>
- /// 收件人地址,多個用“,”號隔開
- /// < /summary>
- public string To
- {
- set { _to = value; }
- }
- private string _from = string.Empty;
- /// < summary>
- /// 發(fā)件人地址
- /// < /summary>
- public string From
- {
- set { _from = value; }
- }
- private string _fromName = string.Empty;
- /// < summary>
- /// 發(fā)件人顯示名稱
- /// < /summary>
- public string FromName
- {
- set { _fromName = value; }
- }
- private string _cc = string.Empty;
- /// < summary>
- /// 抄送,多個用“,”號隔開
- /// < /summary>
- public string CC
- {
- set { _cc = value; }
- }
- private string _bcc = string.Empty;
- /// < summary>
- /// 密抄,多個用“,”號隔開
- /// < /summary>
- public string BCC
- {
- set { _bcc = value; }
- }
- private string _charset = "GB2312";
- /// < summary>
- /// 郵件正文的編碼
- /// < /summary>
- public string Charset
- {
- set { _charset = value; }
- }
- private string _contentType = "html";
- /// < summary>
- /// 郵件格式(html or txt)
- /// < /summary>
- public string ContentType
- {
- set { _contentType = value; }
- }
- private string _subject = string.Empty;
- /// < summary>
- /// 郵件標題
- /// < /summary>
- public string Subject
- {
- set { _subject = value; }
- }
- private string _body = string.Empty;
- /// < summary>
- /// 郵件內(nèi)容
- /// < /summary>
- public string Body
- {
- set { _body = value; }
- }
- private string _smtp;
- /// < summary>
- /// SMTP服務(wù)器地址
- /// < /summary>
- public string Smtp
- {
- set { _smtp = value; }
- }
- private string _username;
- /// < summary>
- /// SMTP用戶名
- /// < /summary>
- public string Username
- {
- set { _username = value; }
- }
- /// < summary>
- /// SMTP密碼
- /// < /summary>
- private string _password;
- public string Password
- {
- set { _password = value; }
- }
- private int _port = 25;
- /// < summary>
- /// SMTP商品
- /// < /summary>
- public int Port
- {
- set { _port = value; }
- }
- /// < summary>
- /// 發(fā)送
- /// < /summary>
- public void Send()
- {
- MailAddress from = new MailAddress(_from, _fromName);
- MailMessage message = new MailMessage();
- message.From = from;
- string[] toadd = _to.Split(',');
- foreach (string _add in toadd)
- {
- try
- {
- message.To.Add(new MailAddress(_add));
- }
- catch(Exception e)
- {
- _error += "To Address Error : " + e.Message + "(" + _add + ");";
- }
- }
- if (_cc != string.Empty)
- {
- string[] ccadd = _cc.Split(',');
- foreach (string _add in ccadd)
- {
- try
- {
- message.CC.Add(new MailAddress(_add));
- }
- catch (Exception e)
- {
- _error += "CC Address Error : " + e.Message + "(" + _add + ");";
- }
- }
- }
- if (_bcc != string.Empty)
- {
- string[] bccadd = _bcc.Split(',');
- foreach (string _add in bccadd)
- {
- try
- {
- message.Bcc.Add(new MailAddress(_add));
- }
- catch (Exception e)
- {
- _error += "BCC Address Error : " + e.Message + "(" + _add + ");";
- }
- }
- }
- message.Sender = from;
- message.Subject = _subject;
- message.Body = _body;
- if (_contentType == "html" || _contentType == string.Empty)
- {
- message.IsBodyHtml = true;
- }
- else
- {
- message.IsBodyHtml = false;
- }
- message.BodyEncoding = Encoding.GetEncoding(_charset);
- message.DeliveryNotificationOptions = DeliveryNotificationOptions.None;
- SmtpClient __smtp = new SmtpClient();
- __smtp.Host = _smtp;
- __smtp.Port = _port;
- __smtp.UseDefaultCredentials = false;
- __smtp.Credentials = new System.Net.NetworkCredential(_username, _password);
- __smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
- try
- {
- __smtp.Send(message);
- }
- catch (SmtpException e)
- {
- _error += "SMTP Error:" + e.Message + ";";
- }
- }
- private string _error = string.Empty;
- /// < summary>
- /// 返回錯誤信息
- /// < /summary>
- public string Error
- {
- get { return _error; }
- }
- /// < summary>
- /// 清空錯誤信息
- /// < /summary>
- public void ClearErr()
- {
- _error = string.Empty;
- }
- }
- }
說一下ASP調(diào)用C# DLL發(fā)送郵件具體實現(xiàn)過程:
1、首先新建一個類庫項目;打開項目屬性頁,在“應(yīng)用程序”標簽設(shè)置程序集名稱為“IMELS”(當然,這個你可以設(shè)置為你喜歡的名字),輸出類型為類庫,如圖:
點擊“程序集信息”,勾選“使程序集COM可見”,如圖:
2、“簽名”標簽,勾選“為程序簽名”,如圖:
然后“在選擇強名稱密鑰文件”下拉列表中選擇密鑰文件,如果沒有密鑰文件,就選擇“新建”,這里我選擇新建,如圖:
在“密鑰文件名稱”欄里輸入密鑰的名稱,你可以選擇為密鑰添加密碼保護它,我這里沒有使用密碼。
然后為項目添加一個類“SendMail ”,代碼就如上了。
3、代碼完成后,生成DLL文件,把DLL放到D:盤或別的什么盤,不過最好不要放在系統(tǒng)盤,然后就是注冊了,注冊C#寫的DLL是不能用regsvr32的,要用regasm,格式為:regasm /codebase d:\DLL\IMELS.dll。
這樣DLL的編寫和注冊都已完成了,下面就是應(yīng)用了,asp中調(diào)用方法如下:
- < %
- dim send
- set send = Server.CreateObject("IMELS.SendMail")
- send.From = "test@163.com"
- send.FromName = "無問"
- send.Smtp = "smtp.163.com"
- send.Username = "用戶名"
- send.Password = "密碼"
- send.Subject = "asp調(diào)用C#編寫的DLL發(fā)送郵件測試標題"
- send.ContentType = "html"
- send.Charset = "gb2312"
- send.Body = "asp調(diào)用C#編寫的DLL發(fā)送郵件測試正文"
- send.To = "to@163.com"
- send.CC = "抄送地址"
- send.BCC = "密抄地址"
- send.Send()
- Response.Write(send.Error)
- %>
好了,大功告成,ASP調(diào)用C# DLL發(fā)送郵件功能就實現(xiàn)了!
【編輯推薦】