/// <summary>
        /// Retrieves attachments for an EmailQueueItem
        /// </summary>
        /// <param name="item">The item to get attachments for</param>
        /// <returns></returns>
        public Attachment[] GetAttachments(EmailQueueItem item)
        {
            if (item == null) throw new ArgumentNullException("item");

            TroutLog.Log.Info(string.Format("Retrieving attachments for email {0}", item.ID));

            List<Attachment> attachments = new List<Attachment>();

            if (!Directory.Exists(GetAttachmentDirectory(item))) return attachments.ToArray();

            var attachmentDirectories = Directory.EnumerateDirectories(GetAttachmentDirectory(item));

            foreach (var attachmentDirectory in attachmentDirectories)
            {
                var files = Directory.EnumerateFiles(attachmentDirectory);

                if(files.Any())
                {
                    attachments.Add(new Attachment(files.First()));
                }
            }

            TroutLog.Log.Info(string.Format("Retrieved {0} attachments for email {1}",attachments.Count, item.ID));

            return attachments.ToArray();
        }
        /// <summary>
        /// Saves attachments on the provided MailMessage based on values in the EmailQueueItem
        /// </summary>
        /// <param name="item">EmailQueueItem that attachments are to be saved on</param>
        /// <param name="mailMessage">MailMessage which contains attachments</param>
        public void SaveAttachments(EmailQueueItem item, MailMessage mailMessage)
        {
            if(item == null) throw new ArgumentNullException("item");
            if (mailMessage == null) throw new ArgumentNullException("mailMessage");

            TroutLog.Log.Info(string.Format("Saving {0} attachments for email {1}", mailMessage.Attachments.Count, item.ID));

            for (int i = 0; i < mailMessage.Attachments.Count; i++)
            {
                var attachment = mailMessage.Attachments[i];

                Directory.CreateDirectory(GetAttachmentDirectory(item, i));

                using(FileStream file = new FileStream(GetAttachmentFileName(item, i, attachment), FileMode.OpenOrCreate, FileAccess.ReadWrite))
                {
                    byte[] buffer = new byte[attachment.ContentStream.Length];

                    attachment.ContentStream.Read(buffer, 0, buffer.Length);

                    file.Write(buffer, 0, buffer.Length);
                }
            }

            TroutLog.Log.Info(string.Format("Saved {0} attachments for email {1}", mailMessage.Attachments.Count, item.ID));
        }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="emailQueueItem"></param>
 /// <param name="isSuccess"></param>
 /// <param name="message"></param>
 /// <param name="sentMailMessage"></param>
 /// <param name="tries"></param>
 public DequeueResultItem(EmailQueueItem emailQueueItem, bool isSuccess, string message, MailMessage sentMailMessage, int tries)
 {
     EmailQueueItem = emailQueueItem;
     Tries = tries;
     SentMailMessage = sentMailMessage;
     Message = message;
     IsSuccess = isSuccess;
 }
Пример #4
0
        protected override bool ParseArguments(string[] args)
        {
            OptionSet optionSet = new OptionSet().Add("id=", (int v) => EmailID = v);

            try
            {
                optionSet.Parse(args);
            }
            catch (OptionException)
            {
                Console.WriteLine("Error");
                return false;
            }
            
            Email = Repository.First(e => e.ID == EmailID);

            return Email != null;
        }
        /// <summary>
        /// Enqueues a set of MailMessages to be sent
        /// </summary>
        /// <param name="messages"></param>
        public void EnqueueMessages(IEnumerable<MailMessage> messages)
        {
            if (messages == null) throw new ArgumentNullException("messages");

            TroutLog.Log.Info(string.Format("Beginning queuing of {0} messages", messages.Count()));

            List<Tuple<EmailQueueItem, MailMessage>> createdMessages = new List<Tuple<EmailQueueItem, MailMessage>>();

            foreach (var message in messages.Where(m => m != null))
            {
                var emailQueueItem = new EmailQueueItem()
                                         {
                                             To = message.To.ToString(),
                                             Cc = message.CC.ToString(),
                                             Bcc = message.Bcc.ToString(),
                                             Subject = message.Subject,
                                             Body = message.Body,
                                             CreateDate = DateTime.Now,
                                             IsSent = false,
                                             NumberTries = 0,
                                             LastTryDate = null,
                                             SendDate = null,
                                             IsBodyHtml = true,
                                             AttachmentCount = (byte)message.Attachments.Count
                                         };
                
                createdMessages.Add(new Tuple<EmailQueueItem, MailMessage>(emailQueueItem, message));
                
                Repository.Add(emailQueueItem);
            }

            Repository.SaveChanges();

            foreach (var createdMessage in createdMessages)
            {
                if (createdMessage.Item1.AttachmentCount > 0)
                {
                    AttachmentFileSystem.SaveAttachments(createdMessage.Item1, createdMessage.Item2);
                }

                TroutLog.Log.Info(string.Format("{0} was queued", createdMessage.Item1.ID));
            }

        }
 private string GetAttachmentFileName(EmailQueueItem item, int attachmentIndex, Attachment attachment)
 {
     return string.Format("{0}\\{1}",
                          GetAttachmentDirectory(item, attachmentIndex),
                          attachment.Name);
 }
 private string GetAttachmentDirectory(EmailQueueItem item, int attachmentIndex)
 {
     return string.Format("{0}\\{1}a\\",
                          GetAttachmentDirectory(item),
                          attachmentIndex);
 }
 private string GetAttachmentDirectory(EmailQueueItem item)
 {
     return string.Format("{0}\\{1}t\\{2}t\\{3}\\",
                          Config.AttachmentPath,
                          (item.ID/100000).ToString("000\0\0"), //123,456,789 -> 123400
                          (item.ID/1000)%100, //123,456,789 -> 56
                          item.ID);
 }
        private void PurgeAttachments(EmailQueueItem item)
        {
            if (!Directory.Exists(GetAttachmentDirectory(item))) return;

            TroutLog.Log.Info(string.Format("Purging attachments for email {0} attachments", item.ID));

            Directory.Delete(GetAttachmentDirectory(item), true);
        }
        private MailMessage GetMailMessage(EmailQueueItem message, OverrideList overrides)
        {
            var mailMessage = new MailMessage();
            mailMessage.From = Config.FromAddress;
            if (!string.IsNullOrWhiteSpace(message.To)) mailMessage.To.Add(message.To);
            if (!string.IsNullOrWhiteSpace(message.Cc)) mailMessage.CC.Add(message.Cc);
            if (!string.IsNullOrWhiteSpace(message.Bcc)) mailMessage.Bcc.Add(message.Bcc);
            mailMessage.Subject = message.Subject;
            mailMessage.Body = message.Body;
            mailMessage.IsBodyHtml = message.IsBodyHtml;

            mailMessage = StaticOverridesesProvider.StaticOverrides.ApplyOverrides(mailMessage);
            mailMessage = overrides.ApplyOverrides(mailMessage);
            return mailMessage;
        }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="emailQueueItem"></param>
 /// <param name="message"></param>
 public DequeueListItem(EmailQueueItem emailQueueItem, MailMessage message)
 {
     EmailQueueItem = emailQueueItem;
     Message = message;
 }
 public Attachment[] GetAttachments(EmailQueueItem item)
 {
     return Attachments.ContainsKey(item) ? Attachments[item] : new Attachment[0];
 }
 public void SaveAttachments(EmailQueueItem item, MailMessage mailMessage)
 {
     Attachments[item] = mailMessage.Attachments.ToArray();
 }