一直想写一个asp能发送邮件的服务器组件,不过用VC太麻烦了,一直都没都手。
前两天看一篇文章,说是asp怎么调用C#写的DLL,一试之下,果然可以,大喜,这下用来写一个发送邮件的东东简单了吧。
呵呵,非常简单,一会就弄好了,不敢独享,先看代码:
- 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;
- }
- }
- }
说一下ASP调用C# DLL发送邮件具体实现过程:
1、首先新建一个类库项目;打开项目属性页,在“应用程序”标签设置程序集名称为“IMELS”(当然,这个你可以设置为你喜欢的名字),输出类型为类库,如图:
点击“程序集信息”,勾选“使程序集COM可见”,如图:
2、“签名”标签,勾选“为程序签名”,如图:
然后“在选择强名称密钥文件”下拉列表中选择密钥文件,如果没有密钥文件,就选择“新建”,这里我选择新建,如图:
在“密钥文件名称”栏里输入密钥的名称,你可以选择为密钥添加密码保护它,我这里没有使用密码。
然后为项目添加一个类“SendMail ”,代码就如上了。
3、代码完成后,生成DLL文件,把DLL放到D:盘或别的什么盘,不过最好不要放在系统盘,然后就是注册了,注册C#写的DLL是不能用regsvr32的,要用regasm,格式为:regasm /codebase d:\DLL\IMELS.dll。
这样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)
- %>
好了,大功告成,ASP调用C# DLL发送邮件功能就实现了!
【编辑推荐】