使用NET Framework下的SystemNetMailSmtpClient可以很容易的实现邮件发送但现在很多邮件服务器已经支持ESMTP协议而ESMTP最大的区别在于发送时需要提供帐号密码做验证ESMTP通过流的方式发送命令与服务器交互完成邮件的发送与接受更多关于SMTP的命令请参考<> 下面是一个简单的示例(没有做任何判断与封装仅供参考) 代码 string smtp = ; string from = ; string pwd = pwd; string to = ; //建立链接 TcpClient tc = new TcpClient(smtp); byte[] b = new byte[]; tcGetStream()Read(bbLength); ResponseWrite(EncodingUTFGetString(b)+<br/>); //标识身份 WriteMsg(tcGetStream() HELO xxxx\r\n); ResponseWrite(ReadMsg(tcGetStream()) + <br/>); //身份验证开始 WriteMsg(tcGetStream() AUTH LOGIN\r\n); ResponseWrite(ReadMsg(tcGetStream()) + <br/>); //帐号 base编码 byte[] bTxt=new byte[]; bTxt=EncodingUTFGetBytes(from); WriteMsg(tcGetStream() ConvertToBaseString(bTxt)+\r\n); ResponseWrite(ReadMsg(tcGetStream()) + <br/>); //密码base编码 bTxt=EncodingUTFGetBytes(pwd); WriteMsg(tcGetStream() ConvertToBaseString(bTxt) + \r\n); ResponseWrite(ReadMsg(tcGetStream()) + <br/>); //身份验证结束 //发送邮件地址 WriteMsg(tcGetStream() stringFormat(MAIL FROM:{}\r\n from)); ResponseWrite(ReadMsg(tcGetStream()) + <br/>); //接受邮件地址可以有多个RCPT TO WriteMsg(tcGetStream() stringFormat(RCPT TO:{}\r\n to)); ResponseWrite(ReadMsg(tcGetStream()) + <br/>); //初如化 WriteMsg(tcGetStream() DATA \r\n); ResponseWrite(ReadMsg(tcGetStream()) + <br/>); //邮件内容 StringBuilder sb = new StringBuilder(); sbAppendFormat(From:{}\r\n from);//发信人 sbAppendFormat(To:{}\r\n to);//收信人可以有多个用分号隔开 这两行可以与上面的MAIL FROM和RCPT TO不同(显示在邮件内容里的) sbAppendFormat(Date:{}\r\nDateTimeNowToString());//时间 sbAppendFormat(Subject:{}\r\n 测试);//主题 sbAppend(\r\n); sbAppend(内容); //正文 sbAppend(\r\n\r\n); WriteMsg(tcGetStream() sbToString()); ResponseWrite(ReadMsg(tcGetStream()) + <br/>); //结束会话 WriteMsg(tcGetStream() QUIT); //关闭链接 tcClose(); 如果运行正常上面的返回状态依次为 ESMTP CMailServer SMTP Service Ready welcome here VXNlcmhbWU UGFzcdvcmQ OK OK OK send the mail data end with OK |