Example #1
0
 public SendMailResult SendMail(SendMailRequest request)
 {
     try
     {
         return(SendMailRequestProcessor.ProcessSendMailRequest(request));
     }
     catch (Exception e)
     {
         SendMailResult result = new SendMailResult();
         result.Status = SendMailResultStatus.Error.ToString();
         result.Messages.Add(e.Message);
         return(result);
     }
 }
        /// <summary>
        /// Process a SendMailRequest, send the email and generate proper response
        /// </summary>
        /// <param name="request">The request containing email details</param>
        /// <returns>The result of the sending email action</returns>
        public static SendMailResult ProcessSendMailRequest(SendMailRequest request)
        {
            SendMailResult result = new SendMailResult();

            var providers = GetOrderedMailServiceProviders(request);

            // if no ApiKeys are found, we can't use any providers
            if (providers.Count == 0)
            {
                result.Status = SendMailResultStatus.Error.ToString();
                result.Messages.Add("No mail service provider available.");
                return(result);
            }

            for (int i = 0; i < providers.Count; i++)
            {
                try
                {
                    string messageId;
                    string detailMessage;

                    bool success = providers[i].SendMail(request, out messageId, out detailMessage);

                    // add message returned by the provider, if any
                    if (!String.IsNullOrWhiteSpace(detailMessage))
                    {
                        result.Messages.Add(detailMessage);
                    }

                    if (success)
                    {
                        result.Status       = SendMailResultStatus.Success.ToString();
                        result.MessageId    = messageId;
                        result.ProviderUsed = providers[i].ProviderType.ToString();
                        return(result);
                    }
                    else
                    {
                        // we don't know why it failed, so continue and try next provider
                        result.Status = SendMailResultStatus.Error.ToString();
                        continue;
                    }
                }
                catch (Exception e)
                {
                    result.Status = SendMailResultStatus.Error.ToString();
                    result.Messages.Add(SendMailRequestProcessor.GetErrorStringFromException(e));

                    if (e is FormatException)
                    {
                        // some of the email address is not correctly formated. stop trying and return the error
                        return(result);
                    }
                    else
                    {
                        // for other exceptions, ingore and try next provider
                        continue;
                    }
                }
            }

            return(result);
        }