Пример #1
0
        public bool CheckMailboxExists(string email, out SmtpStatusCode result)
        {
            try
            {
                using (TcpClient tcpClient = new TcpClient())
                {
                    tcpClient.SendTimeout    = 1000;
                    tcpClient.ReceiveTimeout = 1000;

                    if (!tcpClient.ConnectAsync(_host, _port).Wait(1000))
                    {
                        throw new SmtpClientTimeoutException();
                    }

                    NetworkStream networkStream = tcpClient.GetStream();
                    StreamReader  streamReader  = new StreamReader(networkStream);

                    this.AcceptResponse(streamReader, SmtpStatusCode.ServiceReady);

                    string mailHost = (new MailAddress(email)).Host;

                    this.SendCommand(networkStream, streamReader, "HELO " + mailHost, SmtpStatusCode.Ok);
                    this.SendCommand(networkStream, streamReader, "MAIL FROM:<check@" + mailHost + ">", SmtpStatusCode.Ok);
                    _response = this.SendCommand(networkStream, streamReader, "RCPT TO:<" + email + ">");
                    this.SendCommand(networkStream, streamReader, "QUIT", SmtpStatusCode.ServiceClosingTransmissionChannel, SmtpStatusCode.MailboxUnavailable);

                    result = _response.Code;

                    return(true);
                }
            }
            catch (IOException e)
            {
                // StreamReader problem
            }
            catch (SocketException e)
            {
                // TcpClient problem
            }

            result = SmtpStatusCode.GeneralFailure;
            return(false);
        }
Пример #2
0
 public SmtpService(string host, int port = 25)
 {
     _host     = host;
     _port     = port;
     _response = new SmtpResponse();
 }