// GET api/values public async Task <IHttpActionResult> Get(String Emailaddress) { try { if (!IsEmailValid(Emailaddress)) { return(BadRequest("InvalidEmailAddress")); } EmailAccountVerificationProcess process = new EmailAccountVerificationProcess(); ValidateAccountResponse Response = process.ValidateAccount(Emailaddress); return(Ok(new ApiResponse() { Result = Response.Result, VerificationResult = Response.VerificationResult, ErrorCode = Response.ErrorCode })); } catch (Exception ex) { return(InternalServerError(ex)); } }
public ValidateAccountResponse ValidateAccount(string EmailAddress) { ValidateAccountResponse Result = new ValidateAccountResponse() { Result = true, VerificationResult = false, ErrorCode = "" }; try { string receiptTo = EmailAddress; string command = "nslookup -type=MX "; string response = ""; String[] arrSplit = receiptTo.Split('@'); string[] mxUrl = null; string MailExchanger = "mail exchanger ="; string domain = arrSplit[arrSplit.Length-1]; command += domain; ExecuteCommandSync(command, ref response); foreach (String line in response.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)) { if (line.Contains(domain)) { string tempUrl = line.IndexOf("mail exchanger =") < 0 ? null : line.Substring(line.IndexOf(MailExchanger) + MailExchanger.Length).Trim(); if (tempUrl != null) { if (mxUrl == null) { mxUrl = new string[1]; } else { Array.Resize<string>(ref mxUrl, mxUrl.Length + 1); } mxUrl[mxUrl.Length - 1] = tempUrl; } } } //mx servers found if mxUrl!=null if (mxUrl != null) { bool accountFound = false; for (int i = 0; i < mxUrl.Length; i++) { //smtp servers run on either of 25,465,587 ports TcpClient tClient; try { tClient = new TcpClient(mxUrl[i], 25); } catch (Exception ex) { try { tClient = new TcpClient(mxUrl[i], 465); } catch (Exception ex2) { tClient = new TcpClient(mxUrl[i], 587); } } string CRLF = "\r\n"; byte[] dataBuffer; string ResponseString; NetworkStream netStream = tClient.GetStream(); StreamReader reader = new StreamReader(netStream); ResponseString = reader.ReadLine(); /* Perform HELO to SMTP Server and get Response */ dataBuffer = BytesFromString("HELO " + domain + CRLF); netStream.Write(dataBuffer, 0, dataBuffer.Length); ResponseString = reader.ReadLine(); dataBuffer = BytesFromString("MAIL FROM:<*****@*****.**>" + CRLF); netStream.Write(dataBuffer, 0, dataBuffer.Length); ResponseString = reader.ReadLine(); /* Read Response of the RCPT TO Message to know from SMTP if it exist or not */ dataBuffer = BytesFromString("RCPT TO:<" + receiptTo + ">" + CRLF); netStream.Write(dataBuffer, 0, dataBuffer.Length); ResponseString = reader.ReadLine(); if (GetResponseCode(ResponseString) == 250) { accountFound = true; } /* QUIT CONNECTION */ dataBuffer = BytesFromString("QUIT" + CRLF); netStream.Write(dataBuffer, 0, dataBuffer.Length); ResponseString = reader.ReadLine(); tClient.Close(); if (accountFound) break; } Result.VerificationResult= accountFound; } else { Result.VerificationResult=false; Result.ErrorCode = "No MX servers Found"; } } catch (Exception ex) { Result.VerificationResult = false; Result.Result = false; Result.ErrorCode = ex.Message; } return Result; }