Exemplo n.º 1
0
        private async Task <EmailProtoType> CreateEmail(int companyid, CompanyUser user, MassEmail em)
        {
            EmailProtoType res = new EmailProtoType();

            try
            {
                var model = await CreateEmailModel(companyid, user, em);

                res.JustAttachment = model.JustAttachment;
                if (!res.JustAttachment)
                {
                    string viewname = $"/Views/MassEmail/{em.TemplateName}_Template.cshtml";
                    res.Message = await _razorViewToStringRenderer.RenderViewToStringAsync(viewname, model);
                }
                model.Attachments.ForEach(a => res.Attachments.Add(a));
                if (string.IsNullOrEmpty(model.Subject))
                {
                    res.Subject = string.IsNullOrEmpty(em.Subject) ? "Info" : em.Subject;
                }
                else
                {
                    res.Subject = model.Subject;
                }
                // var user = _userManager.Users.SingleOrDefault(u => u.Id == userid);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "CreateEmail error");
                return(null);
            }
            return(res);
        }
Exemplo n.º 2
0
        public async Task <bool> SendMassEmailToUser(int companyid, CompanyUser user, MassEmail em)
        {
            try
            {
                //to do AK send parents email

                EmailProtoType proto = await CreateEmail(companyid, user, em);

                if (user.IsChild())
                {
                    var email = _context.Users.Where(x => x.Id == user.ParentUserId).FirstOrDefault();
                    if (email.ConfirmedByAdmin)
                    {
                        EmailValidCheck(email.Email);
                        await _mailservice.SendEmailAsync(email.Email, proto.Subject, proto.Message, companyid);
                    }
                }
                else
                {
                    var email = _context.Users.Where(x => x.Id == user.Id).FirstOrDefault();
                    if (email.ConfirmedByAdmin)
                    {
                        EmailValidCheck(email.Email);
                        await _mailservice.SendEmailAsync(email.Email, proto.Subject, proto.Message, companyid, proto.Attachments);
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "SendMassEmailToUser error");
                return(false);
            }
            return(true);
        }