/// <summary> /// Communicate with SMTP server /// </summary> /// <param name="domain"></param> /// <param name="from"></param> /// <param name="recipient"></param> /// <returns></returns> private Result WillAccept(string domain, string from, string recipient) { if (Email.ServerName == string.Empty) { throw new NullReferenceException("No Email.ServerName has been specified"); } System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient(domain, (int)Port.Smtp); _stream = client.GetStream(); _reader = new System.IO.StreamReader(client.GetStream()); Result status = Result.Succeeded; string mailServer = Email.ServerName; string crlf = Environment.NewLine; if (mailServer == null) { mailServer = MxRecord.Infer(from).HostName; } // some servers acknowledge connection if (_reader.Peek() != -1) { this.ValidResponse(Response.ConnectSuccess); } // HELO if (!this.SmtpCommand(string.Format("HELO {0}{1}", mailServer, crlf))) { status = Result.NoServerGreeting; } // MAIL FROM if (status.Contains(Result.Succeeded) && !this.SmtpCommand(string.Format("MAIL FROM:<{0}>{1}", from, crlf))) { status = Result.RefusedMailFrom; } // RCPT TO if (status.Contains(Result.Succeeded) && !this.SmtpCommand(string.Format("RCPT TO:<{0}>{1}", recipient, crlf))) { status = Result.AddressNotFound; } // RCPT TO random // if random address is successful then server is not really validating if (status.Contains(Result.Succeeded) && this.SmtpCommand(string.Format("RCPT TO:<{0}@{1}>{2}", _random, this.HostPart(recipient), crlf))) { status = Result.ValidatedBadAddress; } // QUIT this.SmtpCommand(string.Format("QUIT{0}", crlf), Response.QuitSuccess); _stream.Close(); _reader.Close(); client.Close(); return(status); }
/// <summary> /// Validate an e-mail address by checking the mail server /// </summary> /// <param name="from"></param> /// <param name="recipient"></param> /// <returns></returns> public Stack <string> Ping(string from, string recipient) { if (!Regex.IsMatch(recipient, _pattern)) { _stack.Push(Result.BadAddressFormat.ToString()); return(_stack); } if (_port25blocked) { // execute through web service /*com.vasst.SMTP smtp = new com.vasst.SMTP(); * //Dim smtp As New com.webott.Utility * object[] response = smtp.Ping(recipient); * _stack.Clear(); * for (int x = response.Length - 1; x >= 0; x += -1) { * _stack.Push(response(x)); * } */ } else { // execute locally Result status = Result.Failed; string domain = this.HostPart(recipient); if (_testDomain) { try { IPHostEntry host = System.Net.Dns.GetHostEntry(domain); } catch { _stack.Push(Result.DomainNotResolved.ToString()); return(_stack); } } List <MxRecord> mxRecords = DNS.MxQuery(domain); if (mxRecords.Count == 0) { // if no records found then try common mail server name status = Result.NoMxRecords; mxRecords.Add(MxRecord.Infer(domain)); } else { mxRecords.Sort(); } for (int x = 0; x <= mxRecords.Count - 1; x++) { status = this.WillAccept(mxRecords[x].HostName, from, recipient); if (status.Contains(Result.MxComplete)) { break; } } _stack.Push(status.ToString()); } return(_stack); }