public async Task <string> SendTestEmailAsync(int userId, EmailOutgoingSettings outgoingSettings) { await _privilegesManager.Demand(userId, InstanceAdminPrivileges.ManageInstanceSettings); VerifyOutgoingSettings(outgoingSettings); var currentUser = await _userRepository.GetUserAsync(userId); if (string.IsNullOrWhiteSpace(currentUser.Email)) { throw new ConflictException("Your user profile does not include an email address. Please add one to receive a test email.", ErrorCodes.UserHasNoEmail); } var config = await GetEmailConfigAsync(outgoingSettings, currentUser); _emailHelper.Initialize(config); string body = EmailTemplateHelper.GetSendTestEmailTemplate(_websiteAddressService.GetWebsiteAddress()); try { _emailHelper.SendEmail(currentUser.Email, TestEmailSubject, body); return(currentUser.Email); } catch (EmailException ex) { throw new BadRequestException(ex.Message, ex.ErrorCode); } }
private async Task <IEmailConfigInstanceSettings> GetEmailConfigAsync(EmailOutgoingSettings outgoingSettings, User currentUser) { var config = new TestEmailConfigInstanceSettings(outgoingSettings); if (!outgoingSettings.AuthenticatedSmtp) { config.UserName = string.Empty; config.Password = string.Empty; } else if (!outgoingSettings.IsPasswordDirty) { var emailSettings = await _instanceSettingsRepository.GetEmailSettings(); config.Password = SystemEncryptions.DecryptFromSilverlight(emailSettings.Password); } return(config); }
private void VerifyOutgoingSettings(EmailOutgoingSettings outgoingSettings) { if (string.IsNullOrWhiteSpace(outgoingSettings.ServerAddress)) { throw new BadRequestException("Please enter a mail server.", ErrorCodes.OutgoingEmptyMailServer); } if (outgoingSettings.Port < 1 || outgoingSettings.Port > 65535) { throw new BadRequestException("Ensure the port number is between 1 and 65535.", ErrorCodes.OutgoingPortOutOfRange); } if (string.IsNullOrWhiteSpace(outgoingSettings.AccountEmailAddress)) { throw new BadRequestException("Please enter the system email account address.", ErrorCodes.EmptyEmailAddress); } // Input is trimmed to be consistent with SilverLight implementation if (!EmailValidator.IsEmailAddress(outgoingSettings.AccountEmailAddress.Trim())) { throw new BadRequestException("The system email account address is not in a valid format.", ErrorCodes.InvalidEmailAddress); } if (outgoingSettings.AuthenticatedSmtp) { if (string.IsNullOrWhiteSpace(outgoingSettings.AccountUsername)) { throw new BadRequestException("Please enter the SMTP administrator username.", ErrorCodes.EmptySmtpAdministratorUsername); } if (outgoingSettings.IsPasswordDirty && string.IsNullOrWhiteSpace(outgoingSettings.AccountPassword)) { throw new BadRequestException("Please enter the SMTP administrator password.", ErrorCodes.EmptySmtpAdministratorPassword); } } }
public Task <string> SendTestEmailAsync([FromBody] EmailOutgoingSettings settings) { return(_emailSettingsService.SendTestEmailAsync(Session.UserId, settings)); }
public void Initialize() { Mock <IPrivilegesRepository> privilegesRepositoryMock = new Mock <IPrivilegesRepository>(); Mock <IUserRepository> userRepositoryMock = new Mock <IUserRepository>(); Mock <IWebsiteAddressService> websiteAddressServiceMock = new Mock <IWebsiteAddressService>(); _instanceSettingsRepositoryMock = new Mock <IInstanceSettingsRepository>(); _emailHelperMock = new Mock <IEmailHelper>(); _incomingEmailServiceMock = new Mock <IIncomingEmailService>(); _emailSettingsService = new EmailSettingsService(new PrivilegesManager(privilegesRepositoryMock.Object), userRepositoryMock.Object, _emailHelperMock.Object, websiteAddressServiceMock.Object, _instanceSettingsRepositoryMock.Object, _incomingEmailServiceMock.Object); privilegesRepositoryMock.Setup(repo => repo.GetInstanceAdminPrivilegesAsync(UserId)).ReturnsAsync(() => _adminPrivilege); userRepositoryMock.Setup(repo => repo.GetUserAsync(UserId)).ReturnsAsync(() => _user); websiteAddressServiceMock.Setup(service => service.GetWebsiteAddress()).Returns(WebsiteAddress); _instanceSettingsRepositoryMock.Setup(repo => repo.GetEmailSettings()).ReturnsAsync(() => _emailSettings); // Setup Default Values _outgoingSettings = new EmailOutgoingSettings() { AuthenticatedSmtp = true, AccountPassword = "******", AccountUsername = "******", AccountEmailAddress = "*****@*****.**", EnableSsl = true, Port = 2, ServerAddress = "smtp.blueprintsys.com", IsPasswordDirty = true }; _incomingSettings = new EmailIncomingSettings() { AccountUsername = "******", AccountPassword = "******", EnableSsl = true, Port = 2, ServerAddress = "mail.test.com", ServerType = EmailClientType.Imap, IsPasswordDirty = true }; _adminPrivilege = InstanceAdminPrivileges.ManageInstanceSettings; _user = new User() { Email = "*****@*****.**" }; _emailSettings = new EmailSettings() { Password = EncryptedPassword, IncomingPassword = EncryptedPassword, Authenticated = true, EnableEmailDiscussion = false, EnableEmailReplies = true, EnableNotifications = true, EnableSSL = true, SenderEmailAddress = "*****@*****.**", HostName = "smtp.test.com", Port = 1234, UserName = "******", IncomingEnableSSL = false, IncomingHostName = "pop3.test.com", IncomingPort = 2345, IncomingServerType = 0, IncomingUserName = "******" }; _emailSettingsDto = new EmailSettingsDto() { EnableDiscussions = false, EnableEmailNotifications = true, EnableReviewNotifications = false, Incoming = new EmailIncomingSettings() { AccountPassword = "******", AccountUsername = "******", EnableSsl = true, IsPasswordDirty = true, Port = 8765, ServerAddress = "imap.test.com", ServerType = EmailClientType.Pop3 }, Outgoing = new EmailOutgoingSettings() { AccountEmailAddress = "*****@*****.**", AccountPassword = "******", AccountUsername = "******", AuthenticatedSmtp = true, EnableSsl = true, IsPasswordDirty = true, Port = 9876, ServerAddress = "mail.test.com" } }; }