public async Task <ResponseModel> SendEmailAsync(string to, string subject, string body, bool isHtml = false)
        {
            SendEmailModel sem = new SendEmailModel(
                Guid.Empty,
                to,
                subject,
                body,
                isHtml
                );

            return(await this.DataManager.SendEmail(sem));
        }
        public bool SendEmail(Guid userId, string to, string subject, string body, bool isHtml = false)
        {
            SendEmailModel sem = new SendEmailModel(
                userId,
                to,
                subject,
                body,
                isHtml
                );

            return(SendEmail(sem));
        }
        public async Task <ResponseModel> ProcessTemplate(Guid userId, EmailTemplateId templateId, string to, Dictionary <string, string> parameters, params object[] items)
        {
            if (TryProcessTemplate(out EmailTemplateProcessor p, templateId, parameters, items))
            {
                SendEmailModel sem = new SendEmailModel(userId, to, p.Template.Subject, p.Template.Text);

                return(await this.DataManager.SendEmail(sem));
            }
            else
            {
                return(ResponseModel.Error(ResponseCode.Internal_UnhandledError, string.Join(Environment.NewLine, p.Errors)));
            }
        }
        public bool SendEmergencyEmail(string subject, string body, bool queue = false)
        {
            var info = this.DataManager.Settings.SMTP;

            SendEmailModel sem = new SendEmailModel(
                Guid.Empty,
                info.Alerts,
                subject,
                body,
                false
                );

            if (!queue)
            {
                return(SendEmail(sem));
            }

            //fire and forget
            this.DataManager.SendEmail(sem);
            return(true);
        }
 public async Task <ResponseModel> SendEmail(SendEmailModel m) =>
 await WriteToQueue(QueueName.Outbound_Email, JsonConvert.SerializeObject(m, JSON.SerializationSettings));
        public bool SendEmail(SendEmailModel model)
        {
            var info = this.DataManager.Settings.SMTP;

            try
            {
                model.Attempts++;
                using (System.Net.Mail.SmtpClient sc = new System.Net.Mail.SmtpClient(info.Host, info.Port))
                {
                    sc.Timeout     = info.TimeoutInSeconds;
                    sc.EnableSsl   = true;
                    sc.Credentials = new System.Net.NetworkCredential(info.Username, info.Password);
                    sc.EnableSsl   = info.UseSSL;
                    sc.Send(new MailMessage(info.From, model.To, model.Subj, model.Body)
                    {
                        IsBodyHtml = model.Html
                    });
                    //sc.Timeout = info.Timeout;
                    model.Status = EmailStatus.Sent;
                }
                return(true);
            }
            catch (SmtpFailedRecipientException ex)
            {
                model.Status     = EmailStatus.BadRecipient;
                model.SmtpStatus = ex.StatusCode.ToString();
                model.Error      = ex.Message;
                return(false);
            }
            catch (SmtpException ex)
            {
                switch (ex.StatusCode)
                {
                case SmtpStatusCode.Ok:
                    return(true);

                case SmtpStatusCode.GeneralFailure:
                    model.Status = EmailStatus.HostNotFound;
                    model.Error  = ex.Message;
                    return(false);

                case SmtpStatusCode.MailboxNameNotAllowed:
                case SmtpStatusCode.MailboxUnavailable:
                    model.Status     = EmailStatus.BadRecipient;
                    model.SmtpStatus = ex.StatusCode.ToString();
                    model.Error      = ex.Message;
                    return(false);

                case SmtpStatusCode.ServiceClosingTransmissionChannel:
                case SmtpStatusCode.ServiceNotAvailable:
                    model.Status     = EmailStatus.ServerDown;
                    model.SmtpStatus = ex.StatusCode.ToString();
                    model.Error      = ex.Message;
                    return(false);

                case SmtpStatusCode.MailboxBusy:
                case SmtpStatusCode.InsufficientStorage:
                case SmtpStatusCode.ExceededStorageAllocation:
                    model.Status     = EmailStatus.BadRecipient;
                    model.SmtpStatus = ex.StatusCode.ToString();
                    model.Error      = ex.Message;
                    return(false);

                case SmtpStatusCode.BadCommandSequence:
                case SmtpStatusCode.ClientNotPermitted:
                case SmtpStatusCode.CommandNotImplemented:
                case SmtpStatusCode.CommandParameterNotImplemented:
                case SmtpStatusCode.CommandUnrecognized:
                case SmtpStatusCode.HelpMessage:
                case SmtpStatusCode.LocalErrorInProcessing:
                case SmtpStatusCode.MustIssueStartTlsFirst:
                case SmtpStatusCode.ServiceReady:
                case SmtpStatusCode.StartMailInput:
                case SmtpStatusCode.SyntaxError:
                case SmtpStatusCode.SystemStatus:
                case SmtpStatusCode.TransactionFailed:
                case SmtpStatusCode.UserNotLocalTryAlternatePath:
                case SmtpStatusCode.UserNotLocalWillForward:
                default:
                    model.SmtpStatus = ex.StatusCode.ToString();
                    model.Error      = ex.Message;
                    model.Status     = EmailStatus.SmtpException;
                    return(false);
                }
            }
            catch (Exception ex)
            {
                model.Status = EmailStatus.SystemError;
                model.Error  = ex.Message;
                return(false);
            }
        }