Ejemplo n.º 1
0
        public async Task <FartakActionResult <FartakEmailModel> > SendToAdminAsync(string body, string subject = "")
        {
            var model = new FartakEmailModel
            {
                Body          = body,
                Subject       = string.IsNullOrWhiteSpace(subject) ? "ارسال پیام هشدار از فرتاک" : subject,
                EmailAddresse = new FartakMailAddress
                {
                    Name    = "HRShojaie",
                    Address = _smtpConfig.AdminAddress
                }
            };

            return(await SendAsync(model));
        }
Ejemplo n.º 2
0
        public async Task <FartakActionResult <FartakEmailModel> > SendAsync(string subject, string body, string toName,
                                                                             string toAddress)
        {
            var model = new FartakEmailModel
            {
                Body          = body,
                Subject       = subject,
                EmailAddresse = new FartakMailAddress
                {
                    Name    = toName,
                    Address = toAddress
                }
            };

            return(await SendAsync(model));
        }
Ejemplo n.º 3
0
        public async Task <FartakActionResult <FartakEmailModel> > SendAsync(FartakEmailModel model)
        {
            using var client = new SmtpClient();
            if (!string.IsNullOrWhiteSpace(_smtpConfig.LocalDomain))
            {
                client.LocalDomain = _smtpConfig.LocalDomain;
            }

            try
            {
                await client.ConnectAsync(_smtpConfig.Server, _smtpConfig.Port, SecureSocketOptions.None);
            }
            catch (Exception e)
            {
                model.Sent = false;
                return(FartakActionResult <FartakEmailModel> .Failed(new FartakResultError
                {
                    Description = e.ToString()
                }));
            }
            if (!string.IsNullOrWhiteSpace(_smtpConfig.Username) &&
                !string.IsNullOrWhiteSpace(_smtpConfig.Password))
            {
                await client.AuthenticateAsync(_smtpConfig.Username, _smtpConfig.Password);
            }

            var message = PrepairEmailMessage(model.BlindCarbonCopies, model.CarbonCopies, model.Subject,
                                              model.Body, model.EmailAddresse.Name, model.EmailAddresse.Address, model.Headers);

            try
            {
                await client.SendAsync(message);

                model.Sent    = true;
                model.Message = $"Email sent successfuly on {DateTimeOffset.UtcNow}";
                return(FartakActionResult <FartakEmailModel> .Invoke(model));
            }
            catch (Exception e)
            {
                model.Sent    = false;
                model.Message = e.ToString();
                return(FartakActionResult <FartakEmailModel> .Failed());
            }
        }