public async Task TakeBody_Test() { var body = "TestBody"; var firstEmail = EmailGeneratorUtil.GenerateEmailFirst(); firstEmail.Body = body; var encodeDecodeServiceMock = new Mock <IEncodeDecodeService>().Object; var loggerMock = new Mock <ILogger <EmailService> >().Object; var options = TestUtilities.GetOptions(nameof(TakeBody_Test)); using (var actContext = new E_MailApplicationsManagerContext(options)) { var email = await actContext.Emails.AddAsync(firstEmail); await actContext.SaveChangesAsync(); var dto = new EmailContentDto { GmailId = email.Entity.GmailId }; var sut = new EmailService(actContext, loggerMock, encodeDecodeServiceMock); var result = await sut.TakeBodyAsync(dto); Assert.IsNotNull(result); } }
public async Task <Email> AddBodyToCurrentEmailAsync(EmailContentDto emailDto) { ValidatorEmailService.ValidatorAddBodyToCurrentEmailIfDtoBodyIsNull(emailDto); ValidatorEmailService.ValidatorAddBodyToCurrentEmailBodyLength(emailDto); var encodeBody = this.encodeDecodeService.Base64Decode(emailDto.Body); var email = await this.context.Emails .Include(u => u.User) .Where(gMail => gMail.GmailId == emailDto.GmailId) .SingleOrDefaultAsync(); var currentUser = await this.context.Users .Where(id => id.Id == emailDto.UserId) .SingleOrDefaultAsync(); ValidatorEmailService.ValidatorAddBodyToCurrentEmailExistBody(email, emailDto); var encriptBody = this.encodeDecodeService.Encrypt(encodeBody); if (email.Body == null) { email.Body = encriptBody; email.User = currentUser; email.UserId = emailDto.UserId; email.IsSeen = true; await this.context.SaveChangesAsync(); } return(email); }
public static void ValidatorAddBodyToCurrentEmailExistBody(Email email, EmailContentDto emailDto) { if (email.Body != null) { throw new EmailExeption($"Email with the following id {emailDto.GmailId} contains body"); } }
public async Task ThrowExeptionWhenCheckEmailBodyIsNull_Test() { string body = null; var firstEmail = EmailGeneratorUtil.GenerateEmailFirst(); firstEmail.Body = body; var encodeDecodeServiceMock = new Mock <IEncodeDecodeService>().Object; var loggerMock = new Mock <ILogger <EmailService> >().Object; var options = TestUtilities.GetOptions(nameof(ThrowExeptionWhenCheckEmailBodyIsNull_Test)); using (var actContext = new E_MailApplicationsManagerContext(options)) { var email = await actContext.Emails.AddAsync(firstEmail); await actContext.SaveChangesAsync(); var dto = new EmailContentDto { GmailId = firstEmail.GmailId, }; var sut = new EmailService(actContext, loggerMock, encodeDecodeServiceMock); await sut.CheckEmailBodyAsync(dto); } }
public static void ValidatorAddBodyToCurrentEmailBodyLength(EmailContentDto emailDto) { if (emailDto.Body.Length > 1000) { throw new EmailExeption($"Body of email is to long!"); } }
public static void ValidatorAddBodyToCurrentEmailIfDtoBodyIsNull(EmailContentDto emailDto) { if (emailDto.Body == null) { throw new EmailExeption($"Email with the following id {emailDto.GmailId} does not exist"); } }
public async Task GetAllUserWorkingOnEmail_Test() { var firstEmail = EmailGeneratorUtil.GenerateEmailFirst(); var secondEmail = EmailGeneratorUtil.GenerateEmailSecond(); var userId = secondEmail.UserId; var emailContentDto = new EmailContentDto { UserId = userId }; var options = TestUtilities.GetOptions(nameof(GetAllUserWorkingOnEmail_Test)); using (var actContext = new E_MailApplicationsManagerContext(options)) { await actContext.Emails.AddAsync(firstEmail); await actContext.Emails.AddAsync(secondEmail); await actContext.SaveChangesAsync(); var sut = new SearchService(actContext); var result = await sut.GetAllUserWorkingOnEmailAsync(emailContentDto); Assert.IsNotNull(result); } }
public async Task ThrowExeptionWhenEmailBodyIsToLong_AddBodyToCurrentEmailAsync_Test() { var firstEmail = EmailGeneratorUtil.GenerateEmailFirst(); var body = new String('T', 1001); var encodeDecodeServiceMock = new Mock <IEncodeDecodeService>().Object; var loggerMock = new Mock <ILogger <EmailService> >().Object; var options = TestUtilities.GetOptions(nameof(ThrowExeptionWhenEmailBodyIsToLong_AddBodyToCurrentEmailAsync_Test)); using (var actContext = new E_MailApplicationsManagerContext(options)) { var email = await actContext.Emails.AddAsync(firstEmail); await actContext.SaveChangesAsync(); var emailDto = new EmailContentDto { Body = body, GmailId = firstEmail.GmailId }; var sut = new EmailService(actContext, loggerMock, encodeDecodeServiceMock); await sut.AddBodyToCurrentEmailAsync(emailDto); } }
public async Task <Email> TakeBodyAsync(EmailContentDto emailDto) { var email = await this.context.Emails .Where(gMail => gMail.GmailId == emailDto.GmailId) .SingleOrDefaultAsync(); return(email); }
/// <inheritdoc/> public Task <EmailContentDto> RenderNewApplicationContentAsync(ApplicationDto applicationDto, CancellationToken cancellationToken) { var content = new EmailContentDto() { Subject = "Система жалоб и предложений: Новая жалоба", Body = $"<p>Название: {applicationDto.Name}</p>" + $"<p>Описание: {applicationDto.Description}</p>" }; return(Task.FromResult(content)); }
/// <inheritdoc/> public Task <EmailContentDto> RenderRejectedApplicationContentAsync(ApplicationDto applicationDto, CancellationToken cancellationToken) { var content = new EmailContentDto() { Subject = "Система жалоб и предложений: Жалоба была отменена", Body = $"<p>Название: {applicationDto.Name}</p>" + $"<p>Описание: {applicationDto.Description}</p>" + $"<p>Комментарий: {applicationDto.RejectComments}</p>" }; return(Task.FromResult(content)); }
/// <inheritdoc/> public Task <EmailContentDto> RenderPublishedApplicationContentAsync(ApplicationDto applicationDto, CancellationToken cancellationToken) { var content = new EmailContentDto() { Subject = "Система жалоб и предложений: Ответ на жалобу был опубликован", Body = $"<p>Название: {applicationDto.Name}</p>" + $"<p>Описание: {applicationDto.Description}</p>" + $"<p>Ответ: {applicationDto.Reply.Text}</p>" }; return(Task.FromResult(content)); }
public async Task <IEnumerable <Email> > GetAllUserWorkingOnEmailAsync(EmailContentDto userIdDto) { var userId = await this.context.Users .FirstOrDefaultAsync(uId => uId.Id == userIdDto.UserId); var email = await this.context.Emails .Include(u => u.User) .Where(workingOnEmail => workingOnEmail.UserId == userIdDto.UserId && workingOnEmail.EmailStatusId != (int)EmailStatusesType.Closed) .Select(emails => emails) .ToListAsync(); return(email); }
public async Task <IActionResult> CheckMyEmail() { var baseDto = new EmailContentDto { UserId = User.FindFirstValue(ClaimTypes.NameIdentifier) }; var emails = (await this.searchService.GetAllUserWorkingOnEmailAsync(baseDto)) .Select(email => new EmailViewModel(email)); var results = new SearchEmailViewModel(emails); return(View("CheckMyEmail", results)); }
public static void SendEmailMsg(EmailContentDto emailContentDto) { try { string path = AppDomain.CurrentDomain.BaseDirectory; string body = PopulateBody(emailContentDto); SendHtmlFormattedEmail(emailContentDto.email, "ITBeep", body); } catch (SmtpException ex) { } catch (Exception ex) { } }
public async Task Send(BulkEmailDto dto) { using (var tokenResponse = await HttpHelper.SendRequest(HttpMethod.Post, _smartMail.AuthApiUrl, null, new { username = _smartMail.Email, password = _smartMail.Password })) { var tokenResult = await tokenResponse.Content.ReadAsStringAsync(); var tokenData = JsonConvert.DeserializeObject <SmartMailUserAuthentication>(tokenResult); _token = tokenData.AccessToken; var logs = new List <EmailLog>(); foreach (var item in dto.Mails) { var log = new EmailLog { AppId = dto.AppId, CustomerId = dto.CustomerId, RecipientName = item.RecipientName, Email = item.RecipientEmail, Message = item.Message, Subject = item.Subject, StatusCode = 200 }; try { var messageHTML = generateMailBody(item.RecipientName, item.Message); var content = new EmailContentDto(item.RecipientEmail, item.Subject, messageHTML); using (var mailResponse = await HttpHelper.SendRequest(HttpMethod.Post, _smartMail.SendEmailApiUrl, tokenData.AccessToken, content)) { if (!mailResponse.IsSuccessStatusCode) { log.StatusCode = (int)mailResponse.StatusCode; log.ExceptionMessage = await mailResponse.Content.ReadAsStringAsync(); } } } catch (Exception ex) { log.StatusCode = 400; log.ExceptionMessage = ex.ToString(); } logs.Add(log); } await _repository.Insert(logs); } }
private static string PopulateBody(EmailContentDto emailContentDto) { string path = AppDomain.CurrentDomain.BaseDirectory; string body = string.Empty; using (StreamReader reader = new StreamReader(path + "EmailTemplate.html")) { body = reader.ReadToEnd(); } body = body.Replace("{UserName}", emailContentDto.username); body = body.Replace("{Email}", emailContentDto.email); body = body.Replace("{Mobile}", emailContentDto.mobile); body = body.Replace("{Services}", emailContentDto.serviceType); body = body.Replace("{intersted}", emailContentDto.schedule); return(body); }
public async Task <Email> CheckEmailBodyAsync(EmailContentDto emailDto) { var email = await this.context.Emails .SingleOrDefaultAsync(gMail => gMail.GmailId == emailDto.GmailId); ValidatorEmailService.ValidatorCheckEmailBody(email); var currentUser = await this.context.Users .Where(userId => userId.Id == emailDto.UserId) .Select(user => user.UserName) .SingleOrDefaultAsync(); email.EmailStatusId = (int)EmailStatusesType.New; email.SetCurrentStatus = DateTime.Now; await this.context.SaveChangesAsync(); logger.LogInformation($"Changed email status to New by {currentUser}"); return(email); }
public async Task <int> Send(EmailDto dto) { int statusCode = 400; try { using (var tokenResponse = await HttpHelper.SendRequest(HttpMethod.Post, _smartMail.AuthApiUrl, null, new { username = _smartMail.Email, password = _smartMail.Password })) { if (tokenResponse.IsSuccessStatusCode) { var tokenResult = await tokenResponse.Content.ReadAsStringAsync(); var tokenData = JsonConvert.DeserializeObject <SmartMailUserAuthentication>(tokenResult); _token = tokenData.AccessToken; var messageHTML = generateMailBody(dto.RecipientName, dto.Message); var content = new EmailContentDto(dto.RecipientEmail, dto.Subject, messageHTML); using (var mailResponse = await HttpHelper.SendRequest(HttpMethod.Post, _smartMail.SendEmailApiUrl, tokenData.AccessToken, content, "text/html")) { if (mailResponse.IsSuccessStatusCode) { statusCode = 200; await _repository.Insert(dto.RecipientEmail, dto.RecipientName, dto.Message, statusCode, dto.AppId, dto.Subject, dto.CustomerId); } else { await _repository.Insert(dto.RecipientEmail, dto.RecipientName, dto.Message, (int)tokenResponse.StatusCode, dto.AppId, dto.Subject, dto.CustomerId, await mailResponse.Content.ReadAsStringAsync()); } } } else { await _repository.Insert(dto.RecipientEmail, dto.RecipientName, dto.Message, (int)tokenResponse.StatusCode, dto.AppId, dto.Subject, dto.CustomerId, await tokenResponse.Content.ReadAsStringAsync()); } } } catch (Exception ex) { await _repository.Insert(dto.RecipientEmail, dto.RecipientName, dto.Message, statusCode, dto.AppId, dto.Subject, dto.CustomerId, ex.ToString()); } return(await Task.FromResult(statusCode)); }
public async Task <IActionResult> ForgotPassword(ForgotPasswordDTO ForgotPassword) { var captchaText = TextTools.GetEnglishNumber(ForgotPassword.txtCaptcha); var session = HttpContext.Session.GetString("Captcha"); if (string.IsNullOrEmpty(session) || session != captchaText) { ModelState.AddModelError("txtCaptcha", "کد امنیتی را اشتباه وارد کردید"); } if (ModelState.IsValid) { var user = await userManager.FindByEmailAsync(ForgotPassword.UserName); if (user is null) { ModelState.AddModelError("UserName", " کاربری با این مشخصات یافت نشد ."); return(View(ForgotPassword)); } var Token = await userManager.GeneratePasswordResetTokenAsync(user); var request = HttpContext.Request; var Domain = $"{request.Scheme}://{request.Host}"; var emailContent = new EmailContentDto() { FirstName = user.FirstName, LastName = user.LastName, Link = $"{Domain}/Account/ResetPassword?username={user.UserName}&token={Token}&UCB={ForgotPassword.UCB}" }; var Body = await this.RenderViewAsync("_ResetPasswordEmailContent", emailContent); await sendEmailService.SendAsync(user.Email, Body); return(RedirectToAction("ResponseForgotPassword")); } return(View(ForgotPassword)); }
public async Task <IActionResult> FillEmailForm(string id) { try { var emailDto = new EmailContentDto { GmailId = id }; var email = await this.service.TakeBodyAsync(emailDto); var encryptBody = this.encodeDecodeService.Decrypt(email.Body); var result = new EmailBodyViewModel(id, encryptBody); return(View("FillEmailForm", result)); } catch (EmailExeption ex) { return(View("Message", new MessageViewModel { Message = ex.Message })); } }
public async Task AddBodyToCurrentEmail_Test() { var body = "TestBody"; var firstEmail = EmailGeneratorUtil.GenerateEmailFirst(); var encodeDecodeServiceMock = new Mock <IEncodeDecodeService>().Object; var loggerMock = new Mock <ILogger <EmailService> >().Object; var options = TestUtilities.GetOptions(nameof(AddBodyToCurrentEmail_Test)); using (var actContext = new E_MailApplicationsManagerContext(options)) { var email = await actContext.Emails.AddAsync(firstEmail); await actContext.SaveChangesAsync(); var emailDto = new EmailContentDto { Body = body, GmailId = firstEmail.GmailId }; var sut = new EmailService(actContext, loggerMock, encodeDecodeServiceMock); await sut.AddBodyToCurrentEmailAsync(emailDto); } using (var assertContext = new E_MailApplicationsManagerContext(options)) { var sut = assertContext.Emails .Select(emailBody => emailBody.Body) .ToString(); Assert.IsNotNull(sut); } }
public async Task <IActionResult> CheckBody(string id) { try { var emailDto = new EmailContentDto { UserId = User.FindFirstValue(ClaimTypes.NameIdentifier), GmailId = id }; var email = await this.service.CheckEmailBodyAsync(emailDto); var decodeBody = this.encodeDecodeService.Decrypt(email.Body); var result = new EmailBodyViewModel(id, decodeBody); return(View("CheckBody", result)); } catch (EmailExeption ex) { return(View("Message", new MessageViewModel { Message = ex.Message })); } }
public IHttpActionResult SendEmail([FromBody] EmailContentDto model) { Shared.SendEmailMsg(model); return(Ok(new { code = "200", result = "email has been sent" })); }