Inheritance: IDisposable
Example #1
0
        private int SendMail(DKIM.SmtpClient smtpClient, string from, string to, string cc, string subject, string listId, string body, bool isHtml, List <Attach> attachs)
        {
            using (var message = new MailMessage())
            {
                message.BodyEncoding = Encoding.UTF8;
                message.IsBodyHtml   = isHtml;
                message.Subject      = subject;
                message.Body         = body;
                message.Sender       = new MailAddress(Config.ReturnPath);
                message.From         = new MailAddress(from);
                message.To.Add(to);
                if (!String.IsNullOrEmpty(listId))
                {
                    message.Headers.Add("List-Id", listId);
                }

                //Send copy if needed
                if (!String.IsNullOrEmpty(cc))
                {
                    message.CC.Add(cc);
                }

                //Attach files if exists
                if (attachs.Count != 0)
                {
                    foreach (var attach in attachs)
                    {
                        message.Attachments.Add(new Attachment(new MemoryStream(attach.Data), attach.Name));
                    }
                }

                //Sign message if available
                DkimSigner      dkimSig;
                DomainKeySigner domainKeySig;

                _dkimSignerCache.TryGetValue(message.From.Address, out dkimSig);
                _domailKeySignerCache.TryGetValue(message.From.Address, out domainKeySig);
                smtpClient.DkimSigner       = dkimSig;
                smtpClient.DomainKeysSigner = domainKeySig;

                if (Config.CoreTest == 0)
                {
                    smtpClient.Send(message);
                }
                else if (Config.CoreTest > 0)
                {
                    Thread.Sleep(Config.CoreTest);
                }
            }

            return((int)SmtpStatusCode.Ok);
        }
Example #2
0
        private DataTable SendEmails(MassMail massMail, IEnumerable<Mail> mails)
        {
            var mailClient = new DKIM.SmtpClient(Config.MailServer, Config.MailPort);
            var resultTable = new DataTable();
            var processedCount = 0;
            var errorCount = 0;

            resultTable.Columns.Add("ID");
            resultTable.Columns.Add("Status");
            resultTable.Columns.Add("SendMoment");

            foreach (var mail in mails)
            {
                var result = 0;
                var attachs = new List<Attach>();

                try
                {
                    if (mail.Model == null)
                        throw new XmlException(String.Format("error when creating model for ID:[{0}]", mail.Id));

                    //Load template
                    var id = mail.TemplateId;
                    var template = _templateCache.GetOrAdd(id, new Lazy<Template>(() => massMail.GetTemplate(id))).Value;
                    var body = Functions.RazorGetText(template.Body, template.Guid, mail.Model);

                    //Parse subject
                    var subject = mail.Subject;
                    if (!mail.StaticSubject)
                        subject = Functions.RazorGetText(mail.Subject, Functions.GetMd5(mail.Subject), mail.Model);

                    //Get attachments
                    if (mail.HasAttachment)
                        // ReSharper disable once LoopCanBeConvertedToQuery
                        foreach (DataRow attachRow in massMail.GetAttachments(mail.Id).Rows)
                        {
                            var attachId = (long)attachRow["AttachmentID"];
                            attachs.Add(_attachmentCache.GetOrAdd(attachId, new Lazy<Attach>(() => massMail.GetAttachment(attachId))).Value);
                        }

                    result = SendMail(mailClient, mail.From, mail.To, mail.Cc, subject, mail.ListId, body, template.IsHtml, attachs);
                    processedCount++;
                }
                catch (XmlException ex)
                {
                    Logger.Log.Error("Unable to parse xml: {0}", ex.Message);
                    errorCount++;
                    result = (int)MailStatusCode.XMLERROR;
                }
                catch (RazorException ex)
                {
                    Logger.Log.Error("Razor error: {0}", ex.Message);
                    errorCount++;
                    result = (int)MailStatusCode.RAZORERROR;
                }
                catch (SmtpFailedRecipientsException ex)
                {
                    Logger.Log.Error("SMTP exception: {0}", ex.Message);
                    errorCount++;
                    result = (int)ex.StatusCode;
                }
                catch (Exception ex)
                {
                    Logger.Log.Error("Unable to send email: {0}", ex.Message);
                    errorCount++;
                    result = (int)MailStatusCode.FAILED;
                }
                finally
                {
                    resultTable.Rows.Add(new object[] { mail.Id, result, DateTime.Now });
                }
            }
            MessagesSend(processedCount, errorCount);
            mailClient.Dispose();

            return resultTable;
        }
Example #3
0
        private DataTable SendEmails(MassMail massMail, IEnumerable <Mail> mails)
        {
            var mailClient     = new DKIM.SmtpClient(Config.MailServer, Config.MailPort);
            var resultTable    = new DataTable();
            var processedCount = 0;
            var errorCount     = 0;

            resultTable.Columns.Add("ID");
            resultTable.Columns.Add("Status");
            resultTable.Columns.Add("SendMoment");

            foreach (var mail in mails)
            {
                var result  = 0;
                var attachs = new List <Attach>();

                try
                {
                    if (mail.Model == null)
                    {
                        throw new XmlException(String.Format("error when creating model for ID:[{0}]", mail.Id));
                    }

                    //Load template
                    var id       = mail.TemplateId;
                    var template = _templateCache.GetOrAdd(id, new Lazy <Template>(() => massMail.GetTemplate(id))).Value;
                    var body     = Functions.RazorGetText(template.Body, template.Guid, mail.Model);

                    //Parse subject
                    var subject = mail.Subject;
                    if (!mail.StaticSubject)
                    {
                        subject = Functions.RazorGetText(mail.Subject, Functions.GetMd5(mail.Subject), mail.Model);
                    }


                    //Get attachments
                    if (mail.HasAttachment)
                    {
                        // ReSharper disable once LoopCanBeConvertedToQuery
                        foreach (DataRow attachRow in massMail.GetAttachments(mail.Id).Rows)
                        {
                            var attachId = (long)attachRow["AttachmentID"];
                            attachs.Add(_attachmentCache.GetOrAdd(attachId, new Lazy <Attach>(() => massMail.GetAttachment(attachId))).Value);
                        }
                    }

                    result = SendMail(mailClient, mail.From, mail.To, mail.Cc, subject, mail.ListId, body, template.IsHtml, attachs);
                    processedCount++;
                }
                catch (XmlException ex)
                {
                    Logger.Log.Error("Unable to parse xml: {0}", ex.Message);
                    errorCount++;
                    result = (int)MailStatusCode.XMLERROR;
                }
                catch (RazorException ex)
                {
                    Logger.Log.Error("Razor error: {0}", ex.Message);
                    errorCount++;
                    result = (int)MailStatusCode.RAZORERROR;
                }
                catch (SmtpFailedRecipientsException ex)
                {
                    Logger.Log.Error("SMTP exception: {0}", ex.Message);
                    errorCount++;
                    result = (int)ex.StatusCode;
                }
                catch (Exception ex)
                {
                    Logger.Log.Error("Unable to send email: {0}", ex.Message);
                    errorCount++;
                    result = (int)MailStatusCode.FAILED;
                }
                finally
                {
                    resultTable.Rows.Add(new object[] { mail.Id, result, DateTime.Now });
                }
            }
            MessagesSend(processedCount, errorCount);
            mailClient.Dispose();

            return(resultTable);
        }