/// <summary> /// Finds the incident by ticket. /// </summary> /// <param name="Ticket">The ticket.</param> /// <returns></returns> private static int FindIncidentByTicket(string Ticket) { int IncidentId = TicketUidUtil.GetThreadId(Ticket); using (IDataReader reader = DBIncident.GetIncident(IncidentId, 0, 1)) { if (reader.Read()) { return(IncidentId); } } return(-1); }
/// <summary> /// Gets the outgoing email format body preview. /// </summary> /// <param name="IncidentId">The incident id.</param> /// <returns></returns> public static string GetOutgoingEmailFormatBodyPreview(int issueId) { IncidentBox incidentBox = IncidentBox.Load(Incident.GetIncidentBox(issueId)); string issueTitle = Incident.GetIncidentTitle(issueId); string issueLink = Alerts2.MakeIssueLink(issueId); string issueTicket = TicketUidUtil.Create(incidentBox.IdentifierMask, issueId); StringBuilder sb = new StringBuilder(incidentBox.Document.GeneralBlock.OutgoingEmailFormatBody, 4096); sb.Replace("[=Title=]", issueTitle); sb.Replace("[=Ticket=]", issueTicket); //sb.Replace("[=Text=]", node.Text); string linkStart = string.Format(CultureInfo.InvariantCulture, "<a href=\"{0}\">", HttpUtility.HtmlAttributeEncode(issueLink)); sb.Replace("[=Link=]", linkStart); sb.Replace("[=/Link=]", "</a>"); UserLight currentUser = Security.CurrentUser; if (currentUser != null) { sb.Replace("[=FirstName=]", currentUser.FirstName); sb.Replace("[=LastName=]", currentUser.LastName); sb.Replace("[=Email=]", currentUser.Email); using (IDataReader reader = User.GetUserProfile(currentUser.UserID)) { if (reader.Read()) { sb.Replace("[=Phone=]", reader["Phone"].ToString()); sb.Replace("[=Fax=]", reader["Fax"].ToString()); sb.Replace("[=Mobile=]", reader["Mobile"].ToString()); sb.Replace("[=Position=]", reader["Position"].ToString()); sb.Replace("[=Department=]", reader["Department"].ToString()); sb.Replace("[=Company=]", reader["Company"].ToString()); sb.Replace("[=Location=]", reader["Location"].ToString()); } } } return(sb.ToString()); }
public static void OpenEml(int IncidentId, int EMailMessageId, Stream outputStream) { Pop3Message message = EMailMessage.GetPop3Message(EMailMessageId); IncidentBox incidentBox = IncidentBox.Load(Incident.GetIncidentBox(IncidentId)); EMailRouterPop3Box emailBox = EMailRouterPop3Box.ListInternal(); string SenderName = EMailMessage.GetSenderName(message); string FromEmail = SenderName == string.Empty?emailBox.EMailAddress: string.Format("\"{0}\" <{1}>", SenderName, emailBox.EMailAddress); OutputMessageCreator output = new OutputMessageCreator(message, IncidentId, emailBox.EMailAddress, FromEmail); output.AddRecipient(Security.CurrentUser.UserID); string Subject = (message.Subject == null?string.Empty:message.Subject); if (Subject == string.Empty) { // OZ: Maybe: Set Default Inicdent Title if subject is empty //Subject = Incident.GetIncidentTitle(IncidentId); } if (TicketUidUtil.LoadFromString(Subject) == string.Empty) { output.Subject = string.Format("[{0}] {1}", TicketUidUtil.Create(incidentBox.IdentifierMask, IncidentId), Subject); } OutputMessage outputMsg = (OutputMessage)output.Create()[0]; outputStream.Write(outputMsg.Data, 0, outputMsg.Data.Length); outputStream.Flush(); }
/// <summary> /// Checks the specified email box. /// </summary> /// <param name="emailBox">The email box.</param> /// <param name="message">The message.</param> /// <returns></returns> public static EMailMessageAntiSpamRuleRusult Check(EMailRouterPop3Box emailBox, Pop3Message message) { // 2007-01-09: Check Empty From and Sender if (message.Sender == null && message.From == null || message.To == null) { return(EMailMessageAntiSpamRuleRusult.Deny); } EMailMessageAntiSpamRule[] antiSpamRules = EMailMessageAntiSpamRule.List(); // Load Rules //EMailMessageAntiSpamRule[] antiSpamRules = _antiSpamRules; // Check Rules foreach (EMailMessageAntiSpamRule rule in antiSpamRules) { if (((EMailMessageAntiSpamRuleType)rule.RuleType) != EMailMessageAntiSpamRuleType.Service) { string KeyValue = GetStringFromEmailByKey(message, rule.Key); if (KeyValue != null) { switch ((EMailMessageAntiSpamRuleType)rule.RuleType) { case EMailMessageAntiSpamRuleType.Contains: foreach (string ContainsSubStr in rule.Value.Split(';')) { string TrimContainsSubStr = ContainsSubStr.Trim(); if (TrimContainsSubStr != string.Empty) { if (TrimContainsSubStr.IndexOfAny(new char[] { '*', '?' }) != -1) { if (Pattern.Match(KeyValue, TrimContainsSubStr)) { return(rule.Accept ? EMailMessageAntiSpamRuleRusult.Accept : EMailMessageAntiSpamRuleRusult.Deny); } } else { if (KeyValue.IndexOf(TrimContainsSubStr) != -1) { return(rule.Accept ? EMailMessageAntiSpamRuleRusult.Accept : EMailMessageAntiSpamRuleRusult.Deny); } } } } break; case EMailMessageAntiSpamRuleType.IsEqual: if (string.Compare(KeyValue, rule.Value, true) == 0) { return(rule.Accept ? EMailMessageAntiSpamRuleRusult.Accept : EMailMessageAntiSpamRuleRusult.Deny); } break; case EMailMessageAntiSpamRuleType.RegexMatch: Match match = Regex.Match(KeyValue, rule.Value); if ((match.Success && (match.Index == 0)) && (match.Length == KeyValue.Length)) { return(rule.Accept ? EMailMessageAntiSpamRuleRusult.Accept : EMailMessageAntiSpamRuleRusult.Deny); } break; } } } else { string FromEmail = EMailMessage.GetSenderEmail(message); switch (rule.Key) { case BlackListServiceName: if (BlackListItem.Contains(FromEmail)) { return(rule.Accept ? EMailMessageAntiSpamRuleRusult.Accept : EMailMessageAntiSpamRuleRusult.Deny); } break; case WhiteListServiceName: if (WhiteListItem.Contains(FromEmail)) { return(rule.Accept ? EMailMessageAntiSpamRuleRusult.Accept : EMailMessageAntiSpamRuleRusult.Deny); } break; case TicketServiceName: if (message.Subject != null && TicketUidUtil.LoadFromString(message.Subject) != string.Empty) { return(rule.Accept ? EMailMessageAntiSpamRuleRusult.Accept : EMailMessageAntiSpamRuleRusult.Deny); } break; case IncidentBoxRulesServiceName: // Step 1. Get Incident Info IEMailIncidentMapping mappingHandler = EMailIncidentMappingHandler.LoadHandler(emailBox.Settings.SelectedHandlerId); IncidentInfo incidentInfo = mappingHandler.Create(emailBox, message); // Step 2. Evaluate IncidentBoxRules IncidentBox box = IncidentBoxRule.Evaluate(incidentInfo, false); if (box != null) { return(rule.Accept ? EMailMessageAntiSpamRuleRusult.Accept : EMailMessageAntiSpamRuleRusult.Deny); } break; } } } return(EMailMessageAntiSpamRuleRusult.Pending); }
public static ArrayList Send(int IncidentId, EMailRouterPop3Box pop3Box, Pop3Message InMsg, string[] RecipientEmails) { ArrayList retVal = new ArrayList(); IncidentBox incidentBox = IncidentBox.Load(Incident.GetIncidentBox(IncidentId)); EMailRouterIncidentBoxBlock settings = IncidentBoxDocument.Load(incidentBox.IncidentBoxId).EMailRouterBlock; if (pop3Box.IsInternal) { #region Internal -> Extrenal // Internal -> Internal Info // 2009-06-16 OZ: Add Chech send emails to internal users if (settings.AllowEMailRouting) { // 2007-02-12 OZ: "FN LN" <*****@*****.**> string FromEmail = string.Format("\"{0} {1}\" <{2}>", Security.CurrentUser.LastName, Security.CurrentUser.FirstName, pop3Box.EMailAddress); OutputMessageCreator output2Iternal = new OutputMessageCreator(InMsg, IncidentId, pop3Box.EMailAddress, FromEmail); // Exclude a message sender //output2Iternal.AddIgnoreRecipient(EMailMessage.GetSenderEmail(InMsg)); // Load InternalUser foreach (int UserId in GetInternalUsersByIncidentId(IncidentId)) { output2Iternal.AddRecipient(UserId); retVal.Add(UserId); } // Load InformationRecipientList // foreach(int infoRecipient in IncidentBoxDocument.Load(incidentBox.IncidentBoxId).EMailRouterBlock.InformationRecipientList) // { // output2Iternal.AddRecipient(infoRecipient); // } foreach (OutputMessage outputMsg in output2Iternal.Create()) { //try //{ SmtpClientUtility.SendMessage(OutgoingEmailServiceType.HelpDeskEmailBox, pop3Box.EMailRouterPop3BoxId, outputMsg.MailFrom, outputMsg.RcptTo, outputMsg.Subject, outputMsg.Data); //} //catch(Exception ex) //{ // System.Diagnostics.Trace.WriteLine(ex); // Log.WriteError(ex.ToString()); //} } } // Internal -> Extrenal string publicEmail = FindEMailRouterPublicEmail(IncidentId); if (publicEmail != string.Empty) { //EMailRouterPop3Box externalPop3Box = EMailRouterPop3Box.Load(realEMailBoxId); OutputMessageCreator output2External = new OutputMessageCreator(InMsg, IncidentId, publicEmail, publicEmail); // Load External Senders // foreach(string exRecipient in EMailMessage.GetExternalSendersByIncidentId(IncidentId)) // { // output2External.AddRecipient(exRecipient); // } // Load External Senders foreach (EMailIssueExternalRecipient exRecipient in EMailIssueExternalRecipient.List(IncidentId)) { output2External.AddRecipient(exRecipient.EMail); } // 2007-03-12 RecipientEmails Addon if (RecipientEmails != null) { foreach (string exItem in RecipientEmails) { int emailUserId = DBUser.GetUserByEmail(exItem, false); if (emailUserId > 0) { if (!retVal.Contains(emailUserId)) { output2External.AddRecipient(exItem); retVal.Add(emailUserId); } } else { output2External.AddRecipient(exItem); } } } // int emailBoxId = EMail.EMailRouterOutputMessage.FindEMailRouterPublicId(IncidentId); foreach (OutputMessage outputMsg in output2External.Create()) { //try //{ SmtpClientUtility.SendMessage(OutgoingEmailServiceType.HelpDeskEmailBox, emailBoxId, outputMsg.MailFrom, outputMsg.RcptTo, outputMsg.Subject, outputMsg.Data); //} //catch(Exception ex) //{ // System.Diagnostics.Trace.WriteLine(ex); // Log.WriteError(ex.ToString()); //} } } #endregion } else { if (!settings.AllowEMailRouting) { return(retVal); } EMailRouterPop3Box internalPop3Box = EMailRouterPop3Box.ListInternal(); if (internalPop3Box == null) { return(retVal); } #region // External -> Internal // External -> Internal string SenderName = EMailMessage.GetSenderName(InMsg); string FromEmail = SenderName == string.Empty?internalPop3Box.EMailAddress: string.Format("\"{0}\" <{1}>", SenderName, internalPop3Box.EMailAddress); OutputMessageCreator output = new OutputMessageCreator(InMsg, IncidentId, internalPop3Box.EMailAddress, FromEmail); string Subject = (InMsg.Subject == null?string.Empty:InMsg.Subject); if (Subject == string.Empty) { // OZ: Maybe: Set Default Inicdent Title if subject is empty //Subject = Incident.GetIncidentTitle(IncidentId); } if (TicketUidUtil.LoadFromString(Subject) == string.Empty) { output.Subject = string.Format("[{0}] {1}", TicketUidUtil.Create(incidentBox.IdentifierMask, IncidentId), Subject); } foreach (int UserId in GetInternalUsersByIncidentId(IncidentId)) { output.AddRecipient(UserId); retVal.Add(UserId); } // IncidentBoxDocument incidentDoc = IncidentBoxDocument.Load(incidentBox.IncidentBoxId); // // foreach(int infoRecipient in incidentDoc.EMailRouterBlock.InformationRecipientList) // { // output.AddRecipient(infoRecipient); // } foreach (OutputMessage outputMsg in output.Create()) { //try //{ SmtpClientUtility.SendMessage(OutgoingEmailServiceType.HelpDeskEmailBox, internalPop3Box.EMailRouterPop3BoxId, outputMsg.MailFrom, outputMsg.RcptTo, outputMsg.Subject, outputMsg.Data); //} //catch(Exception ex) //{ // System.Diagnostics.Trace.WriteLine(ex); // Log.WriteError(ex.ToString()); //} } #endregion } return(retVal); }
/// <summary> /// Approves the pending. /// </summary> /// <param name="EMailMessageId">The E mail message id.</param> public static void ApprovePending(int EMailMessageId) { // Stop Double Approve if (!PendingEMailMessageRow.Contains(EMailMessageId)) { return; } using (DbTransaction tran = DbTransaction.Begin()) { // Remove from pending PendingEMailMessageRow.DeleteByEMailMessageId(EMailMessageId); // Load Pop3 Message EMailMessageInfo msgInfo = EMailMessageInfo.Load(EMailMessageId); EMailRouterPop3Box emailBox = EMailRouterPop3Box.Load(msgInfo.EMailRouterPop3BoxId); Pop3Message msg = EMailMessage.GetPop3Message(EMailMessageId); // Add User to White Lits string SenderEmail = EMailMessage.GetSenderEmail(msg); //if(!WhiteListItem.Contains(SenderEmail)) if (PortalConfig.UseAntiSpamFilter && PortalConfig.AutoFillWhiteList) { WhiteListItem.Create(SenderEmail); } string TicketUID = TicketUidUtil.LoadFromString(msg.Subject == null ? string.Empty : msg.Subject); if (TicketUID == string.Empty) { int IncidentId = CreateNewIncident(EMailMessageId, emailBox, msg); // Automaticaly by Incident.CreateFromEmail //EMailRouterOutputMessage.Send(IncidentId, emailBox, msg); } else { // Assign By Ticket int IncidentId = FindIncidentByTicket(TicketUID); if (IncidentId != -1) { int creatorId, issueBoxId; using (IDataReader reader = Incident.GetIncident(IncidentId)) { reader.Read(); creatorId = (int)reader["CreatorId"]; issueBoxId = (int)reader["IncidentBoxId"]; } int stateId, managerId, responsibleId; bool isResposibleGroup; ArrayList users = new ArrayList(); Issue2.GetIssueBoxSettings(issueBoxId, out stateId, out managerId, out responsibleId, out isResposibleGroup, users); UserLight prevUser = LogOnCreator(creatorId, msg); int ThreadNodeId = AddToIncidentMessage(emailBox.IsInternal, IncidentId, EMailMessageId, msg); if (ProcessXIbnHeaders(IncidentId, ThreadNodeId, msg)) { ArrayList excludeUsers = EMailRouterOutputMessage.Send(IncidentId, emailBox, msg); // O.R. [2008-09-09]: Exclude inactive users if (responsibleId > 0 && User.GetUserActivity(responsibleId) != User.UserActivity.Active) { responsibleId = -1; } ArrayList activeUsers = new ArrayList(); foreach (int userId in users) { if (User.GetUserActivity(userId) == User.UserActivity.Active) { activeUsers.Add(userId); } } // Issue2.SendAlertsForNewIssue(IncidentId, managerId, responsibleId, activeUsers, excludeUsers); } } else { IncidentId = CreateNewIncident(EMailMessageId, emailBox, msg); // Automaticaly by Incident.CreateFromEmail //EMailRouterOutputMessage.Send(IncidentId, emailBox, msg); } } tran.Commit(); } }
public static void Open(int IncidentId, int EMailMessageId, Stream outputStream) { System.Reflection.Assembly asm = typeof(MsgMessage).Assembly; Pop3Message message = EMailMessage.GetPop3Message(EMailMessageId); IncidentBox incidentBox = IncidentBox.Load(Incident.GetIncidentBox(IncidentId)); EMailRouterPop3Box box = EMailRouterPop3Box.ListInternal(); string receiver = Security.CurrentUser.Email; //EMailMessage.GetSenderEmail(message); string sender = box.EMailAddress; using (Stream stream = asm.GetManifestResourceStream("Mediachase.IBN.Business.Resources.template.msg")) { using (MemoryStream memStream = new MemoryStream()) { using (MsgHelper helper = new MsgHelper(stream)) { helper.SetSenderEmail(sender); //string SenderName = EMailMessage.GetSenderName(message); //helper.SetSenderName(SenderName==string.Empty?sender:SenderName); helper.SetSenderName(sender); string IncidentTitle = Incident.GetIncidentTitle(IncidentId); string IncidentTicket = TicketUidUtil.Create(incidentBox.IdentifierMask, IncidentId); string Subject = (message.Subject == null?string.Empty:message.Subject); if (Subject == string.Empty) { // OZ: Maybe: Set Default Inicdent Title if subject is empty //Subject = Incident.GetIncidentTitle(IncidentId); } if (TicketUidUtil.LoadFromString(Subject) == string.Empty) { Subject = string.Format("[{0}] {1}", TicketUidUtil.Create(incidentBox.IdentifierMask, IncidentId), Subject); } helper.SetReceiverName(Security.CurrentUser.DisplayName); helper.SetDisplayTo(receiver); helper.SetReceiverEmail(receiver); helper.SetCreationTimes(DateTime.UtcNow); helper.SetSubject(Subject); helper.SetBody(message.BodyText); if (message.BodyHtml == null || message.BodyHtml.Trim() == String.Empty) { helper.SetHtmlBody(message.BodyText); } else { helper.SetHtmlBody(message.BodyHtml); } helper.Commit(); helper.createMSG(memStream); memStream.Flush(); memStream.Seek(0, SeekOrigin.Begin); const int bufLen = 1024; byte[] buffer = new byte[bufLen]; int bytesRead; while ((bytesRead = memStream.Read(buffer, 0, bufLen)) > 0) { outputStream.Write(buffer, 0, bytesRead); } outputStream.Flush(); } } } }
private void OnNewMessage(object sender, EventArgs e) { if (e is NewPop3MessageEventArgs && sender is EMailRouterPop3BoxMessageLoader) { EMailRouterPop3Box internalPop3Box = EMailRouterPop3Box.ListInternal(); if (internalPop3Box != null && !internalPop3Box.Activity.IsActive) { internalPop3Box = null; } UserLight prevUser = Security.CurrentUser; Alerts2.SendToCurrentUser = true; try { using (DbTransaction tran = DbTransaction.Begin()) { EMailRouterPop3BoxMessageLoader loader = (EMailRouterPop3BoxMessageLoader)sender; Pop3Message msg = ((NewPop3MessageEventArgs)e).Message; string TicketUID = TicketUidUtil.LoadFromString(msg.Subject == null ? string.Empty : msg.Subject); EMailMessageAntiSpamRuleRusult result = EMailMessageAntiSpamRuleRusult.Deny; if (loader.Owner.IsInternal) { int UserId = DBUser.GetUserByEmail(EMailMessage.GetSenderEmail(msg), false); if (TicketUID != string.Empty && UserId > 0) { result = EMailMessageAntiSpamRuleRusult.Accept; } } else { if (PortalConfig.UseAntiSpamFilter) { result = EMailMessageAntiSpamRule.Check(loader.Owner, msg); } else { result = EMailMessageAntiSpamRuleRusult.Accept; } } int EMailMessageId = -1; // OZ: [2007-01-29] EMailMessageLog if (EmailLogSettings != null) { string from = string.Empty; if (msg.Headers["Reply-To"] != null) { from = msg.Headers["Reply-To"]; } //else if (msg.Headers["Sender"] != null) //{ // from = msg.Headers["Sender"]; //} else if (msg.Headers["From"] != null) { from = msg.Headers["From"]; } EMailMessageLog.Add(from, msg.Headers["To"] != null ? msg.Headers["To"] : string.Empty, msg.Headers["Subject"] != null ? msg.Headers["Subject"] : string.Empty, loader.Owner.EMailRouterPop3BoxId, result); } // switch (result) { case EMailMessageAntiSpamRuleRusult.Pending: // Register Email Message EMailMessageId = RegisterEmail(loader.Owner.EMailRouterPop3BoxId, msg); // Add to pending email table EMailMessage.MarkAsPendingMessage(EMailMessageId); break; case EMailMessageAntiSpamRuleRusult.Accept: // Register Email Message EMailMessageId = RegisterEmail(loader.Owner.EMailRouterPop3BoxId, msg); if (TicketUID == string.Empty) { int IncidentId = EMailMessage.CreateNewIncident(EMailMessageId, loader.Owner, msg); } else { // Assign By Ticket int IncidentId = FindIncidentByTicket(TicketUID); if (IncidentId != -1) { int creatorId, issueBoxId; using (IDataReader reader = DBIncident.GetIncident(IncidentId, 0, 1)) { reader.Read(); creatorId = (int)reader["CreatorId"]; issueBoxId = (int)reader["IncidentBoxId"]; } int stateId, managerId, responsibleId; bool isResposibleGroup; ArrayList users = new ArrayList(); Issue2.GetIssueBoxSettings(issueBoxId, out stateId, out managerId, out responsibleId, out isResposibleGroup, users); EMailMessage.LogOnCreator(creatorId, msg); int ThreadNodeId = EMailMessage.AddToIncidentMessage(loader.Owner.IsInternal, IncidentId, EMailMessageId, msg); if (EMailMessage.ProcessXIbnHeaders(IncidentId, ThreadNodeId, msg)) { try { ArrayList excludeUsers = EMailRouterOutputMessage.Send(IncidentId, loader.Owner, msg); SystemEvents.AddSystemEvents(SystemEventTypes.Issue_Updated_Forum_MessageAdded, IncidentId, -1, excludeUsers); } catch (Exception ex) { System.Diagnostics.Trace.WriteLine(ex); Log.WriteError(ex.ToString()); } } } else { // Add to pending email table if (!loader.Owner.IsInternal) { EMailMessage.MarkAsPendingMessage(EMailMessageId); } } } break; case EMailMessageAntiSpamRuleRusult.Deny: // Do nothing break; } tran.Commit(); } } finally { Alerts2.SendToCurrentUser = false; if (Security.CurrentUser != prevUser) { Security.SetCurrentUser(prevUser); } } } }