/// <summary> /// Validates the input email request. /// </summary> /// <param name="request">The <see cref="Services.EmailService.MessageContracts.SendEmailRequest"/> to be validated.</param> /// <returns>A <see cref="Services.EmailService.MessageContracts.InvalidRecipients"/> that contains the information if Recipients address is invalid or with bad domain.</returns> private InvalidRecipients ValidateEmailAddress(SendEmailRequest request) { InvalidRecipients resplist = new InvalidRecipients(); Logger.Info("[ValidateEmailAddress] validating Recipients email addresses."); Logger.Info(request.ToString()); //Validate domain foreach (var email in request.Recipients.Where(email => _badDomainCheckProvider.IsBadDomain(email))) { Logger.ErrorFormat("The recipient email address [{0}] is not a supported domain.", email); resplist.BadDomains.Add(email); } if (request.CarbonCopyList == null) { return(resplist); } //validate bad domain foreach (var email in request.CarbonCopyList.Where(email => _badDomainCheckProvider.IsBadDomain(email))) { Logger.ErrorFormat("The cclist email address [{0}] is not a supported domain.", email); resplist.BadDomains.Add(email); } return(resplist); }
/// <summary> /// The implementation of the <see cref="Services.EmailService.ServiceContracts.ISendEmailToValidAddressService.SendEmailWithBadAddressCheck"/>. /// It validates the input request and sends an email. /// </summary> /// <param name="request">The <see cref="Services.EmailService.MessageContracts.SendEmailRequest"/> that contains the input request for sending an email to one or more recipients with one or more <see cref="Services.EmailServiceProvider.Interface.FileAttachment"/>.</param> /// <returns>A <see cref="Services.EmailService.MessageContracts.SendEmailResponseWithBadAddressCheck"/> that contains the information if the email was sent successfully along with list of invalid Recipients.</returns> /// <exception cref="System.ServiceModel.FaultException"> /// Throws <see cref="Services.CommonLibraries.Infrastructure.Faults.SystemFault"/> when any unhandled exception occours. /// </exception> /// <exception cref="System.ServiceModel.FaultException"> /// Throws this fault <see cref="Services.EmailService.FaultContracts.EmailServiceFault.ValidationFailed"/> when the <paramref name="request"/> fails validation. /// Following are the possible validation failures. /// <list type="bullet"> /// <item> /// <term><see cref="Services.EmailService.MessageContracts.SendEmailRequest.Subject"/></term> /// <description>When it is <see langword="null"/> or <see cref="System.String.Empty"/></description> /// </item> /// <item> /// <term><see cref="Services.EmailService.MessageContracts.SendEmailRequest.Body"/></term> /// <description>When it is <see langword="null"/> or <see cref="System.String.Empty"/></description> /// </item> /// <item> /// <term><see cref="Services.EmailService.MessageContracts.SendEmailRequest.FromEmailAddress"/></term> /// <description>When it is <see langword="null"/> or <see cref="System.String.Empty"/> or is an invalid email address.</description> /// </item> /// <item> /// <term><see cref="Services.EmailService.MessageContracts.SendEmailRequest.Recipients"/></term> /// <description>When it is <see langword="null"/> or empty or contains one or more invalid email addresses.</description> /// </item> /// <item> /// <term><see cref="Services.EmailService.MessageContracts.SendEmailRequest.CarbonCopyList"/></term> /// <description>When it contains one or more invalid email addresses.</description> /// </item> /// <item> /// <term><see cref="Services.EmailService.MessageContracts.SendEmailRequest.Attachments"/></term> /// <description>When it contains one or more invalid attachments that is not base 64 encoded or missing file name.</description> /// </item> /// </list> /// </exception> /// <exception cref="System.ServiceModel.FaultException"> /// Throws <see cref="Services.CommonLibraries.Infrastructure.Faults.AuthorizationFault"/> when authorization fails. /// </exception> public SendEmailWithBadAddressCheckResponse SendEmailWithBadAddressCheck(SendEmailRequest request) { SendEmailWithBadAddressCheckResponse response = null; bool result = false; try { Logger.Debug("[SendEmailWithBadAddressCheck] Validating the sessionId."); if (string.Compare(request.SessionId, _sessionToken, StringComparison.InvariantCultureIgnoreCase) != 0) { throw new InvalidSessionIdException(); } Logger.Debug("[SendEmailWithBadAddressCheck] Validating input parameters."); if (!ValidateEmailParameters(request)) { Logger.Error("[SendEmailWithBadAddressCheck] Input request is invalid."); throw new FaultException <EmailServiceFault>(EmailServiceFault.ValidationFailed, ValidationMessage); } Logger.Debug("[SendEmailWithBadAddressCheck] Sending the email."); InvalidRecipients InvalidRecipients = ValidateEmailAddress(request); //removal of Recipients from the list who have bad domains in address and mail is getting triggered to recepeints with valid domain address request.Recipients.RemoveAll(x => InvalidRecipients.BadDomains.Contains(x)); if (request.CarbonCopyList != null) { request.CarbonCopyList.RemoveAll(x => InvalidRecipients.BadDomains.Contains(x)); } if (request.Recipients != null && request.Recipients.Any()) { result = _emailServiceProvider.SendEmail(request.Subject, request.Body, request.FromEmailAddress, request.Recipients, request.CarbonCopyList, request.Attachments); } if (result) { Logger.InfoFormat("[SendEmailWithBadAddressCheck] Email was sent successfully. Result :[{0}]", result); } else { Logger.InfoFormat("[SendEmailWithBadAddressCheck] Email could not send .Please check logs for more info Result :[{0}]", result); } //List of bad domain and bad address is added to response with IsSuccess flag response = new SendEmailWithBadAddressCheckResponse { IsSuccess = result, InvalidRecipients = InvalidRecipients }; } catch (Exception ex) { Logger.FatalFormat("[SendEmailWithBadAddressCheck] Unexpected exception. Message:{0}. Stacktrace:{1}", ex, ex.Message, ex.StackTrace); throw GetFaultException(request, ex); } return(response); }