前两天客户说网站的发送邮件出了问题,代码是很正常的用smtp发邮件的代码,却总是不能通过验证,很诧异。后来联系客户才知道,原来客户的邮件服务器采用了smtp-after-pop的验证方式,即要先通过pop验证,才能通过smtp发送邮件,所以要在smtp发送代码前先加一部分pop验证的代码:
string CRLF = "\r\n";
TcpClient popServer = new TcpClient(smtpServer, 110);

NetworkStream netStrm = popServer.GetStream();
StreamReader rdStrm = new StreamReader(popServer.GetStream());
rdStrm.ReadLine();

// Login Process
string data = "USER " + sender + CRLF;
byte[] szData = System.Text.Encoding.ASCII.GetBytes(data.ToCharArray());
netStrm.Write(szData, 0, szData.Length);
rdStrm.ReadLine();

data = "PASS " + smtpPwd + CRLF;
szData = System.Text.Encoding.ASCII.GetBytes(data.ToCharArray());
netStrm.Write(szData, 0, szData.Length);
rdStrm.ReadLine();

这个通讯格式是RFC 1725的pop通讯格式
Client : +OK Server POP Ready!!
Client : USER xxx
Server : +OK
Client : PASS yyy
Server : +OK user successfully logged on
Client : STAT
Server : +OK n m
Client : RETR
1Server : +OK
---{ data }-----
Client : QUIT
Server : +OK Server POP signing off