Esempio n. 1
0
        public bool SendBySender(int?applicationId = null, DateTime?sendAfter = null)
        {
            EmailQueue item = new EmailQueue();

            item.ApplicationId   = applicationId ?? e.Applications.Single(a => a.IsSystem).Id;
            item.Message         = HermesUtils.SerializeMailMessage(mail, Formatting.Indented);
            item.Date_Send_After = sendAfter == null ? DateTime.UtcNow : (DateTime)sendAfter;
            item.Date_Inserted   = DateTime.UtcNow;
            item.AttachmentList  = attachmentList.ToString();
            item.Status          = EmailQueueStatus.waiting;

            e.EmailQueueItems.Add(item);
            e.SaveChanges();

            return(true);
        }
Esempio n. 2
0
        public bool SendMail(Application application = null, bool disposeClient = true)
        {
            if (mail.To.Count + mail.Bcc.Count == 0)
            {
                return(true);
            }
            bool   result;
            string smtpError = "";

            if (attachmentList.Count() > 0)
            {
                mail.Attachments.Clear();
                for (int i = 0; i < attachmentList.Count(); i++)
                {
                    Attachment att;
                    if (!attachmentList[i].GetType().Equals(typeof(JValue)))
                    {
                        FileMetadata fileInfo = e.FileMetadataRecords.Find((int)attachmentList[i]["Key"]);
                        try {
                            byte[] data        = fileInfo.CachedCopy.Blob;
                            Stream fileContent = new MemoryStream(data);

                            att = new Attachment(fileContent, fileInfo.Filename);
                        }
                        catch (NullReferenceException ex)
                        {
                            OmniusException.Log($"Odeslání e-mailu se nezdařilo - příloha <b>{attachmentList[i]["Value"].ToString()}</b> nebyla nalezena", OmniusLogSource.Hermes, ex, application);
                            return(false);
                        }
                    }
                    else
                    {
                        string path = attachmentList[i].ToString();
                        att = new Attachment(path);
                    }
                    mail.Attachments.Add(att);
                }
            }

            try {
                client.Send(mail);
                result = true;
            }
            catch (Exception e)
            {
                result    = false;
                smtpError = e.Message;
            }

            // Uložíme do logu
            EmailLog log = new EmailLog();

            log.Content    = HermesUtils.SerializeMailMessage(mail, Formatting.Indented);
            log.DateSend   = DateTime.UtcNow;
            log.Status     = result ? EmailSendStatus.success : EmailSendStatus.failed;
            log.SMTP_Error = smtpError;

            e.EmailLogItems.Add(log);
            e.SaveChanges();

            OmniusLog.Log($"Odeslání e-mailu \"{mail.Subject}\" (<a href=\"/Hermes/Log/Detail/{log.Id}\" title=\"Detail e-mailu\">detail e-mailu</a>)", (result ? OmniusLogLevel.Info : OmniusLogLevel.Error), OmniusLogSource.Hermes, application);

            if (disposeClient)
            {
                client.Dispose();
            }

            return(result);
        }