扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:追风 来源:chinabs 2007年9月2日
关键字:
下面是一个实际的例子,客户端在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服务器发送邮件的功能。
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者