public int Send(int tenant_id, string username, MailSendItem item, int mail_id) { var mbox = manager.GetMailBox(tenant_id, username, new MailAddress(item.From)); if (mbox == null) throw new ArgumentException("no such mailbox"); if (mbox.Name != "") { item.DisplayName = mbox.Name; } if (string.IsNullOrEmpty(item.HtmlBody)) item.HtmlBody = EmptyHtmlBody; string mime_message_id, in_reply_to; var message_item = SaveToDraft(tenant_id, username, item, mail_id, out mime_message_id, out in_reply_to, mbox); if (message_item.Id > 0) { var user_culture = Thread.CurrentThread.CurrentCulture; var user_ui_culture = Thread.CurrentThread.CurrentUICulture; ThreadPool.QueueUserWorkItem(delegate { try { Thread.CurrentThread.CurrentCulture = user_culture; Thread.CurrentThread.CurrentUICulture = user_ui_culture; item.ChangeEmbededAttachmentLinks(tenant_id, username); item.ChangeSmileLinks(); var message = item.ToMimeMessage(tenant_id, username, true); message.MessageId = mime_message_id; in_reply_to = in_reply_to.Trim(); if (!string.IsNullOrEmpty(in_reply_to)) message.InReplyTo = in_reply_to; if (mbox.RefreshToken != null) { ActiveUp.Net.Mail.SmtpClient.SendSsl(message, mbox.SmtpServer, mbox.SmtpPort, mbox.SmtpAccount, GetAccessToken(mbox), SaslMechanism.OAuth2); } else if (mbox.OutcomingEncryptionType == EncryptionType.None) { if (mbox.AuthenticationTypeSmtp == SaslMechanism.None) ActiveUp.Net.Mail.SmtpClient.Send(message, mbox.SmtpServer, mbox.SmtpPort); else ActiveUp.Net.Mail.SmtpClient.Send(message, mbox.SmtpServer, mbox.SmtpPort, mbox.SmtpAccount, mbox.SmtpPassword, mbox.AuthenticationTypeSmtp); } else { if (mbox.AuthenticationTypeSmtp == SaslMechanism.None) ActiveUp.Net.Mail.SmtpClient.SendSsl(message, mbox.SmtpServer, mbox.SmtpPort, mbox.OutcomingEncryptionType); else ActiveUp.Net.Mail.SmtpClient.SendSsl(message, mbox.SmtpServer, mbox.SmtpPort, mbox.SmtpAccount, mbox.SmtpPassword, mbox.AuthenticationTypeSmtp, mbox.OutcomingEncryptionType); } // message was correctly send - lets update its chains id var draft_chain_id = message_item.ChainId; // before moving message from draft to sent folder - lets recalculate its correct chain id message_item.ChainId = manager.DetectChainId(mbox, message_item); // push new message correct chain id to db manager.UpdateMessageChainId(mbox, message_item.Id, MailFolder.Ids.drafts, draft_chain_id, message_item.ChainId); manager.UpdateCrmLinkedChainId(mbox.MailBoxId, tenant_id, draft_chain_id, message_item.ChainId); //Move to_addresses sent manager.SetConversationsFolder(tenant_id, username, MailFolder.Ids.sent, new List<int> { (Int32)message_item.Id }); manager.SetMessageFolderRestore(tenant_id, username, MailFolder.Ids.sent, (int)message_item.Id); manager.AddRelationshipEventForLinkedAccounts(mbox, message_item); foreach (var handler in message_handlers) { try { handler.HandleRetrievedMessage(mbox, message, message_item, MailFolder.Ids.sent, string.Empty, string.Empty, message_item.IsNew, message_item.TagIds!= null ? message_item.TagIds.ToArray() : new int[0]); } catch (Exception ex) { log.Error(ex, "MailSendQueue::Send"); } } } catch (Exception ex) { AddNotificationAlertToMailbox(tenant_id, username, item, message_item, ex, mbox); } }); } else { throw new ArgumentException("Failed to_addresses save draft"); } return message_item.Id > 0 ? (Int32)message_item.Id : 1; // Callback in api will be raised if value > 0 }
public int Send(int tenant_id, string username, MailSendItem item, int mail_id) { var mbox = _manager.GetMailBox(tenant_id, username, new MailAddress(item.From)); if (mbox == null) throw new ArgumentException("no such mailbox"); if (mbox.Name != "") { item.DisplayName = mbox.Name; } string mime_message_id, in_reply_to; var result_message = SaveToDraft(tenant_id, username, item, mail_id, out mime_message_id, out in_reply_to, mbox); if (result_message.Id > 0) { var user_culture = Thread.CurrentThread.CurrentCulture; var user_ui_culture = Thread.CurrentThread.CurrentUICulture; ThreadPool.QueueUserWorkItem(delegate { Message message = null; try { Thread.CurrentThread.CurrentCulture = user_culture; Thread.CurrentThread.CurrentUICulture = user_ui_culture; item.ChangeEmbededAttachmentLinks(tenant_id, username); item.ChangeSmileLinks(); message = item.ToMimeMessage(tenant_id, username, true); message.MessageId = mime_message_id; in_reply_to = in_reply_to.Trim(); if (!string.IsNullOrEmpty(in_reply_to)) message.InReplyTo = in_reply_to; if (mbox.RefreshToken != null) { ActiveUp.Net.Mail.SmtpClient.SendSsl(message, mbox.SmtpServer, mbox.SmtpPort, mbox.SmtpAccount, GetAccessToken(mbox), SaslMechanism.OAuth2); } else if (mbox.OutcomingEncryptionType == EncryptionType.None) { if (mbox.AuthenticationTypeSmtp == SaslMechanism.None) ActiveUp.Net.Mail.SmtpClient.Send(message, mbox.SmtpServer, mbox.SmtpPort); else ActiveUp.Net.Mail.SmtpClient.Send(message, mbox.SmtpServer, mbox.SmtpPort, mbox.SmtpAccount, mbox.SmtpPassword, mbox.AuthenticationTypeSmtp); } else { if (mbox.AuthenticationTypeSmtp == SaslMechanism.None) ActiveUp.Net.Mail.SmtpClient.SendSsl(message, mbox.SmtpServer, mbox.SmtpPort, mbox.OutcomingEncryptionType); else ActiveUp.Net.Mail.SmtpClient.SendSsl(message, mbox.SmtpServer, mbox.SmtpPort, mbox.SmtpAccount, mbox.SmtpPassword, mbox.AuthenticationTypeSmtp, mbox.OutcomingEncryptionType); } //Move to_addresses sent _manager.SetConversationsFolder(tenant_id, username, MailFolder.Ids.sent, new List<int> { (Int32)result_message.Id }); _manager.SetMessageFolderRestore(tenant_id, username, MailFolder.Ids.sent, (int)result_message.Id); _manager.AddRelationshipEventForLinkedAccounts(mbox, result_message); } catch (Exception ex) { AddNotificationAlertToMailbox(tenant_id, username, item, result_message, ex, mbox, message); } }); } else { throw new ArgumentException("Failed to_addresses save draft"); } return result_message.Id > 0 ? (Int32)result_message.Id : 1; // Callback in api will be raised if value > 0 }
public int Send(int tenant, string username, MailSendItem originalMessage, int mailId, int mailboxId) { var mbox = manager.GetUnremovedMailBox(mailboxId); if (mbox == null) throw new ArgumentException("no such mailbox"); originalMessage.MailboxId = mbox.MailBoxId; if (mbox.Name != "") { originalMessage.DisplayName = mbox.Name; } if (string.IsNullOrEmpty(originalMessage.HtmlBody)) originalMessage.HtmlBody = EMPTY_HTML_BODY; var messageItem = SaveToDraft(originalMessage, mailId, mbox); if (messageItem.Id > 0) { var userCulture = Thread.CurrentThread.CurrentCulture; var userUiCulture = Thread.CurrentThread.CurrentUICulture; var scheme = HttpContext.Current.Request.GetUrlRewriter().Scheme; // move to_addresses temp manager.SetConversationsFolder(tenant, username, MailFolder.Ids.temp, new List<int> { (Int32)messageItem.Id }); manager.SetMessageFolderRestore(tenant, username, MailFolder.Ids.drafts, (int)messageItem.Id); ThreadPool.QueueUserWorkItem(delegate { Message mimeMessage; try { Thread.CurrentThread.CurrentCulture = userCulture; Thread.CurrentThread.CurrentUICulture = userUiCulture; CoreContext.TenantManager.SetCurrentTenant(tenant); SecurityContext.AuthenticateMe(new Guid(username)); ApiHelper.SetupScheme(scheme); originalMessage.ChangeEmbededAttachmentLinks(tenant, username); originalMessage.ChangeSmileLinks(); originalMessage.ChangeAttachedFileLinksAddresses(tenant); originalMessage.ChangeAttachedFileLinksImages(); mimeMessage = originalMessage.ToMimeMessage(tenant, username, true); var smptClient = MailClientBuilder.Smtp(); if (mbox.RefreshToken != null) { smptClient.SendSsl(mimeMessage, mbox.SmtpServer, mbox.SmtpPort, mbox.SmtpAccount, GetAccessToken(mbox), SaslMechanism.OAuth2); } else if (mbox.OutcomingEncryptionType == EncryptionType.None) { if (mbox.AuthenticationTypeSmtp == SaslMechanism.None) smptClient.Send(mimeMessage, mbox.SmtpServer, mbox.SmtpPort); else smptClient.Send(mimeMessage, mbox.SmtpServer, mbox.SmtpPort, mbox.SmtpAccount, mbox.SmtpPassword, mbox.AuthenticationTypeSmtp); } else { if (mbox.AuthenticationTypeSmtp == SaslMechanism.None) smptClient.SendSsl(mimeMessage, mbox.SmtpServer, mbox.SmtpPort, mbox.OutcomingEncryptionType); else smptClient.SendSsl(mimeMessage, mbox.SmtpServer, mbox.SmtpPort, mbox.SmtpAccount, mbox.SmtpPassword, mbox.AuthenticationTypeSmtp, mbox.OutcomingEncryptionType); } } catch (Exception ex) { AddNotificationAlertToMailbox(originalMessage, (Int32)messageItem.Id, ex, mbox); // move to_addresses drafts manager.SetConversationsFolder(tenant, username, MailFolder.Ids.drafts, new List<int> { (Int32)messageItem.Id }); manager.SetMessageFolderRestore(tenant, username, MailFolder.Ids.drafts, (int)messageItem.Id); // send unsuccess notification SendMailNotification(tenant, username, 1); return; } SendMailNotification(tenant, username, 0); try { // message was correctly send - lets update its chains id var draftChainId = messageItem.ChainId; // before moving message from draft to sent folder - lets recalculate its correct chain id messageItem.ChainId = manager.DetectChainId(mbox, messageItem); // push new message correct chain id to db manager.UpdateMessageChainId(mbox, messageItem.Id, MailFolder.Ids.temp, draftChainId, messageItem.ChainId); manager.UpdateCrmLinkedChainId(mbox.MailBoxId, tenant, draftChainId, messageItem.ChainId); // move to_addresses sent manager.SetConversationsFolder(tenant, username, MailFolder.Ids.sent, new List<int> { (Int32)messageItem.Id }); manager.SetMessageFolderRestore(tenant, username, MailFolder.Ids.sent, (int)messageItem.Id); manager.AddRelationshipEventForLinkedAccounts(mbox, messageItem, log); manager.SaveEmailInData(mbox, messageItem, log); manager.SaveMailContacts(mbox.TenantId, mbox.UserId, mimeMessage); StoreMessageToImapSentFolder(mbox, mimeMessage); } catch (Exception ex) { log.Error("Unexpected Error in Send(), message_item.Id = {0}, {1}, {2}", messageItem.Id, ex.ToString(), ex.StackTrace); } }); } else { throw new ArgumentException("Failed to_addresses save draft"); } return messageItem.Id > 0 ? (Int32) messageItem.Id : 1; // Callback in api will be raised if value > 0 }