Exemplo n.º 1
0
        public MailMessageSenderResult Send(IMailMessage msg)
        {
            List <MessageType> DistinctSenders = new List <MessageType>();

            foreach (IMessageRecipient recipient in msg.Recipients)
            {
                if (!DistinctSenders.Contains(recipient.MessageType))
                {
                    DistinctSenders.Add(recipient.MessageType);
                }
            }

            MailMessageSenderResult result = new MailMessageSenderResult();

            foreach (MessageType messageType in DistinctSenders)
            {
                // Create new message with only one type of recipients
                MailMessage FilteredMsg = new MailMessage();
                FilteredMsg.Recipients.AddRange(msg.Recipients[messageType]);
                FilteredMsg.Sender = msg.Sender;
                FilteredMsg.Attachments.AddRange(msg.Attachments);
                FilteredMsg.Body.AddRange(msg.Body);
                FilteredMsg.IsBulk  = msg.IsBulk;
                FilteredMsg.Subject = msg.Subject;

                MailMessageSenderResult senderResult = Transmit(messageType, FilteredMsg);
                result.RecipientResults.AddRange(senderResult.RecipientResults);
            }

            return(result);
        }
Exemplo n.º 2
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);
                }
            }
        }