Ejemplo n.º 1
0
        public async Task <IActionResult> SendEmail([FromBody] RelayMailMessage message)
        {
            List <string> addresses = new List <string>();

            foreach (string id in message.To.Select(x => x.Trim()))
            {
                var account = await _svc.FindByGuidAsync(id);

                if (account == null)
                {
                    account = await _svc.FindByAccountAsync(id);
                }

                if (account != null)
                {
                    string name = account.Properties
                                  .FirstOrDefault(p => p.Key == Identity.Accounts.ClaimTypes.Name)?.Value;

                    addresses.AddRange(account.Properties
                                       .Where(p => p.Key == Identity.Accounts.ClaimTypes.Email)
                                       .Select(p => $"{name} <{p.Value}>")
                                       .ToList()
                                       );
                }
            }

            if (addresses.Count == 0)
            {
                throw new Exception("No valid addresses.");
            }

            var msg = new MailMessage
            {
                To      = String.Join("; ", addresses.ToArray()),
                Cc      = string.Join("; ", message.Cc),
                Bcc     = string.Join("; ", message.Bcc),
                From    = message.From,
                Subject = message.Subject,
            };

            if (message.Body.StartsWith("<!doctype html", true, null))
            {
                msg.Html = message.Body;
            }
            else
            {
                msg.Text = message.Body;
            }

            var response = await _mailer.Send(msg);

            return(Ok());
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> SendUsernameCode([FromBody] Credentials model)
        {
            if (!await _svc.IsTokenUniqueAsync(model.Username))
            {
                return(Ok());
            }

            var opts = new DistributedCacheEntryOptions {
                AbsoluteExpirationRelativeToNow = new TimeSpan(0, 30, 0)
            };

            string key  = $"{User.GetSubjectId()}:{model.Username}:verifycode";
            string code = new Random().Next(123456, 987654).ToString();
            await _cache.SetStringAsync(key, code, opts);

            Logger.LogDebug("generated code: " + code);

            if (_mailer != null)
            {
                await _mailer.Send(new MailMessage
                {
                    To      = model.Username,
                    Subject = "Verification Code",
                    Text    = $"Verification Code: {code}"
                });
            }
            return(Ok());
        }
Ejemplo n.º 3
0
 private async Task SendCode(string email, int code)
 {
     if (_mailer != null && code > 0)
     {
         await _mailer.Send(new MailMessage
         {
             To      = email,
             Subject = "Verification Code",
             Text    = $"Verification Code: {code}"
         });
     }
     Logger.LogDebug("Generated confirmation code: {0}", code);
 }