public IsValidAnswer IsServerValid(Guid id, string userName, string userPassword, bool enableSync, bool sendEmail, string senderEmailAddress, bool isAnonymousAuthentication) { var currentMailServer = new MailServer(UserConnection); var isValidAnswer = new IsValidAnswer() { IsValid = true, Message = string.Empty }; if (!currentMailServer.FetchFromDB(id)) { isValidAnswer.IsValid = false; return(isValidAnswer); } if (currentMailServer.AllowEmailDownloading && enableSync) { try { var imapServerCredentials = new MailCredentials { Host = currentMailServer.Address, Port = currentMailServer.Port, UseSsl = currentMailServer.UseSSL, StartTls = currentMailServer.IsStartTls, UserName = userName, UserPassword = userPassword }; var mapClient = new ImapClient(imapServerCredentials, MailSynchronizer.GetImapErrorMessages(UserConnection), UserConnection, true); } catch (ImapException exception) { isValidAnswer.IsValid = false; isValidAnswer.Message = ConnectToImapServerCaption.ToString() + exception.Message; } } if (currentMailServer.AllowEmailSending && sendEmail) { var smtpServerCredentials = new MailCredentials(); if (isAnonymousAuthentication) { smtpServerCredentials.IsAnonymousAuthentication = true; } else { smtpServerCredentials.UserName = userName; smtpServerCredentials.UserPassword = userPassword; } smtpServerCredentials.Host = currentMailServer.SMTPServerAddress; smtpServerCredentials.Port = currentMailServer.SMTPPort; smtpServerCredentials.UseSsl = currentMailServer.UseSSLforSending; smtpServerCredentials.StartTls = currentMailServer.IsStartTls; smtpServerCredentials.Timeout = currentMailServer.SMTPServerTimeout * 1000; smtpServerCredentials.SenderEmailAddress = senderEmailAddress; var smtpClient = new SmtpClient(UserConnection, smtpServerCredentials); try { smtpClient.SendTestMessage(); } catch (SmtpException ex) { if (!string.IsNullOrEmpty(isValidAnswer.Message)) { isValidAnswer.Message += Environment.NewLine; } isValidAnswer.IsValid = false; isValidAnswer.Message += CanNotSendTestMessageCaption.ToString() + ex.Message; } } return(isValidAnswer); }
public IsValidAnswer IsServerValid(Guid id, string userName, string userPassword, bool enableSync, bool sendEmail, string senderEmailAddress, bool isAnonymousAuthentication) { var currentMailServer = new MailServer(UserConnection); var isValidAnswer = new IsValidAnswer() { IsValid = true, Message = string.Empty }; if (!currentMailServer.FetchFromDB(id)) { isValidAnswer.IsValid = false; return(isValidAnswer); } if (currentMailServer.AllowEmailDownloading && enableSync) { try { var imapServerCredentials = new MailCredentials { Host = currentMailServer.Address, Port = currentMailServer.Port, UseSsl = currentMailServer.UseSSL, StartTls = currentMailServer.IsStartTls, UserName = userName, UserPassword = userPassword }; var imapClient = ClassFactory.Get <IImapClient>("OldEmailIntegration", new ConstructorArgument("credentials", imapServerCredentials), new ConstructorArgument("errorMessages", new Terrasoft.Mail.ImapErrorMessages()), new ConstructorArgument("userConnection", UserConnection), new ConstructorArgument("login", true)); } catch (ImapException exception) { isValidAnswer.IsValid = false; isValidAnswer.Message = ConnectToImapServerCaption.ToString() + exception.Message; } } if (currentMailServer.AllowEmailSending && sendEmail) { var smtpServerCredentials = new MailCredentials(); if (isAnonymousAuthentication) { smtpServerCredentials.IsAnonymousAuthentication = true; } else { smtpServerCredentials.UserName = userName; smtpServerCredentials.UserPassword = userPassword; } smtpServerCredentials.Host = currentMailServer.SMTPServerAddress; smtpServerCredentials.Port = currentMailServer.SMTPPort; smtpServerCredentials.UseSsl = currentMailServer.UseSSLforSending; smtpServerCredentials.StartTls = currentMailServer.IsStartTls; smtpServerCredentials.Timeout = currentMailServer.SMTPServerTimeout * 1000; smtpServerCredentials.SenderEmailAddress = senderEmailAddress; try { var emailClientFactory = ClassFactory.Get <EmailClientFactory>(new ConstructorArgument("userConnection", UserConnection)); IEmailSender emailSender = ClassFactory.Get <IEmailSender>( new ConstructorArgument("emailClientFactory", emailClientFactory), new ConstructorArgument("userConnection", UserConnection)); emailSender.SendTestMessage(smtpServerCredentials.SenderEmailAddress, smtpServerCredentials); } catch (SmtpException ex) { _log.Error(ex); if (!string.IsNullOrEmpty(isValidAnswer.Message)) { isValidAnswer.Message += Environment.NewLine; } isValidAnswer.IsValid = false; isValidAnswer.Message += CanNotSendTestMessageCaption.ToString() + ex.Message; } } return(isValidAnswer); }