当前位置:asp编程网>技术教程>Net教程>  正文

ASP调用C#编写的DLL发送邮件

2012-04-02 17:58:13   来源:网络    作者:佚名   浏览量:2468   收藏
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>   
         /// 发件人地址   
         /// </summary>   
         public string From   
         {   
             set { _from = value; }   
         }   
   
         private string _fromName = string.Empty;   
   
         /// <summary>   
         /// 发件人显示名称   
         /// </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>   
         /// 邮件内容   
         /// </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 = 25;   
         /// <summary>   
         /// SMTP商品   
         /// </summary>   
         public int Port   
         {   
             set { _port = value; }   
         }   
   
         /// <summary>   
         /// 发送   
         /// </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;   
         }   
     }   
}
(鼠标移到代码上去,在代码的顶部会出现四个图标,第一个是查看源代码,第二个是复制代码,第三个是打印代码,第四个是帮助)

说一下具体实现过程:

1、首先新建一个类库项目;打开项目属性页,在“应用程序”标签设置程序集名称为“IMELS”(当然,这个你可以设置为你喜欢的名字),输出类型为类库,如图:

 
点击“程序集信息”,勾选“使程序集COM可见”,如图:
 

2、“签名”标签,勾选“为程序签名”,如图:

 

然后“在选择强名称密钥文件”下拉列表中选择密钥文件,如果没有密钥文件,就选择“新建”,这里我选择新建,如图:
 

在“密钥文件名称”栏里输入密钥的名称,你可以选择为密钥添加密码保护它,我这里没有使用密码。

然后为项目添加一个类“SendMail ”,代码就如上了。

3、代码完成后,生成DLL文件,把DLL放到D:盘或别的什么盘,不过最好不要放在系统盘,然后就是注册了,注册C#写的DLL是不能用regsvr32的,要用C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\regasm.exe,格式为:regasm /codebase d:\DLL\IMELS.dll。(注意:这里的v4.0.30319是你dll开发的framework的版本,我使用Framework4.0开发的,所以这里选择4.0的注册版本)

这样DLL的编写和注册都已完成了,下面就是应用了,asp中调用方法如下:

<% 
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调用C#编写的DLL发送邮件测试标题" 
send.ContentType = "html" 
send.Charset = "gb2312" 
send.Body = "asp调用C#编写的DLL发送邮件测试正文" 
send.To = "to@163.com" 
send.CC = "抄送地址" 
send.BCC = "密抄地址" 
send.Send() 
Response.Write(send.Error) 
%>
(鼠标移到代码上去,在代码的顶部会出现四个图标,第一个是查看源代码,第二个是复制代码,第三个是打印代码,第四个是帮助)

转自:http://www.cnblogs.com/wlkjhxd/archive/2009/03/18/1415037.html



关于我们-广告合作-联系我们-积分规则-网站地图

Copyright(C)2013-2017版权所属asp编程网