欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

在asp.NET 中使用SMTP发送邮件的实现代码

程序员文章站 2024-03-06 23:23:50
核心代码:复制代码 代码如下: public class mail { #region 邮件参数 static public string accountname = sy...
核心代码:
复制代码 代码如下:

public class mail
{
#region 邮件参数
static public string accountname = system.configuration.configurationmanager.appsettings["smtpaccountname"];
static public string password = system.configuration.configurationmanager.appsettings["smtpaccountpw"];
static public string smtpserver = system.configuration.configurationmanager.appsettings["smtpserver"];
static public int smtpport = int.parse(system.configuration.configurationmanager.appsettings["smtpport"]);
#endregion

/// <summary>
/// 邮件发送方法一
/// </summary>
/// <param name="sendto"></param>
/// <param name="subject"></param>
/// <param name="body"></param>
static public void sendmail(string sendto, string subject, string body)
{
//.net smtp
system.web.mail.mailmessage mailmsg = new system.web.mail.mailmessage();
mailmsg.to = sendto;
//mailmsg.cc = cc;
mailmsg.subject = subject;
mailmsg.body = body;
mailmsg.bodyformat = mailformat.html;


//sender here
mailmsg.from = mail.accountname;
// certify needed
mailmsg.fields.add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");//1 is to certify
//the user id
mailmsg.fields.add(
"http://schemas.microsoft.com/cdo/configuration/sendusername",
mail.accountname);
//the password
mailmsg.fields.add(
"http://schemas.microsoft.com/cdo/configuration/sendpassword",
mail.password);

system.web.mail.smtpmail.smtpserver = mail.smtpserver;

system.web.mail.smtpmail.send(mailmsg);

}
/// <summary>
/// 邮件发送方法二
/// </summary>
/// <param name="sendto"></param>
/// <param name="subject"></param>
/// <param name="body"></param>
static public void sendmail2(string sendto, string subject, string body)
{
system.net.mail.mailmessage msg = new system.net.mail.mailmessage(accountname, sendto, subject, body);
msg.from = new system.net.mail.mailaddress(accountname, "mail");
system.net.mail.smtpclient client = new system.net.mail.smtpclient(smtpserver);
msg.isbodyhtml = true;
client.credentials = new system.net.networkcredential(accountname, password);
client.deliverymethod = system.net.mail.smtpdeliverymethod.network;

client.send(msg);
}
}


摘要
本文简单介绍了smtp协议(rfc2554)发送邮件的过程,并讨论了在 .net 中使用smtp发送邮件由简到繁的三种不同方案、各自可能遇到的问题及其解决办法。
目录
? .net的smtp类
? 使用cdo组件发送邮件
? 使用socket撰写邮件发送程序
总结
简介
邮件发送功能常常是许多.net应用,尤其是带网络功能的应用中不可缺少的模块之一,本文就此介绍了使用.net的smtp类库和另两种分别通过cdo(collaboration data objects)及socket来实现发送邮件功能的方法。
.net的smtp类
首先,我们来介绍一下.net类库种自带的smtp类。在.net中的system.web.mail名字空间下,有一个专门使用smtp协议来发送邮件的类:smtpmail,它已能满足最普通的发送邮件的需求。这个类只有一个自己的公共函数--send()和一个公共属性—smtpserver,如下图:
您必须通过smtpserver属性来指定发送邮件的服务器的名称(或ip地址),然后再调用
send()函数来发送邮件。
代码示例如下:
(in c#)
复制代码 代码如下:

using system.web.mail;
public void sendmail()
{
try
{
system.web.mail.mailmessage mymail=new mailmessage();
mymail.from = "myaccount@test.com";
mymail.to = "myaccount@test.com";
mymail.subject = "mailtest";
mymail.priority = mailpriority.low;
mymail.bodyformat = mailformat.text;
mymail.body = "test";
smtpmail.smtpserver="smarthost"; //your smtp server here
smtpmail.send(mymail);
}
catch(exception e)
{
throw e;
}
}

您可以在send函数的参数mailmessage对象中设置邮件的相关属性,如优先级、附件等等。除了以mailmessage对象为参数(如上述代码),send函数还可以简单的直接以邮件的4个主要信息(from,to,subject,messagetext)作为字符串参数来调用。
使用cdo组件发送邮件
cdo是collaboration data objects的简称,它是一组高层的com对象集合,并经历了好几个版本的演化,现在在windows2000和exchange2000中使用的都是cdo2.0的版本(分别为cdosys.dll和cdoex.dll)。cdosys构建在smtp协议和nntp协议之上,并且作为windows2000 server的组件被安装,您可以在系统目录(如c:\winnt或c:\windows)的system32子目录中找到它(cdosys.dll)。
cdo组件相对于先前介绍的smtpmail对象功能更为丰富,并提供了一些smtpmail类所没有提供的功能,如通过需要认证的smtp服务器发送邮件等。
下面一段代码就展示了如何使用cdo组件通过需要认证的smtp服务器发送邮件的过程:
(in c#)
复制代码 代码如下:

public void cdosendmail()
{
try
{
cdo.message omsg = new cdo.message();

omsg.from = "myaccount@test.com";
omsg.to = "myaccount@test.com";
omsg.subject = "mailtest";

omsg.htmlbody = "<html><body>test</body></html>";
cdo.iconfiguration iconfg = omsg.configuration;
adodb.fields ofields = iconfg.fields;

ofields["http://schemas.microsoft.com/cdo/configuration/sendusing"].value=2;
ofields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].value="myaccount@test.com"; //sender mail ofields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].value="myaccount@test.com"; //email account ofields["http://schemas.microsoft.com/cdo/configuration/sendusername"].value="username"; ofields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].value="password"; ofields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].value=1;
//value=0 代表anonymous验证方式(不需要验证)
//value=1 代表basic验证方式(使用basic (clear-text) authentication.
//the configuration sendusername/sendpassword or postusername/postpassword fields are used to specify credentials.)
//value=2 代表ntlm验证方式(secure password authentication in microsoft outlook express)
ofields["http://schemas.microsoft.com/cdo/configuration/languagecode"].value=0x0804;
ofields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].value="smtp.21cn.com";
ofields.update();
omsg.bodypart.charset="gb2312";
omsg.htmlbodypart.charset="gb2312";
omsg.send();
omsg = null;
}
catch (exception e)
{
throw e;
}
}

注意:由于exchange2000的cdo组件cdoex.dll会更新原有的windows2000的cdo组件cdosys.dll,所以如果您希望继续使用cdosys.dll,您必须先通过regsrv32.exe卸载掉cdoex.dll。
使用socket撰写邮件发送程序
当然,如果您觉得smtpmail不能满足您的需求,cdo又不够直截了当,那就只能自己动手了;其实如果您很熟悉socket编程,自己写一个发送邮件的程序并不很难,以下就是一个例子。
首先,我们简单介绍一下带验证的smtp服务器如何使用auth原语进行身份验证,其详细的定义可以参考rfc2554。
具体如下:
1)首先,需要使用ehlo而不是原先的helo。
2)ehlo成功以后,客户端需要发送auth原语,与服务器就认证时用户名和密码的传递方式进行协商。
3)如果协商成功,服务器会返回以3开头的结果码,这是就可以把用户名和密码传给服务器。
4)最后,如果验证成功,就可以开始发信了。
下面是一个实际的例子,客户端在winxp的command窗口中通过“telnet smtp.263.net 25="命令连接到263的smtp服务器发信:
220 welcome to coremail system(with anti-spam) 2.1
ehlo 263.net
250-192.168.30.29
250-pipelining
250-size 10240000
250-etrn
250-auth login
250 8bitmime
auth login
334 vxnlcm5hbwu6
bxlhy2nvdw50
334 ugfzc3dvcmq6
bxlwyxnzd29yza==
235 authentication successful
mail from:myaccount@263.net
250 ok
rcpt to:myaccount@263.net
250 ok
data
354 end data with <cr><lf>.<cr><lf>
this is a testing email.
haha.
.
250 ok: queued as ac5291d6406c4
quit
221 bye
上面的内容就是发信的全过程。其中与身份验证有关的主要是第九到第十四行:
auth login ';';';';客户端输入
334 vxnlcm5hbwu6 ';';';';服务器提示“username:="
bxlhy2nvdw50 ';';';';客户端输入“myaccount="的base64编码
334 ugfzc3dvcmq6 ';';';';服务器提示“password:="
bxlwyxnzd29yza== ';';';';客户端输入“mypassword="的base64编码
235 authentication successful ';';';';服务器端通过验证
从上面的分析可以看出,在这个身份验证过程中,服务器和客户端都直接通过socket传递经过标准base64编码的纯文本。这个过程可以非常方便的用c#实现,或者直接添加到原有的源代码中。
另外,有些esmtp服务器不支持auth login方式的认证,只支持auth cram-md5方式验证。但是这两者之间的区别只是文本的编码方式不同。
实现此功能的源代码可以在sourceforge.net http://sourceforge.net/projects/opensmtp-net/ 上找到下载。下面给出了一个简单的伪码:
复制代码 代码如下:

public void sendmail(mailmessage msg)
{
networkstream nwstream = getconnection();
writetostream(ref nwstream, "ehlo " + smtphost + "\r\n");
string welcomemsg = readfromstream(ref nwstream);
// implement helo command if ehlo is unrecognized.
if (isunknowncommand(welcomemsg))
{
writetostream(ref nwstream, "helo " + smtphost + "\r\n");
}
checkforerror(welcomemsg, replyconstants.ok);
// authentication is used if the u/p are supplied
authlogin(ref nwstream);
writetostream(ref nwstream, "mail from: <" + msg.from.address + ">\r\n");
checkforerror(readfromstream(ref nwstream), replyconstants.ok);
sendrecipientlist(ref nwstream, msg.to);
sendrecipientlist(ref nwstream, msg.cc);
sendrecipientlist(ref nwstream, msg.bcc);
writetostream(ref nwstream, "data\r\n");
checkforerror(readfromstream(ref nwstream), replyconstants.start_input);
if (msg.replyto.name != null && msg.replyto.name.length != 0)
{ writetostream(ref nwstream, "reply-to: \"" + msg.replyto.name + "\" <" +
msg.replyto.address + ">\r\n"); }
else
{ writetostream(ref nwstream, "reply-to: <" + msg.replyto.address + ">\r\n"); }

if (msg.from.name != null && msg.from.name.length != 0)
{ writetostream(ref nwstream, "from: \"" + msg.from.name + "\" <" +
msg.from.address + ">\r\n"); }
else
{ writetostream(ref nwstream, "from: <" + msg.from.address + ">\r\n"); }

writetostream(ref nwstream, "to: " + createaddresslist(msg.to) + "\r\n");

if (msg.cc.count != 0)
{ writetostream(ref nwstream, "cc: " + createaddresslist(msg.cc) + "\r\n"); }
writetostream(ref nwstream, "subject: " + msg.subject + "\r\n");
if (msg.priority != null)
{ writetostream(ref nwstream, "x-priority: " + msg.priority + "\r\n"); }
if (msg.headers.count > 0)
{
sendheaders(ref nwstream, msg);
}

if (msg.attachments.count > 0 || msg.htmlbody != null)
{
sendmessagebody(ref nwstream, msg);
}
else
{
writetostream(ref nwstream, msg.body + "\r\n");
}

writetostream(ref nwstream, "\r\n.\r\n");
checkforerror(readfromstream(ref nwstream), replyconstants.ok);

writetostream(ref nwstream, "quit\r\n");
checkforerror(readfromstream(ref nwstream), replyconstants.quit);
closeconnection();
}
private bool authlogin(ref networkstream nwstream)
{
if (username != null && username.length > 0 && password != null && password.length > 0)
{
writetostream(ref nwstream, "auth login\r\n");
if (authimplemented(readfromstream(ref nwstream)))
{
writetostream(ref nwstream, convert.tobase64string(
encoding.ascii.getbytes(this.username.tochararray())) + "\r\n");
checkforerror(readfromstream(ref nwstream), replyconstants.server_challenge);
writetostream(ref nwstream, convert.tobase64string(encoding.ascii.getbytes(
this.password.tochararray())) + "\r\n");
checkforerror(readfromstream(ref nwstream), replyconstants.auth_successful);
return true;
}
}
return false;
}

总结
本文介绍了.net中三种不同的使用smtp协议发送邮件的方法,其中第一种(使用smtpmail类)方案能满足大部分基本的发送邮件的功能需求,而第二种(使用cdo组件)和第三种(使用socket自己撰写smtp类)方案提供更*和完整的定制方法,比如他们都能实现第一种方案不能做到的通过带认证的smtp服务器发送邮件的功能。