Exemple #1
0
        /// <summary>
        /// Sends the email.
        /// </summary>
        /// <param name="to">To.</param>
        /// <param name="from">From.</param>
        /// <param name="cc">The cc.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="message">The message.</param>
        /// <param name="isHtml">if set to <c>true</c> [is HTML].</param>
        /// <param name="processName">Name of the process.</param>
        /// <param name="itemId">The item identifier.</param>
        public void SendEmail(string to, string @from, string cc, string subject, string message, bool isHtml, string processName, int itemId)
        {
            var email = new EmailEntity { From = @from, Subject = subject, Body = message, IsHtml = true };

            email.ToList.Add(to);
            email.CCList.Add(cc);

            SendEmail(email, processName, itemId, null);
        }
Exemple #2
0
        /// <summary>
        /// Sends the email.
        /// </summary>
        /// <param name="to">
        /// To.
        /// </param>
        /// <param name="from">
        /// From.
        /// </param>
        /// <param name="cc">
        /// The cc.
        /// </param>
        /// <param name="subject">
        /// The subject.
        /// </param>
        /// <param name="message">
        /// The message.
        /// </param>
        /// <param name="isHtml">
        /// if set to <c>true</c> [is HTML].
        /// </param>
        /// <param name="linkedResources">
        /// The linked resources.
        /// </param>
        /// <param name="processName">
        /// Name of the process.
        /// </param>
        /// <param name="itemId">
        /// The item identifier.
        /// </param>
        public void SendEmail(string to, string @from, string cc, string subject, string message, bool isHtml, IEnumerable<EmailLinkedResource> linkedResources, string processName, int itemId)
        {
            var email = new EmailEntity { From = @from, Subject = subject, Body = message, IsHtml = true };

            email.ToList.Add(to);
            email.CCList.Add(cc);
            email.LinkedResources.AddRange(linkedResources);

            SendEmail(email, processName, itemId, null);
        }
Exemple #3
0
        private static MailMessage CreateMailMessage(EmailEntity email, ISystemOptionsInfo systemOptions)
        {
            MailMessage mailMessage = null;
            AlternateView bodyView = null;

            try
            {
                mailMessage = new MailMessage();
                mailMessage.From = !string.IsNullOrEmpty(email.From) ? new MailAddress(email.From) : new MailAddress(systemOptions.EmailFromAddress);

                foreach (var address in email.ToList)
                    mailMessage.To.Add(address);

                foreach (var address in email.CCList.Where(address => !string.IsNullOrEmpty(address)))
                    mailMessage.CC.Add(address);

                foreach (var address in email.BCCList)
                    mailMessage.Bcc.Add(address);

                mailMessage.SubjectEncoding = Encoding.UTF8;
                mailMessage.Subject = email.Subject;

                mailMessage.BodyEncoding = Encoding.UTF8;
                bodyView = AlternateView.CreateAlternateViewFromString(email.Body, Encoding.UTF8, email.IsHtml ? MediaTypeNames.Text.Html : MediaTypeNames.Text.Plain);
                bodyView.TransferEncoding = TransferEncoding.SevenBit;

                mailMessage.IsBodyHtml = email.IsHtml;

                bodyView.LinkedResources.AddRange(email.LinkedResources.Select(CreateLinkedResource));
                mailMessage.AlternateViews.Add(bodyView);

                foreach (var attachment in email.AttachmentPaths)
                    mailMessage.Attachments.Add(new Attachment(attachment));

                return mailMessage;
            }
            catch (Exception)
            {
                if (bodyView != null)
                    bodyView.Dispose();

                if (mailMessage != null)
                    mailMessage.Dispose();

                throw;
            }
        }
Exemple #4
0
        /// <summary> Sends an email asynchronously. </summary>
        /// <param name="email"> The email. </param>
        /// <param name="processName"> The process name. </param>
        /// <param name="itemId"> The item id. </param>
        /// <param name="sender">Action or User</param>
        /// <param name="sysOptions"> The system options. </param>
        /// <returns> The asynchronous task. </returns>
        /// <exception cref="ArgumentNullException">
        /// The <paramref name="email"/> parameter is null.
        /// </exception>
        public async Task SendEmailAsync(EmailEntity email, string processName, int itemId, string sender, ISystemOptionsInfo sysOptions)
        {
            if (email == null)
                throw new ArgumentNullException("email");

            var systemOptions = sysOptions ?? SystemOptionsInfo.GetSystemOptionsInfo();
            if (systemOptions == null)
                throw new SystemOptionsLoadException("Couldn't load system options.");

            if (string.IsNullOrWhiteSpace(systemOptions.EmailFromAddress) || string.IsNullOrWhiteSpace(systemOptions.SMTPServer))
                throw new InvalidSystemOptionsException("Email System Options must be set in the Control Panel!");

            using (var mailMessage = CreateMailMessage(email, systemOptions))
            {
                using (var smtpClient = new SmtpClient { Host = systemOptions.SMTPServer, EnableSsl = systemOptions.SMTPUseSsl, DeliveryMethod = SmtpDeliveryMethod.Network })
                {
                    if (systemOptions.SMTPPort > 0)
                        smtpClient.Port = systemOptions.SMTPPort;

                    smtpClient.UseDefaultCredentials = !systemOptions.SMTPUseAuth;
                    if (systemOptions.SMTPUseAuth)
                        smtpClient.Credentials = new NetworkCredential(systemOptions.SMTPAuthName, systemOptions.SMTPAuthPass);
                    smtpClient.Timeout = 100000;

                    await smtpClient.SendMailAsync(mailMessage);

                    CreateAuditRecord(processName, itemId, string.Join(";", email.ToList), mailMessage.Subject, sender);
                }
            }
        }
Exemple #5
0
        /// <summary> Sends an email asynchronously. </summary>
        /// <param name="to"> To address. </param>
        /// <param name="from"> From address. </param>
        /// <param name="cc"> CC address. </param>
        /// <param name="subject"> The subject. </param>
        /// <param name="message"> The message. </param>
        /// <param name="isHtml"> The is html. </param>
        /// <param name="processName"> The process name. </param>
        /// <param name="itemId"> The item id. </param>
        /// <param name="sender">Action or User</param>
        /// <param name="sysOptions"> The system options. </param>
        /// <returns> The asynchronous task. </returns>
        public async Task SendEmailAsync(
            string to,
            string @from,
            string cc,
            string subject,
            string message,
            bool isHtml,
            string processName,
            int itemId,
            string sender,
            ISystemOptionsInfo sysOptions)
        {
            var email = new EmailEntity { From = @from, Subject = subject, Body = message, IsHtml = true };

            email.ToList.Add(to);
            email.CCList.Add(cc);

            await SendEmailAsync(email, processName, itemId, sender, sysOptions);
        }
Exemple #6
0
        public void SendEmail(EmailEntity email, string processName, int itemId, ISystemOptionsInfo sysOptions)
        {
            if (email == null)
                return;

            var systemOptions = sysOptions ?? SystemOptionsInfo.GetSystemOptionsInfo();

            if (systemOptions == null)
                throw new VeyronException("System Options not loaded!!!");

            if (string.IsNullOrWhiteSpace(systemOptions.EmailFromAddress) || string.IsNullOrWhiteSpace(systemOptions.SMTPServer))
                throw new VeyronException("Email System Options must be set in the Control Panel!");


            using (var mailMessage = CreateMailMessage(email, systemOptions))
            {
                using (var smtpClient = new SmtpClient { Host = systemOptions.SMTPServer, EnableSsl = systemOptions.SMTPUseSsl, DeliveryMethod = SmtpDeliveryMethod.Network })
                {
                    if (systemOptions.SMTPPort > 0)
                        smtpClient.Port = systemOptions.SMTPPort;

                    smtpClient.UseDefaultCredentials = !systemOptions.SMTPUseAuth;
                    if (systemOptions.SMTPUseAuth)
                        smtpClient.Credentials = new NetworkCredential(systemOptions.SMTPAuthName, systemOptions.SMTPAuthPass);
                    smtpClient.Timeout = 100000;

                    smtpClient.Send(mailMessage);

                    CreateAuditRecord(processName, itemId, string.Join(";", email.ToList), mailMessage.Subject, string.Empty);
                }
            }
        }
Exemple #7
0
        /// <summary>
        /// Sends the email.
        /// </summary>
        /// <param name="to">To.</param>
        /// <param name="from">From.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="message">The message.</param>
        /// <param name="isHtml">if set to <c>true</c> [is HTML].</param>
        /// <param name="processName">Name of the process.</param>
        /// <param name="itemId">The item identifier.</param>
        public void SendEmail(IEnumerable<string> to, string @from, string subject, string message, bool isHtml, string processName, int itemId)
        {
            var email = new EmailEntity { From = @from, Subject = subject, Body = message, IsHtml = isHtml };

            if (to != null)
                foreach (var t in to)
                    email.ToList.Add(t);

            SendEmail(email, processName, itemId, null);
        }