Exemplo n.º 1
0
        /// <summary>
        /// Queues an email message to be sent in the background.
        /// </summary>
        public static long AddToQueue(MailMessage email, long lastUpdatedUserId)
        {
            using (var transactionScope = new TransactionScope())
            {
                var InfEmailId = InfEmailDataAccess.SaveEmail(
                    email.From.ToString(),
                    email.To.ToString(),
                    email.CC.ToString(),
                    email.Bcc.ToString(),
                    email.Priority.ToString(),
                    email.Subject,
                    email.Body,
                    email.IsBodyHtml,
                    lastUpdatedUserId);

                foreach (var attachment in email.Attachments)
                {
                    InfEmailDataAccess.SaveEmailAttachment(
                        InfEmailId,
                        attachment.Name,
                        attachment.ContentStream.ReadAllBytes(),
                        lastUpdatedUserId);
                }

                transactionScope.Complete();

                if (AutoStartQueue)
                {
                    StartQueue();
                }

                return(InfEmailId);
            }
        }
Exemplo n.º 2
0
        public static void AddToQueue(string templateName, IDictionary <string, object> tokens, InfTokenOptions tokenOptions, IDictionary <string, byte[]> attachments, long lastUpdatedUserId)
        {
            var emailTemplateRow = InfEmailDataAccess.GetEmailTemplateByName(templateName);

            if (emailTemplateRow == null)
            {
                throw new ArgumentException("Email template not found.", "templateName");
            }

            var fromAddress = emailTemplateRow["FromAddress"].ToString().ReplaceTokens(tokens, tokenOptions);
            var toAddress   = emailTemplateRow["ToAddress"].ToString().ReplaceTokens(tokens, tokenOptions);
            var ccAddress   = emailTemplateRow["CcAddress"].ToString().ReplaceTokens(tokens, tokenOptions);
            var bccAddress  = emailTemplateRow["BccAddress"].ToString().ReplaceTokens(tokens, tokenOptions);
            var subject     = emailTemplateRow["Subject"].ToString().ReplaceTokens(tokens, tokenOptions);
            var body        = emailTemplateRow["Body"].ToString().ReplaceTokens(tokens, tokenOptions);
            var priority    = emailTemplateRow["Priority"].ToString();
            var htmlInd     = emailTemplateRow["HtmlInd"].ToString().StartsWith("Y", StringComparison.OrdinalIgnoreCase);

            using (var email = new MailMessage(fromAddress, toAddress, subject, body))
            {
                email.IsBodyHtml = htmlInd;

                if (!string.IsNullOrWhiteSpace(ccAddress))
                {
                    email.CC.Add(ccAddress);
                }

                if (!string.IsNullOrWhiteSpace(bccAddress))
                {
                    email.Bcc.Add(bccAddress);
                }

                if (!string.IsNullOrWhiteSpace(priority))
                {
                    email.Priority = (MailPriority)Enum.Parse(typeof(MailPriority), priority, true);
                }

                if (attachments != null)
                {
                    foreach (var item in attachments)
                    {
                        var attachmentName  = item.Key;
                        var attachmentBytes = item.Value;

                        using (var memoryStream = new MemoryStream(attachmentBytes))
                        {
                            using (var attachment = new Attachment(memoryStream, attachmentName))
                            {
                                email.Attachments.Add(attachment);
                            }
                        }
                    }
                }

                AddToQueue(email, lastUpdatedUserId);
            }
        }
Exemplo n.º 3
0
        private static void SendQueuedEmails()
        {
            using (var emailQueue = InfEmailDataAccess.GetQueuedEmails(MaxRetryAttempts))
            {
                foreach (DataRow emailRow in emailQueue.Rows)
                {
                    var infEmailId = (long)emailRow["InfEmailId"];
                    var from       = emailRow["FromAddress"].ToString();
                    var to         = emailRow["ToAddress"].ToString().Replace(';', ',');
                    var cc         = emailRow["CcAddress"].ToString().Replace(';', ',');
                    var bcc        = emailRow["BccAddress"].ToString().Replace(';', ',');
                    var subject    = emailRow["Subject"].ToString();
                    var body       = emailRow["Body"].ToString();
                    var priority   = emailRow["Priority"].ToString();
                    var htmlInd    = emailRow["HtmlInd"].ToString().StartsWith("Y", StringComparison.OrdinalIgnoreCase);

                    using (var email = new MailMessage(from, to, subject, body))
                    {
                        email.IsBodyHtml = htmlInd;

                        if (!string.IsNullOrWhiteSpace(cc))
                        {
                            email.CC.Add(cc);
                        }

                        if (!string.IsNullOrWhiteSpace(bcc))
                        {
                            email.Bcc.Add(bcc);
                        }

                        if (!string.IsNullOrWhiteSpace(priority))
                        {
                            email.Priority = (MailPriority)Enum.Parse(typeof(MailPriority), priority, true);
                        }

                        using (var attachmentsTable = InfEmailDataAccess.GetAttachmentsForEmail(infEmailId))
                        {
                            foreach (DataRow attachmentRow in attachmentsTable.Rows)
                            {
                                var attachmentName  = attachmentRow["AttachmentName"].ToString();
                                var attachmentBytes = attachmentRow["AttachmentBytes"] as byte[];

                                using (var memoryStream = new MemoryStream(attachmentBytes))
                                {
                                    using (var attachment = new Attachment(memoryStream, attachmentName))
                                    {
                                        email.Attachments.Add(attachment);
                                    }
                                }
                            }
                        }

                        try
                        {
                            Send(email);
                            InfEmailDataAccess.SaveSuccessStatus(infEmailId);
                        }
                        catch (Exception ex)
                        {
                            var baseException = ex.GetBaseException();
                            InfLogger.Log(baseException);

                            var lastError = string.Format(CultureInfo.InvariantCulture, "{0}: {1}", baseException.GetType().FullName, baseException.Message);
                            InfEmailDataAccess.SaveErrorStatus(infEmailId, lastError);
                        }
                    }
                }
            }
        }