Esempio n. 1
0
        protected virtual MailMessageSenderResult Transmit(MessageType messageType, MailMessage FilteredMsg)
        {
            // Create sender for the message type
            IMailMessageSender      sender       = MailMessageSenderFactory.CreateMessageSender(messageType, FilteredMsg.IsBulk);
            MailMessageSenderResult senderResult = sender.Send(FilteredMsg);

            return(senderResult);
        }
        private static void SenderThreadMain()
        {
            for (; ;)
            {
                MessageContainer c = Dequeue();
                if (c != null)
                {
                    // Message is a retry the needs to be sent in the future
                    if (c.SendTime > DateTime.Now)
                    {
                        Enqueue(c);
                        Thread.Sleep(300);
                    }
                    else
                    {
                        try
                        {
                            // Create sender for this type of message
                            IMailMessageSender sender = MailMessageSenderFactory.CreateMessageSender(c.msgType, c.msg.IsBulk);

                            // If sender supports IRetryOnError, turn it off so we can retry it through the queue
                            IRetryOnError senderRetryOnError        = sender as IRetryOnError;
                            bool          senderRetryOnErrorEnabled = false;
                            if (senderRetryOnError != null)
                            {
                                senderRetryOnErrorEnabled             = senderRetryOnError.EnableRetryOnError;
                                senderRetryOnError.EnableRetryOnError = false;
                            }

                            try
                            {
                                // Send message
                                MailMessageSenderResult senderResult = sender.Send(c.msg);

                                // TODO: add logging

                                //Logger.Info("Send " + c.msgType + " message (" + c.msg.Subject + ") to " + c.msg.Recipients.ToString()+" using "+
                                //    sender.GetType().FullName);
                            }
                            catch (Exception e)
                            {
                                // Catch errors and requeue the message to be resent later
                                if (senderRetryOnErrorEnabled)
                                {
                                    c.ErrorCount++;
                                    if (c.ErrorCount < senderRetryOnError.RetryOnErrorCount)
                                    {
                                        c.SendTime = DateTime.Now + new TimeSpan(0, 0, 10);
                                        Enqueue(c);
                                        //Logger.Error("Error sending message. Retry " + (c.ErrorCount - 1) + " of " +
                                        //    senderRetryOnError.RetryOnErrorCount + ".", e);
                                    }
                                    //else
                                    //    Logger.Error("Error sending message after " + senderRetryOnError.RetryOnErrorCount +
                                    //        " retries. Message has been discarded.", e);
                                }
                                //else
                                //    Logger.Error("Error sending message. Message has been discarded (Retry was not available).", e);
                            }
                        }
                        catch (Exception e)
                        {
                            //Logger.Error("Error creating message sender ("+c.msgType+". Message has been discarded.", e);
                        }
                    }
                }
                else
                {
                    Thread.Sleep(500);
                }
            }
        }