public async Task <EmailValidationResponse> Validate(string value, bool verify) { var response = new EmailValidationResponse { Address = value }; var parserResult = EmailParser.Validate(value, true, true); response.IsValid = parserResult.IsValid; response.LocalPart = parserResult.LocalPart; response.Domain = parserResult.Domain; if (parserResult.IsValid) { if (!InMemoryCache.SuggestiveDomains.Contains(response.Domain)) { var suggestionResult = EmailSuggestor.Suggest(InMemoryCache.SuggestiveDomains, parserResult.Domain); if (suggestionResult.Score > 0 && suggestionResult.Score <= 1) { response.Suggestion = string.Concat(response.LocalPart, "@", suggestionResult.Domain); } } response.IsDisposable = InMemoryCache.DisposableDomains.Contains(response.Domain); response.IsRole = InMemoryCache.RoleNames.Contains(response.LocalPart); if (verify) { var nameServerResult = await NameServer.CheckNameServer(response.Domain); response.IsValid = nameServerResult.IsValid && (nameServerResult.HasMailExchanger || nameServerResult.HasHostRecord); } } return(response); }
public async Task <IActionResult> EmailValidation([FromHeader(Name = "Grid-Authorization-Token")] string token, [FromRoute] string emailid) { try { if (string.IsNullOrEmpty(token)) { return(Ok(new OperationResponse { HasSucceeded = false, IsDomainValidationErrors = true, Message = EnumExtensions.GetDescription(CommonErrors.TokenEmpty) })); } AdminUsersDataAccess _adminUsersDataAccess = new AdminUsersDataAccess(_iconfiguration); DatabaseResponse tokenAuthResponse = await _adminUsersDataAccess.AuthenticateAdminUserToken(token); if (tokenAuthResponse.ResponseCode == (int)DbReturnValue.AuthSuccess) { if (!((AuthTokenResponse)tokenAuthResponse.Results).IsExpired) { if (!ModelState.IsValid) { LogInfo.Error(StatusMessages.DomainValidationError); new OperationResponse { HasSucceeded = false, IsDomainValidationErrors = true, Message = string.Join("; ", ModelState.Values .SelectMany(x => x.Errors) .Select(x => x.ErrorMessage)) }; } DatabaseResponse configResponseEmail = ConfigHelper.GetValue("EmailValidate", _iconfiguration); List <Dictionary <string, string> > _result = ((List <Dictionary <string, string> >)configResponseEmail.Results); EmailValidationHelper emailhelper = new EmailValidationHelper(); EmailConfig objEmailConfig = new EmailConfig(); objEmailConfig.key = _result.Single(x => x["key"] == "NeverbouceKey")["value"]; objEmailConfig.Email = emailid; objEmailConfig.EmailAPIUrl = _result.Single(x => x["key"] == "Emailurl")["value"]; string configResponse = emailhelper.EmailValidation(objEmailConfig); EmailValidationResponse _response = new EmailValidationResponse(); _response.Status = configResponse; if (configResponse.ToLower().Trim() != "invalid") { _response.IsValid = true; return(Ok(new ServerResponse { HasSucceeded = true, Message = StatusMessages.ValidMessage, Result = _response })); } else { //Invalid email _response.IsValid = false; LogInfo.Error(EnumExtensions.GetDescription(CommonErrors.InvalidEmail)); return(Ok(new ServerResponse { HasSucceeded = true, Message = StatusMessages.InvalidMessage, Result = _response })); } } else { //Token expired LogInfo.Warning(EnumExtensions.GetDescription(CommonErrors.ExpiredToken)); return(Ok(new OperationResponse { HasSucceeded = false, Message = EnumExtensions.GetDescription(DbReturnValue.TokenExpired), IsDomainValidationErrors = true })); } } else { // token auth failure LogInfo.Warning(EnumExtensions.GetDescription(DbReturnValue.TokenAuthFailed)); return(Ok(new OperationResponse { HasSucceeded = false, Message = EnumExtensions.GetDescription(DbReturnValue.TokenAuthFailed), IsDomainValidationErrors = false })); } } catch (Exception ex) { LogInfo.Error(new ExceptionHelper().GetLogString(ex, ErrorLevel.Critical)); return(Ok(new OperationResponse { HasSucceeded = false, Message = StatusMessages.ServerError, IsDomainValidationErrors = false })); } }
public IActionResult EmailValidation([FromHeader(Name = "Grid-General-Token")] string Token, [FromRoute] string emailid) { try { if (!ModelState.IsValid) { LogInfo.Error(StatusMessages.DomainValidationError); new OperationResponse { HasSucceeded = false, IsDomainValidationErrors = true, Message = string.Join("; ", ModelState.Values .SelectMany(x => x.Errors) .Select(x => x.ErrorMessage)) }; } TokenValidationHelper tokenValidationHelper = new TokenValidationHelper(); if (!tokenValidationHelper.ValidateGenericToken(Token, _iconfiguration)) { return(Ok(new OperationResponse { HasSucceeded = false, Message = EnumExtensions.GetDescription(DbReturnValue.TokenAuthFailed), IsDomainValidationErrors = true })); } DatabaseResponse configResponseEmail = ConfigHelper.GetValue("EmailValidate", _iconfiguration); List <Dictionary <string, string> > _result = ((List <Dictionary <string, string> >)configResponseEmail.Results); string email1 = emailid; if (_result.Single(x => x["key"] == "AllowPlusSignEmailAddress")["value"] == "1") { //Remove the Plus sign part from email before sending to neverbounce Regex emailregx = new Regex("(\\+)(.*?)(\\@)"); //Replace the part after the plus "+) sign email1 = emailregx.Replace(emailid, "$3"); //$3 to putback the @symbol } EmailValidationHelper emailhelper = new EmailValidationHelper(); EmailConfig objEmailConfig = new EmailConfig(); objEmailConfig.key = _result.Single(x => x["key"] == "NeverbouceKey")["value"]; objEmailConfig.Email = email1; objEmailConfig.EmailAPIUrl = _result.Single(x => x["key"] == "Emailurl")["value"]; string configResponse = emailhelper.EmailValidation(objEmailConfig); EmailValidationResponse _response = new EmailValidationResponse(); _response.Status = configResponse; if (configResponse.ToLower().Trim() != "invalid") { _response.IsValid = true; return(Ok(new ServerResponse { HasSucceeded = true, Message = StatusMessages.ValidMessage, Result = _response })); } else { //Invalid email _response.IsValid = false; LogInfo.Warning(EnumExtensions.GetDescription(CommonErrors.InvalidEmail)); return(Ok(new ServerResponse { HasSucceeded = true, Message = StatusMessages.InvalidMessage, Result = _response })); } } catch (Exception ex) { EmailValidationResponse _response = new EmailValidationResponse(); _response.Status = "api Error"; _response.IsValid = true; LogInfo.Error(new ExceptionHelper().GetLogString(ex, ErrorLevel.Critical)); return(Ok(new ServerResponse { HasSucceeded = true, Message = StatusMessages.ValidMessage, Result = _response })); } }