Beispiel #1
0
        public string Send(UtilEmail utilEmail)
        {
            SmtpEmailConfig emailConfig = new SmtpEmailConfig();
            string          message     = string.Empty;

            using (MailMessage mailMessage = new MailMessage(utilEmail.Email, utilEmail.To))
            {
                mailMessage.Subject = utilEmail.Subject;
                mailMessage.Body    = utilEmail.Body;
                if (utilEmail.Attachment.Length > 0)
                {
                    string fileName = Path.GetFileName(utilEmail.Attachment.FileName);
                    mailMessage.Attachments.Add(new Attachment(utilEmail.Attachment.OpenReadStream(), fileName));
                }
                mailMessage.IsBodyHtml = utilEmail.IsBodyHtml;
                using (SmtpClient smtp = new SmtpClient())
                {
                    smtp.Host      = emailConfig.Host;
                    smtp.EnableSsl = emailConfig.EnableSsl;
                    NetworkCredential NetworkCred = new NetworkCredential(utilEmail.Email, utilEmail.Password);
                    smtp.UseDefaultCredentials = emailConfig.UseDefaultCredentials;
                    smtp.Credentials           = NetworkCred;
                    smtp.Port = emailConfig.Port;
                    smtp.Send(mailMessage);
                    message = "Email sent.";
                }
            }
            return(message);
        }
Beispiel #2
0
    public SmtpEmailService(
        ILogger <SmtpEmailService> log,
        IOptions <SmtpEmailConfig> config)
    {
        if (config == null)
        {
            throw new ArgumentNullException(nameof(config));
        }

        _config = config.Value;
        _log    = log ?? throw new ArgumentNullException(nameof(log));
    }