private IImapClient GetImapClient(string mailServerUId, string mailboxName, string mailboxPassword) { var currentMailServer = new Terrasoft.Configuration.MailServer(UserConnection); if (!currentMailServer.FetchFromDB(mailServerUId) || !currentMailServer.GetTypedColumnValue <bool>("AllowEmailDownloading")) { return(null); } var imapServerCredentials = new MailCredentials { Host = currentMailServer.GetTypedColumnValue <string>("Address"), Port = currentMailServer.GetTypedColumnValue <int>("Port"), UseSsl = currentMailServer.GetTypedColumnValue <bool>("UseSSL"), StartTls = currentMailServer.GetTypedColumnValue <bool>("IsStartTls"), UserName = mailboxName, UserPassword = mailboxPassword }; SynchronizationErrorHelper helper = SynchronizationErrorHelper.GetInstance(UserConnection); try { return(ClassFactory.Get <IImapClient>("OldEmailIntegration", new ConstructorArgument("credentials", imapServerCredentials), new ConstructorArgument("errorMessages", new Terrasoft.Mail.ImapErrorMessages()), new ConstructorArgument("userConnection", UserConnection), new ConstructorArgument("login", true))); } catch (Exception ex) { helper.ProcessSynchronizationError(mailboxName, ex, true); throw; } }
/// <inheritdoc cref="IMailboxValidator.ValidateSynchronization"/> public CredentialsValidationInfo ValidateSynchronization(Mailbox mailbox) { var answer = new CredentialsValidationInfo { IsValid = true }; try { var credentials = new MailCredentials { UserName = mailbox.Login, UserPassword = mailbox.Password, SenderEmailAddress = mailbox.SenderEmailAddress, Host = mailbox.GetServerAddress(), Port = mailbox.GetServerPort(), UseSsl = mailbox.UseSsl, StartTls = mailbox.StartTls }; var imapClient = ClassFactory.Get <IImapClient>("OldEmailIntegration", new ConstructorArgument("credentials", credentials), new ConstructorArgument("errorMessages", new Terrasoft.Mail.ImapErrorMessages()), new ConstructorArgument("userConnection", UserConnection), new ConstructorArgument("login", true)); } catch (ImapException exception) { answer.IsValid = false; answer.Message = ConnectToServerCaption + exception.Message; } return(answer); }
public virtual void SendEmailWithDefaultSender(EmailMessage emailMessage, bool ignoreErrors) { MailCredentials credentials = new MailCredentials { Host = (string)SysSettings.GetValue(UserConnection, "SmtpHost"), Port = int.Parse(SysSettings.GetValue(UserConnection, "SmtpPort").ToString()), UseSsl = (bool)SysSettings.GetValue(UserConnection, "SmtpEnableSsl"), UserName = (string)SysSettings.GetValue(UserConnection, "SmtpUserName"), UserPassword = (string)SysSettings.GetValue(UserConnection, "SmtpUserPassword") }; string[] to = emailMessage.To.ToArray(); EmailAddressCollection cc = GetEmailAddressCollection(emailMessage.Cc); EmailAddressCollection bcc = GetEmailAddressCollection(emailMessage.Bcc); var smtpClient = new SmtpClient(UserConnection); MailMessage mailBeeMessage = smtpClient.CreateMessage(emailMessage.Body, emailMessage.Subject, to, credentials.UserName, true, new Dictionary <Guid, Tuple <byte[], string> >(0)); mailBeeMessage.Cc = cc; mailBeeMessage.Bcc = bcc; mailBeeMessage.Priority = (MailPriority)emailMessage.Priority; try { smtpClient.SendMessage(mailBeeMessage, credentials); } catch (Exception e) { _log.Error(e); if (!ignoreErrors) { throw; } } }
public void TestSend() { var random = new Random(); var port = random.Next(3389, 20000); var to = new [] { "*****@*****.**", "*****@*****.**" }; var from = "*****@*****.**"; var subject = "Hello, Test!"; var body = "<html><body>Hello, Unit Test!</body></html>"; using (var server = new SmtpServer(port)) { server.Start(); var credentials = new MailCredentials { Server = "localhost", Port = port, Username = "******", Password = "******", UseSsl = false }; using (var mailer = new Mailer(credentials)) { mailer.SendMail(to, from, subject, body); Thread.Sleep(100); } var message = server.Messages.FirstOrDefault(); Assert.IsNotNull(message); Assert.IsTrue(message.To.Contains(to[0])); Assert.IsTrue(message.To.Contains(to[1])); Assert.AreEqual(from, message.From); Assert.AreEqual(subject, message.Subject); Assert.AreEqual(body, message.Body.Trim()); } }
private ImapClient GetImapClient(string mailServerUId, string mailboxName, string mailboxPassword) { var currentMailServer = new Terrasoft.Configuration.MailServer(UserConnection); if (!currentMailServer.FetchFromDB(mailServerUId) || !currentMailServer.GetTypedColumnValue <bool>("AllowEmailDownloading")) { return(null); } var imapServerCredentials = new MailCredentials { Host = currentMailServer.GetTypedColumnValue <string>("Address"), Port = currentMailServer.GetTypedColumnValue <int>("Port"), UseSsl = currentMailServer.GetTypedColumnValue <bool>("UseSSL"), StartTls = currentMailServer.GetTypedColumnValue <bool>("IsStartTls"), UserName = mailboxName, UserPassword = mailboxPassword }; ImapClient imapClient; SynchronizationErrorHelper helper = SynchronizationErrorHelper.GetInstance(UserConnection); try { imapClient = new ImapClient(imapServerCredentials, MailSynchronizer.GetImapErrorMessages(UserConnection), UserConnection, true); } catch (Exception ex) { helper.ProcessSynchronizationError(mailboxName, ex, true); throw; } return(imapClient); }
public virtual bool ScriptTask1Execute(ProcessExecutingContext context) { #if !NETSTANDARD2_0 // TODO CRM-42546 var secureText = UserConnection.DataValueTypeManager.GetInstanceByName("SecureText") as SecureTextDataValueType; var currentUserId = CurrentUserId != Guid.Empty ? CurrentUserId : UserConnection.CurrentUser.Id; EntitySchemaQuery query = new EntitySchemaQuery(UserConnection.EntitySchemaManager, "MailboxSyncSettings"); query.AddColumn("UserName"); query.AddColumn("UserPassword"); query.AddColumn("MailServer.Address"); query.AddColumn("MailServer.Port"); query.AddColumn("MailServer.UseSSL"); query.AddColumn("MailServer.IsStartTls"); query.AddColumn("SenderEmailAddress"); query.Filters.Add(query.CreateFilterWithParameters(FilterComparisonType.Equal, "SysAdminUnit.Id", currentUserId)); query.Filters.Add(query.CreateFilterWithParameters(FilterComparisonType.Equal, "MailServer.AllowEmailDownloading", true)); query.Filters.Add(query.CreateFilterWithParameters(FilterComparisonType.Equal, "EnableMailSynhronization", true)); query.Filters.Add(query.CreateFilterWithParameters(FilterComparisonType.Equal, "SenderEmailAddress", SenderEmailAddress)); var select = query.GetSelectQuery(UserConnection); MailCredentials credentials = new MailCredentials(); using (var dbExecutor = UserConnection.EnsureDBConnection()) { using (var reader = select.ExecuteReader(dbExecutor)) { if (!reader.Read()) { var parameters = new Dictionary <string, object> { { "SenderEmailAddress", SenderEmailAddress }, { "CurrentUserId", currentUserId } }; MailSynchronizer.RemoveSyncJob(UserConnection, parameters); throw new ArgumentException("user does not have mail account"); } credentials.UserName = reader.GetColumnValue <string>(reader.GetName(0)); string rawPassword = reader.GetColumnValue <string>(reader.GetName(1)); if (!rawPassword.IsNullOrEmpty()) { credentials.UserPassword = secureText.GetValueForLoad(UserConnection, rawPassword).ToString(); } credentials.Host = reader.GetColumnValue <string>(reader.GetName(2)); credentials.Port = reader.GetColumnValue <int>(reader.GetName(3)); credentials.UseSsl = reader.GetColumnValue <bool>(reader.GetName(4)); credentials.StartTls = reader.GetColumnValue <bool>(reader.GetName(5)); credentials.SenderEmailAddress = reader.GetColumnValue <string>(reader.GetName(6)); } } using (var mailSynchronizer = new MailSynchronizer(UserConnection, credentials)) { mailSynchronizer.ValidateImapClient(); mailSynchronizer.SyncImapMail(); } #endif return(true); }
public bool CreateBPMOnlineFolder(Guid id, string userName, string userPassword) { if (UserConnection.GetIsFeatureEnabled("EmailIntegrationV2")) { return(false); } var currentMailServer = new MailServer(UserConnection); if (!currentMailServer.FetchFromDB(id)) { return(false); } var imapServerCredentials = new MailCredentials { Host = currentMailServer.GetTypedColumnValue <string>("Address"), Port = currentMailServer.GetTypedColumnValue <int>("Port"), UseSsl = currentMailServer.GetTypedColumnValue <bool>("UseSSL"), StartTls = currentMailServer.GetTypedColumnValue <bool>("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)); imapClient.EnsureFolderExists("BPMonline"); var mailboxFolderTypeId = new Guid("99c2351c-f0f8-e111-9dba-00155d051801"); var emailFolderTypeId = new Guid("b97a5836-1cd0-e111-90c6-00155d054c03"); var mailboxFolder = new ActivityFolder(UserConnection); if (mailboxFolder.FetchFromDB( new Dictionary <string, object> { { "FolderType", mailboxFolderTypeId }, { "Name", imapServerCredentials.UserName } })) { var activityFolderSchema = UserConnection.EntitySchemaManager.GetInstanceByName("ActivityFolder"); var bpmonlineFolder = activityFolderSchema.CreateEntity(UserConnection); var parentColumn = activityFolderSchema.Columns.GetByName("Parent"); var folderTypeColumn = activityFolderSchema.Columns.GetByName("FolderType"); bpmonlineFolder.SetDefColumnValues(); bpmonlineFolder.SetColumnValue("Name", "BPMonline"); bpmonlineFolder.SetColumnValue(parentColumn.ColumnValueName, mailboxFolder.PrimaryColumnValue); bpmonlineFolder.SetColumnValue(folderTypeColumn.ColumnValueName, emailFolderTypeId); bpmonlineFolder.Save(); return(true); } return(false); }
private IImapClient GetImapClientByMailboxId(string mailboxId, out string mailboxName) { mailboxName = string.Empty; var mailboxEntitySchema = UserConnection.EntitySchemaManager.GetInstanceByName("MailboxSyncSettings"); var mailbox = mailboxEntitySchema.CreateEntity(UserConnection); if (!mailbox.FetchFromDB(new Guid(mailboxId))) { return(null); } mailboxName = mailbox.GetTypedColumnValue <string>("UserName"); var mailServerUId = mailbox.GetTypedColumnValue <Guid>("MailServerId"); var mailServerEntitySchema = UserConnection.EntitySchemaManager.GetInstanceByName("MailServer"); var currentMailServer = mailServerEntitySchema.CreateEntity(UserConnection); if (!currentMailServer.FetchFromDB(mailServerUId) || !currentMailServer.GetTypedColumnValue <bool>("AllowEmailDownloading")) { return(null); } var imapServerCredentials = new MailCredentials { Host = currentMailServer.GetTypedColumnValue <string>("Address"), Port = currentMailServer.GetTypedColumnValue <int>("Port"), UseSsl = currentMailServer.GetTypedColumnValue <bool>("UseSSL"), UserName = mailbox.GetTypedColumnValue <string>("UserName"), UserPassword = mailbox.GetTypedColumnValue <string>("UserPassword"), StartTls = currentMailServer.GetTypedColumnValue <bool>("IsStartTls"), SenderEmailAddress = mailbox.GetTypedColumnValue <string>("SenderEmailAddress"), }; try { return(ClassFactory.Get <IImapClient>("OldEmailIntegration", new ConstructorArgument("credentials", imapServerCredentials), new ConstructorArgument("errorMessages", new Terrasoft.Mail.ImapErrorMessages()), new ConstructorArgument("userConnection", UserConnection), new ConstructorArgument("login", true))); } catch (Exception ex) { SynchronizationErrorHelper helper = SynchronizationErrorHelper.GetInstance(UserConnection); helper.ProcessSynchronizationError(imapServerCredentials.SenderEmailAddress, ex, true); throw; } }
private static MailCredentials CreateMailCredentials(UserConnection userConnection) { var smtpHost = (string)SysSettings.GetValue(userConnection, "SmtpHost"); var smtpPort = (int)SysSettings.GetValue(userConnection, "SmtpPort"); var smtpUserName = (string)SysSettings.GetValue(userConnection, "SmtpUserName"); var smtpUserPassword = (string)SysSettings.GetValue(userConnection, "SmtpUserPassword"); var enableSsl = (bool)SysSettings.GetValue(userConnection, "SmtpEnableSsl"); var credentials = new MailCredentials() { Host = smtpHost, Port = smtpPort, UseSsl = enableSsl, UserName = smtpUserName, UserPassword = smtpUserPassword }; if (smtpHost.IsNullOrEmpty() || smtpUserName.IsNullOrEmpty()) { string errorMessage = GetLocalizableString(userConnection, "MailNotConfigured"); throw new ArgumentNullOrEmptyException(errorMessage, null); } return(credentials); }
private ImapClient GetImapClientByMailboxId(string mailboxId, out string mailboxName) { mailboxName = string.Empty; var mailbox = new Terrasoft.Configuration.MailboxSyncSettings(UserConnection); if (!mailbox.FetchFromDB(new Guid(mailboxId))) { return(null); } mailboxName = mailbox.GetTypedColumnValue <string>("UserName"); var mailServerUId = mailbox.GetTypedColumnValue <Guid>("MailServerId"); var currentMailServer = new Terrasoft.Configuration.MailServer(UserConnection); if (!currentMailServer.FetchFromDB(mailServerUId) || !currentMailServer.GetTypedColumnValue <bool>("AllowEmailDownloading")) { return(null); } var imapServerCredentials = new MailCredentials { Host = currentMailServer.GetTypedColumnValue <string>("Address"), Port = currentMailServer.GetTypedColumnValue <int>("Port"), UseSsl = currentMailServer.GetTypedColumnValue <bool>("UseSSL"), UserName = mailbox.GetTypedColumnValue <string>("UserName"), UserPassword = mailbox.GetTypedColumnValue <string>("UserPassword"), StartTls = currentMailServer.GetTypedColumnValue <bool>("IsStartTls"), SenderEmailAddress = mailbox.GetTypedColumnValue <string>("SenderEmailAddress"), }; SynchronizationErrorHelper helper = SynchronizationErrorHelper.GetInstance(UserConnection); ImapClient imapClient; try { imapClient = new ImapClient(imapServerCredentials, MailSynchronizer.GetImapErrorMessages(UserConnection), UserConnection, true); } catch (Exception ex) { helper.ProcessSynchronizationError(imapServerCredentials.SenderEmailAddress, ex, true); throw; } return(imapClient); }
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 virtual void SendEmail(Dictionary <Guid, string> recepients) { object useEmails; if (Terrasoft.Core.Configuration.SysSettings.TryGetValue(UserConnection, "SendVisaEmail", out useEmails)) { if (useEmails is bool && !((bool)useEmails)) { return; } } var tempalteId = GetEmailTemplateId(); if (tempalteId == Guid.Empty) { return; } var esq = new EntitySchemaQuery(UserConnection.EntitySchemaManager, "EmailTemplate"); esq.AddColumn("Subject"); esq.AddColumn("Body"); esq.AddColumn("IsHtmlBody"); esq.AddColumn("Macros"); var template = (Terrasoft.Configuration.EmailTemplate)esq.GetEntity(UserConnection, tempalteId); if (template == null) { return; } var credentials = new MailCredentials(); credentials.Host = (string)Terrasoft.Core.Configuration.SysSettings.GetValue(UserConnection, "SmtpHost"); credentials.Port = int.Parse(Terrasoft.Core.Configuration.SysSettings.GetValue(UserConnection, "SmtpPort").ToString()); credentials.UseSsl = (bool)Terrasoft.Core.Configuration.SysSettings.GetValue(UserConnection, "SmtpEnableSsl"); credentials.UserName = (string)Terrasoft.Core.Configuration.SysSettings.GetValue(UserConnection, "SmtpUserName"); credentials.UserPassword = (string)Terrasoft.Core.Configuration.SysSettings.GetValue(UserConnection, "SmtpUserPassword"); if (string.IsNullOrEmpty(credentials.Host) || string.IsNullOrEmpty(credentials.UserName)) { return; } foreach (var recepient in recepients) { if (string.IsNullOrEmpty(recepient.Value)) { continue; } var priorityId = Guid.Parse("ab96fa02-7fe6-df11-971b-001d60e938c6"); var body = ProcessEmailBody(template.Body, recepient.Key, template.GetBytesValue("Macros")); var attachments = EmailTemplateUtility.GetEmailTemplateAttachments(UserConnection, tempalteId); var emailMessage = new EmailMessage { From = credentials.UserName, To = new List <string> { recepient.Value }, Subject = template.Subject, Body = body, Priority = EmailPriorityConverter.GetEmailPriority(priorityId), IsHtmlBody = template.IsHtmlBody, Attachments = attachments, }; try { var emailClientFactory = ClassFactory.Get <EmailClientFactory>(new ConstructorArgument("userConnection", UserConnection)); IEmailSender emailSender = ClassFactory.Get <IEmailSender>( new ConstructorArgument("emailClientFactory", emailClientFactory), new ConstructorArgument("userConnection", UserConnection)); emailSender.Send(emailMessage, credentials); } catch (SmtpException e) { // TODO: log exception } } }
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); }