示例#1
0
        /// <summary>
        /// Sends a mail.
        /// </summary>
        ///
        /// <param name="to">
        /// To field (destinator).
        /// </param>
        ///
        /// <param name="subject">
        /// Mail subject.
        /// </param>
        ///
        /// <param name="message">
        /// Mail message.
        /// </param>
        ///
        /// <param name="options">
        /// SmtpService options (optional).
        /// </param>
        public void Send(string to, string subject, string message, SmtpServiceOptions options = null)
        {
            try
            {
                using (var smtpClient = this.GetSmtpClientInstance())
                    using (var mailMessage = CreateMailMessage(to, subject, message, options))
                    {
                        smtpClient.Send(mailMessage);
                    }
            }
            catch (SmtpException x)
            {
                LoggerServiceHelper.Current.TraceException(this, x);

                throw;
            }
        }
示例#2
0
 /// <summary>
 /// Sends an asynchronous mail.
 /// The 'from' value comes from the configuration file and can be overrided from options.
 /// </summary>
 ///
 /// <param name="to">
 /// To field.
 /// </param>
 ///
 /// <param name="subject">
 /// Mail subject.
 /// </param>
 ///
 /// <param name="message">
 /// Mail message.
 /// </param>
 ///
 /// <param name="options">
 /// Options (optional).
 /// </param>
 public async Task SendAsync(string to, string subject, string message, SmtpServiceOptions options = null)
 {
     await _smtpService.SendAsync(to, subject, message, options);
 }
示例#3
0
        private static MailMessage CreateMailMessage(string to, string subject, string message, SmtpServiceOptions options = null)
        {
            if (options == null)
            {
                options = new SmtpServiceOptions();
            }

            if (string.IsNullOrEmpty(options.From))
            {
                options.From = _config.From;
            }

            if (_config.Redirection != null)
            {
                to = _config.Redirection;

                options.Cc  = null;
                options.Bcc = null;

                subject = string.Format("[REDIRECTION] {0}", subject);
            }
            else
            {
                if (!string.IsNullOrEmpty(_config.Cc))
                {
                    options.Cc = string.Concat(options.Cc, ",", _config.Cc);
                }

                if (!string.IsNullOrEmpty(_config.Bcc))
                {
                    options.Bcc = string.Concat(options.Bcc, ",", _config.Bcc);
                }
            }

            var mailMessage = new MailMessage();

            foreach (var toEntry in ExtractEmails(to))
            {
                mailMessage.To.Add(new MailAddress(toEntry));
            }

            foreach (var ccEntry in ExtractEmails(options.Cc))
            {
                mailMessage.CC.Add(new MailAddress(ccEntry));
            }

            foreach (var bccEntry in ExtractEmails(options.Bcc))
            {
                mailMessage.Bcc.Add(new MailAddress(bccEntry));
            }

            string senderName = (!string.IsNullOrEmpty(options.SenderName)) ? options.SenderName : _config.SenderName;

            mailMessage.From = new MailAddress(options.From, senderName);

            mailMessage.Subject = subject;
            mailMessage.Body    = message;

            mailMessage.IsBodyHtml = options.IsHtmlBody;

            if (options.Files != null)
            {
                foreach (var file in options.Files)
                {
                    mailMessage.Attachments.Add(new Attachment(file, FileHelper.GetMimeType(file)));
                }
            }

            return(mailMessage);
        }
示例#4
0
 /// <summary>
 /// Sends a mail.
 /// The 'from' value comes from the configuration file and can be overrided from options.
 /// </summary>
 ///
 /// <param name="to">
 /// To field.
 /// </param>
 ///
 /// <param name="subject">
 /// Mail subject.
 /// </param>
 ///
 /// <param name="message">
 /// Mail message.
 /// </param>
 ///
 /// <param name="options">
 /// Options (optional).
 /// </param>
 public void Send(string to, string subject, string message, SmtpServiceOptions options = null)
 {
     _smtpService.Send(to, subject, message, options);
 }