熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> ASP編程 >> 正文

asp調用C#編寫的DLL發送郵件

2022-06-13   來源: ASP編程 

  一直想寫一個asp能用發送郵件的服務器組件不過用VC太麻煩了一直都沒都手

  前兩天看一篇文章說是asp怎麼調用C#寫的DLL一試之下果然可以大喜這下用來寫一個發送郵件的東東簡單了吧

  呵呵非常簡單一會就弄好了不敢獨享先看代碼

   using System;  
  using SystemNetMail;  
  using SystemText;  
    
  namespace IMELS  
  {  
       public class SendMail  
       {  
           public SendMail() { }  
   
          private string _to = stringEmpty;  
   
          /// <summary>  
          /// 收件人地址多個用號隔開  
          /// </summary>  
          public string To  
          {  
              set { _to = value; }  
          }  
   
          private string _from = stringEmpty;  
   
          /// <summary>  
          /// 發件人地址  
          /// </summary>  
          public string From  
          {  
              set { _from = value; }  
          }  
   
          private string _fromName = stringEmpty;  
   
          /// <summary>  
          /// 發件人顯示名稱  
          /// </summary>  
          public string FromName  
          {  
              set { _fromName = value; }  
          }  
   
          private string _cc = stringEmpty;  
   
          /// <summary>  
          /// 抄送多個用號隔開  
          /// </summary>  
          public string CC  
          {  
              set { _cc = value; }  
          }  
   
          private string _bcc = stringEmpty;  
   
          /// <summary>  
          /// 密抄多個用號隔開  
          /// </summary>  
          public string BCC  
          {  
              set { _bcc = value; }  
          }  
   
          private string _charset = GB;  
   
          /// <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 = stringEmpty;  
          /// <summary>  
          /// 郵件標題  
          /// </summary>  
          public string Subject  
          {  
              set { _subject = value; }  
          }  
   
          private string _body = stringEmpty;  
          /// <summary>  
          /// 郵件內容  
          /// </summary>  
          public string Body  
          {  
              set { _body = value; }  
          }  
   
          private string _smtp;  
          /// <summary>  
         /// SMTP服務器地址  
         /// </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 = ;  
         /// <summary>  
         /// SMTP商品  
         /// </summary>  
         public int Port  
         {  
             set { _port = value; }  
         }  
  
         /// <summary>  
         /// 發送  
         /// </summary>  
         public void Send()  
         {  
             MailAddress from = new MailAddress(_from _fromName);  
             MailMessage message = new MailMessage();  
             messageFrom = from;  
               
             string[] toadd = _toSplit();  
             foreach (string _add in toadd)  
             {  
                 try 
                 {  
                     messageToAdd(new MailAddress(_add));  
                 }  
                 catch(Exception e)  
                 {  
                     _error += To Address Error : + eMessage + ( + _add + );;  
                 }  
             }  
  
             if (_cc != stringEmpty)  
             {  
  
                 string[] ccadd = _ccSplit();  
  
                 foreach (string _add in ccadd)  
                 {  
                     try 
                     {  
                         messageCCAdd(new MailAddress(_add));  
                     }  
                     catch (Exception e)  
                     {  
                         _error += CC Address Error : + eMessage + ( + _add + );;  
                     }  
                 }  
             }  
             if (_bcc != stringEmpty)  
             {  
                 string[] bccadd = _bccSplit();  
  
                 foreach (string _add in bccadd)  
                 {  
                     try 
                     {  
                         messageBccAdd(new MailAddress(_add));  
                     }  
                     catch (Exception e)  
                     {  
                         _error += BCC Address Error : + eMessage + ( + _add + );;  
                     }  
                 }  
             }  
  
             messageSender = from;  
             messageSubject = _subject;  
             messageBody = _body;  
  
             if (_contentType == html || _contentType == stringEmpty)  
             {  
                 messageIsBodyHtml = true;  
             }  
             else 
             {  
                 messageIsBodyHtml = false;  
             }  
  
             messageBodyEncoding = EncodingGetEncoding(_charset);  
             messageDeliveryNotificationOptions = DeliveryNotificationOptionsNone;  
             SmtpClient __smtp = new SmtpClient();  
             __smtpHost = _smtp;  
             __smtpPort = _port;  
             __smtpUseDefaultCredentials = false;  
             __smtpCredentials = new SystemNetNetworkCredential(_username _password);  
             __smtpDeliveryMethod = SmtpDeliveryMethodNetwork;  
             try 
             {  
                 __smtpSend(message);  
             }  
             catch (SmtpException e)  
             {  
                 _error += SMTP Error: + eMessage + ;;  
             }  
               
         }  
  
         private string _error = stringEmpty;  
         /// <summary>  
         /// 返回錯誤信息  
         /// </summary>  
         public string Error  
         {  
             get { return _error; }  
         }  
         /// <summary>  
         /// 清空錯誤信息  
         /// </summary>  
         public void ClearErr()  
         {  
             _error = stringEmpty;  
         }  
     }  
} 
 

  說一下具體實現過程

  首先新建一個類庫項目打開項目屬性頁應用程序標簽設置程序集名稱為IMELS(當然這個你可以設置為你喜歡的名字)輸出類型為類庫如圖

  asp調用C#編寫的DLL發送郵件

  點擊程序集信息勾選使程序集COM可見如圖

  asp調用C#編寫的DLL發送郵件

  簽名標簽勾選為程序簽名如圖

  asp調用C#編寫的DLL發送郵件

  然後在選擇強名稱密鑰文件下拉列表中選擇密鑰文件如果沒有密鑰文件就選擇新建這裡我選擇新建如圖

  asp調用C#編寫的DLL發送郵件

  在密鑰文件名稱欄裡輸入密鑰的名稱你可以選擇為密鑰添加密碼保護它我這裡沒有使用密碼

  然後為項目添加一個類SendMail 代碼就如上了

  代碼完成後生成DLL文件把DLL放到D盤或別的什麼盤不過最好不要放在系統盤然後就是注冊了注冊C#寫的DLL是不能用regsvr要用regasm格式為regasm /codebase dDLLIMELSdll

  這樣DLL的編寫和注冊都已完成了下面就是應用了asp中調用方法如下

   <%   
  dim send   
  set send = ServerCreateObject(IMELSSendMail)   
    
  sendFrom =   
  sendFromName = 無問  
  sendSmtp =   
  sendUsername = 用戶名  
  sendPassword = 密碼  
 sendSubject = asp調用C#編寫的DLL發送郵件測試標題  
 sendContentType = html  
 sendCharset = gb  
 sendBody = asp調用C#編寫的DLL發送郵件測試正文  
 sendTo =   
 sendCC = 抄送地址  
 sendBCC = 密抄地址  
 sendSend()   
 ResponseWrite(sendError)   
 %>  
 

  好了大功告成!!


From:http://tw.wingwit.com/Article/program/ASP/201311/21684.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.