Exemple #1
0
 private string GetFormattedAttachmentWarnings()
 {
     if (FileAttachmentWarnings.Count <= 100)
     {
         return(FileAttachmentWarnings.ToLine("<br / >"));
     }
     else
     {
         string warnings = FileAttachmentWarnings.Take(100).ToList().ToLine("<br / >");
         warnings += string.Format("...and {0} more!<br / >", (FileAttachmentWarnings.Count - 100));
         return(warnings);
     }
 }
Exemple #2
0
        public void Send(string body = null, string subject = null, bool noDefaultAttachment = false, List <string> additionalAttachments = null)
        {
            if (!EyediaCoreConfigurationSection.CurrentConfig.Email.Enabled)
            {
                return;
            }

            MailMessage oMail = new MailMessage();

            oMail.IsBodyHtml = true;
            oMail.From       = new MailAddress(EyediaCoreConfigurationSection.CurrentConfig.Email.FromEmail);
            if (EyediaCoreConfigurationSection.CurrentConfig.Email.ToEmails.Contains(";"))
            {
                List <string> toEmails = new List <string>(EyediaCoreConfigurationSection.CurrentConfig.Email.ToEmails.Split(";".ToCharArray()));
                foreach (string toEmail in toEmails)
                {
                    oMail.To.Add(toEmail);
                }
            }
            else
            {
                oMail.To.Add(EyediaCoreConfigurationSection.CurrentConfig.Email.ToEmails);
            }
            AddCcAndBcc(oMail);
            oMail.Subject  = subject == null ? Subject : FormatExternalEmailSubject(subject);
            oMail.Priority = MailPriority.Normal;


            if (Job != null)
            {
                if (noDefaultAttachment == false)
                {
                    if (!(string.IsNullOrEmpty(Job.FileName)) &&
                        (File.Exists(Job.FileName)))
                    {
                        if (GetFileSize(Job.FileName) < EyediaCoreConfigurationSection.CurrentConfig.Email.MaxAttachmentSize)
                        {
                            oMail.Attachments.Add(new Attachment(Job.FileName));
                        }
                        else
                        {
                            FileAttachmentWarnings.Add(string.Format("File '{0}' was not attached to the email as it was greater than allowed size ({1} MB)", Path.GetFileName(Job.FileName), EyediaCoreConfigurationSection.CurrentConfig.Email.MaxAttachmentSize));
                        }
                    }
                }
            }

            if (additionalAttachments != null)
            {
                foreach (string file in additionalAttachments)
                {
                    if (!(string.IsNullOrEmpty(file)) &&
                        (File.Exists(file)))
                    {
                        if (new FileUtility().WaitTillFileIsFree(file) == true)
                        {
                            if (GetFileSize(file) < EyediaCoreConfigurationSection.CurrentConfig.Email.MaxAttachmentSize)
                            {
                                oMail.Attachments.Add(new Attachment(file));
                            }
                            else
                            {
                                FileAttachmentWarnings.Add(string.Format("File '{0}' was not attached to the email as it was greater than allowed size ({1} MB)", Path.GetFileName(file), EyediaCoreConfigurationSection.CurrentConfig.Email.MaxAttachmentSize));
                            }
                        }
                    }
                }
            }


            oMail.Body = body == null ? Body : FormatExternalEmailBody(body);
            if (_EmailTracker.IsAllowed(subject, body, DataSource.Id))
            {
                SmtpClient smtpmail = new SmtpClient(EyediaCoreConfigurationSection.CurrentConfig.Email.SmtpServer);
                if (!string.IsNullOrEmpty(EyediaCoreConfigurationSection.CurrentConfig.Email.UserName))
                {
                    smtpmail.Credentials = new System.Net.NetworkCredential(EyediaCoreConfigurationSection.CurrentConfig.Email.UserName,
                                                                            EyediaCoreConfigurationSection.CurrentConfig.Email.Password);
                }
                else
                {
                    smtpmail.Credentials = (ICredentialsByHost)CredentialCache.DefaultNetworkCredentials;
                }

                smtpmail.EnableSsl = EyediaCoreConfigurationSection.CurrentConfig.Email.EnableSsl;
                //smtpmail.DeliveryMethod = SmtpDeliveryMethod.Network;
                try
                {
                    smtpmail.Send(oMail);
                    if (oMail != null)
                    {
                        oMail.Dispose();
                        oMail = null;
                    }
                }
                catch (Exception ex)
                {
                    Eyedia.Core.Net.PostMan.WriteHtml(oMail);
                }

                _EmailTracker.Track(subject, body, DataSource.Id);
            }
            else
            {
                Trace.TraceInformation("Postman: Repeated email with subject = '{0}', Body = '{1}", subject, body);
            }
        }