예제 #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());
        }
예제 #2
0
        public async Task <IActionResult> SendEmailBatch([FromBody] RelayMailMessage message)
        {
            var list = await _svc.FindAll(
                new SearchModel
            {
                Term = "#enabled"
            }
                );

            // domain subset
            if (
                message.To.First().StartsWith("@") &&
                message.To.First() != "@here"
                )
            {
                list = list
                       .Where(a => a.Properties.Any(p =>
                                                    p.Key == "email" && p.Value.EndsWith(message.To.First()))
                              ).ToArray();
            }

            var results = new List <MailMessageStatus>(list.Length);

            string msgId = Guid.NewGuid().ToString("N");

            foreach (var account in list)
            {
                string name = account.Properties
                              .FirstOrDefault(p => p.Key == Identity.Accounts.ClaimTypes.Name)?.Value;

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

                if (addresses.Count == 0)
                {
                    continue;
                }

                string to = String.Join("; ", addresses.ToArray());

                var msg = new MailMessage
                {
                    To        = to,
                    Cc        = string.Join("; ", message.Cc),
                    Bcc       = string.Join("; ", message.Bcc),
                    From      = message.From,
                    Subject   = message.Subject,
                    MessageId = $"{msgId}-{to}"
                };

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

                var response = await _mailer.Send(msg);

                results.Add(response);
            }

            bool done = false;
            int  pass = 0;

            while (!done && pass < 5)
            {
                await Task.Delay(10000);

                done = true;

                foreach (var item in results.Where(r => r.Status == "pending"))
                {
                    var response = await _mailer.Status(item.ReferenceId);

                    item.Status = response.Status;
                    done       &= item.Status != "pending";
                }

                pass += 1;
            }

            Logger.LogInformation("send mail success: {0}", results.Where(r => r.Status == "success").Count());

            foreach (var item in results.Where(r => r.Status != "success"))
            {
                Logger.LogInformation("send mail fail: {0} {1}", item.Status, item.MessageId);
            }

            return(Ok());
        }