public async Task <JsonResult> SendEmail(Models.Email.Email model)
        {
            // Credentials:
            IEmailer emailer = new Emailer();

            var message = new EmailInfo()
            {
                From     = model.FromEmail,
                FromName = model.FromName,
                Subject  = model.Subject,
                HtmlBody = MarkdownService.GetHtmlFromMarkdown(model.Body),
            };

            message.To.Add(SiteEmail);

            int count = 0;

            if (!model.SendAsTest)
            {
                foreach (var group in model.RecipientGroups)
                {
                    var emails = _gm.GetUserEmailsInGroup(group);

                    var emailTasks = emails.Select(to =>
                    {
                        count++;
                        message.To.Clear();
                        message.To.Add(to);
                        return(emailer.SendEmailAsync(message));
                    });

                    await Task.WhenAll(emailTasks);
                }
                return(Json(count));
            }
            else
            {
                var userId = new Guid(User.Identity.GetUserId());
                message.To.Clear();
                message.To.Add(_profileManager.GetUserProfile(userId).Email);
                await emailer.SendEmailAsync(message);


                return(Json(1));
            }
        }
        // GET: Admin/Email
        public ActionResult Index(int?eventId)
        {
            var model = new Models.Email.Email();

            model.AvailableGroups = _pm.GetAvailableGroups();
            model.RecipientGroups = new List <int>();


            model.FromEmail  = SiteEmail;
            model.SendAsTest = true;
            //if (eventId.HasValue)
            //{
            //    var post = _pm.GetPost(eventId.Value, userId);
            //    model.Body = post.Body;
            //}

            return(View(model));
        }
        // GET: Admin/Email
        public ActionResult Index(int?eventId)
        {
            var model = new Models.Email.Email();

            model.AvailableGroups = _pm.GetAvailableGroups();
            model.RecipientGroups = new List <int>();

            model.FromName   = _um.GetUserFullname(Guid.Parse(User.Identity.GetUserId()));
            model.FromEmail  = _um.GetUserEmail(Guid.Parse(User.Identity.GetUserId()));
            model.SendAsTest = true;
            //if (eventId.HasValue)
            //{
            //    var post = _pm.GetPost(eventId.Value, userId);
            //    model.Body = post.Body;
            //}

            return(View(model));
        }
Example #4
0
        public void SendEmail(Models.Email.Email email)
        {
            try
            {
                log.Debug($"Sending email: {email.Recipients.EmailAddress}");
                using (var smtp = new SmtpClient())
                {
                    smtp.Host        = _appSettings.Host;
                    smtp.Port        = Convert.ToInt32(_appSettings.Port);
                    smtp.Credentials = new System.Net.NetworkCredential(_appSettings.User, _appSettings.Password);
                    smtp.EnableSsl   = true;
                    using (var msg = new MailMessage())
                    {
                        msg.BodyEncoding = Encoding.UTF8;
                        msg.Body         = null;

                        //add html view
                        AlternateView htmlview = AlternateView.CreateAlternateViewFromString(email.Message.MessageBody, msg.BodyEncoding, "text/html");
                        htmlview.TransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable;
                        msg.AlternateViews.Add(htmlview);

                        msg.Subject = email.Message.Subject;
                        msg.From    = new MailAddress(email.Message.ReplyAddress, email.Message.FromName);

                        msg.To.Add(new MailAddress(email.Recipients.EmailAddress));

                        smtp.Send(msg);
                    }
                    log.Debug("mail sent: success");
                }
                //Update recipient
                email.Recipients.Status = "Sent";
            }
            catch (Exception ex)
            {
                log.Error($"Send Email Error:{ex.Message } {ex?.InnerException?.Message}");
                throw;
            }
        }