Added rules as json to monitor the inbox

This commit is contained in:
2026-03-17 14:16:06 +00:00
parent af384a8dd1
commit 7699086df4
6 changed files with 422 additions and 171 deletions
+27 -7
View File
@@ -7,17 +7,19 @@ namespace OutlookCaseHelper
{ {
public partial class Form1 : Form public partial class Form1 : Form
{ {
private NotifyIcon trayIcon; private NotifyIcon trayIcon = null!;
private ContextMenuStrip trayMenu; private ContextMenuStrip trayMenu = null!;
private OutlookHelper outlookHelper; private OutlookHelper outlookHelper;
private System.Windows.Forms.Timer monitorTimer = null!;
public Form1() public Form1()
{ {
InitializeComponent(); InitializeComponent();
this.WindowState = FormWindowState.Minimized; this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false; this.ShowInTaskbar = false;
InitializeTray();
outlookHelper = new OutlookHelper(); outlookHelper = new OutlookHelper();
InitializeTray();
InitializeTimer();
} }
private void InitializeTray() private void InitializeTray()
@@ -37,6 +39,23 @@ namespace OutlookCaseHelper
trayIcon.MouseDoubleClick += TrayIcon_MouseDoubleClick; trayIcon.MouseDoubleClick += TrayIcon_MouseDoubleClick;
} }
private void InitializeTimer()
{
monitorTimer = new System.Windows.Forms.Timer();
monitorTimer.Interval = 60000; // verifica a cada 60 segundos
monitorTimer.Tick += MonitorTimer_Tick;
monitorTimer.Start();
}
private void MonitorTimer_Tick(object? sender, EventArgs e)
{
try
{
outlookHelper.ProcessActiveRules();
}
catch { }
}
private void ProcessEmail_Click(object? sender, EventArgs e) private void ProcessEmail_Click(object? sender, EventArgs e)
{ {
try try
@@ -55,8 +74,9 @@ namespace OutlookCaseHelper
bool success = outlookHelper.CreateFolderAndMoveEmails(trackingId); bool success = outlookHelper.CreateFolderAndMoveEmails(trackingId);
if (success) if (success)
{ {
var activeCount = outlookHelper.GetActiveRules().Count;
MessageBox.Show( MessageBox.Show(
$"Folder created/updated and emails moved successfully!\n\nTrackingID: {trackingId}", $"Rule created! Emails moved and monitoring started.\n\nTrackingID: {trackingId}\nActive rules: {activeCount}",
"Success", "Success",
MessageBoxButtons.OK, MessageBoxButtons.OK,
MessageBoxIcon.Information); MessageBoxIcon.Information);
@@ -87,7 +107,7 @@ namespace OutlookCaseHelper
if (success) if (success)
{ {
MessageBox.Show( MessageBox.Show(
"Rule removed successfully!\n\nFolder moved to: Inbox > Cases > Closed", "Rule removed!\n\nFolder moved to: Inbox > Cases > Closed\nMonitoring stopped.",
"Success", "Success",
MessageBoxButtons.OK, MessageBoxButtons.OK,
MessageBoxIcon.Information); MessageBoxIcon.Information);
@@ -108,7 +128,6 @@ namespace OutlookCaseHelper
} }
} }
// NOVO: Remove Rule pelo email selecionado no Outlook
private void RemoveRuleFromSelected_Click(object? sender, EventArgs e) private void RemoveRuleFromSelected_Click(object? sender, EventArgs e)
{ {
try try
@@ -128,7 +147,7 @@ namespace OutlookCaseHelper
if (success) if (success)
{ {
MessageBox.Show( MessageBox.Show(
$"Rule removed successfully!\n\nFolder moved to: Inbox > Cases > Closed\nTrackingID: {trackingId}", $"Rule removed!\n\nFolder moved to: Inbox > Cases > Closed\nTrackingID: {trackingId}\nMonitoring stopped.",
"Success", "Success",
MessageBoxButtons.OK, MessageBoxButtons.OK,
MessageBoxIcon.Information); MessageBoxIcon.Information);
@@ -155,6 +174,7 @@ namespace OutlookCaseHelper
private void Exit_Click(object? sender, EventArgs e) private void Exit_Click(object? sender, EventArgs e)
{ {
monitorTimer.Stop();
trayIcon.Visible = false; trayIcon.Visible = false;
Application.Exit(); Application.Exit();
} }
@@ -6,6 +6,15 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms> <UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<ApplicationIcon>bin\Release\net8.0-windows\casenew.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<Optimize>True</Optimize>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<Optimize>True</Optimize>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
@@ -38,4 +47,8 @@
</COMReference> </COMReference>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Content Include="bin\Release\net8.0-windows\casenew.ico" />
</ItemGroup>
</Project> </Project>
+159 -20
View File
@@ -1,6 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.IO;
using System.Text.Json;
using System.Windows.Forms;
using Outlook = Microsoft.Office.Interop.Outlook; using Outlook = Microsoft.Office.Interop.Outlook;
namespace OutlookCaseHelper namespace OutlookCaseHelper
@@ -9,13 +12,131 @@ namespace OutlookCaseHelper
{ {
private Outlook.Application outlookApp; private Outlook.Application outlookApp;
private Outlook.NameSpace outlookNamespace; private Outlook.NameSpace outlookNamespace;
private HashSet<string> activeRules = new HashSet<string>();
private readonly string rulesFilePath;
private Outlook.Items? inboxItems;
private Outlook.Items? sentItems;
public OutlookHelper() public OutlookHelper()
{ {
outlookApp = new Outlook.Application(); outlookApp = new Outlook.Application();
outlookNamespace = outlookApp.GetNamespace("MAPI"); outlookNamespace = outlookApp.GetNamespace("MAPI");
rulesFilePath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"OutlookCaseHelper",
"active_rules.json");
Directory.CreateDirectory(Path.GetDirectoryName(rulesFilePath)!);
LoadRules();
RegisterEmailEvents();
} }
private void RegisterEmailEvents()
{
try
{
Outlook.Folder? inboxFolder =
outlookNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder;
Outlook.Folder? sentFolder =
outlookNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail) as Outlook.Folder;
if (inboxFolder != null)
{
inboxItems = inboxFolder.Items;
inboxItems.ItemAdd += OnEmailReceived;
}
if (sentFolder != null)
{
sentItems = sentFolder.Items;
sentItems.ItemAdd += OnEmailSent;
}
}
catch { }
}
private void OnEmailReceived(object item)
{
try
{
if (activeRules.Count == 0) return;
if (item is not Outlook.MailItem mail) return;
if (mail.Subject == null) return;
foreach (var trackingId in activeRules)
{
if (mail.Subject.Contains($"TrackingID#{trackingId}"))
{
Outlook.Folder? inboxFolder =
outlookNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder;
if (inboxFolder == null) return;
Outlook.Folder casesFolder = GetOrCreateFolder(inboxFolder, "Cases");
Outlook.Folder activeFolder = GetOrCreateFolder(casesFolder, "Active");
Outlook.Folder trackingFolder = GetOrCreateFolder(activeFolder, trackingId);
mail.Move(trackingFolder);
break;
}
}
}
catch { }
}
private void OnEmailSent(object item)
{
try
{
if (activeRules.Count == 0) return;
if (item is not Outlook.MailItem mail) return;
if (mail.Subject == null) return;
foreach (var trackingId in activeRules)
{
if (mail.Subject.Contains($"TrackingID#{trackingId}"))
{
Outlook.Folder? inboxFolder =
outlookNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder;
if (inboxFolder == null) return;
Outlook.Folder casesFolder = GetOrCreateFolder(inboxFolder, "Cases");
Outlook.Folder activeFolder = GetOrCreateFolder(casesFolder, "Active");
Outlook.Folder trackingFolder = GetOrCreateFolder(activeFolder, trackingId);
mail.Move(trackingFolder);
break;
}
}
}
catch { }
}
private void LoadRules()
{
try
{
if (File.Exists(rulesFilePath))
{
var json = File.ReadAllText(rulesFilePath);
var rules = JsonSerializer.Deserialize<List<string>>(json);
if (rules != null)
activeRules = new HashSet<string>(rules);
}
}
catch { }
}
private void SaveRules()
{
try
{
var json = JsonSerializer.Serialize(new List<string>(activeRules));
File.WriteAllText(rulesFilePath, json);
}
catch { }
}
public HashSet<string> GetActiveRules() => activeRules;
public string? GetSelectedEmailTrackingId() public string? GetSelectedEmailTrackingId()
{ {
try try
@@ -29,7 +150,6 @@ namespace OutlookCaseHelper
Outlook.MailItem? email = selection[1] as Outlook.MailItem; Outlook.MailItem? email = selection[1] as Outlook.MailItem;
if (email == null) return null; if (email == null) return null;
// Aceita "TrackingID#1234567890" com qualquer número
Match match = Regex.Match(email.Subject, @"TrackingID#(\d+)"); Match match = Regex.Match(email.Subject, @"TrackingID#(\d+)");
if (match.Success) if (match.Success)
return match.Groups[1].Value; return match.Groups[1].Value;
@@ -51,16 +171,43 @@ namespace OutlookCaseHelper
Outlook.Folder activeFolder = GetOrCreateFolder(casesFolder, "Active"); Outlook.Folder activeFolder = GetOrCreateFolder(casesFolder, "Active");
Outlook.Folder trackingFolder = GetOrCreateFolder(activeFolder, trackingId); Outlook.Folder trackingFolder = GetOrCreateFolder(activeFolder, trackingId);
MoveEmailsWithTrackingId(inboxFolder, trackingId, trackingFolder); // Usa Restrict para mover apenas emails com o TrackingID
string filter = $"@SQL=\"urn:schemas:httpmail:subject\" LIKE '%TrackingID#{trackingId}%'";
Outlook.Items restricted = inboxFolder.Items.Restrict(filter);
var toMove = new List<Outlook.MailItem>();
foreach (object item in restricted)
{
if (item is Outlook.MailItem mail)
toMove.Add(mail);
}
foreach (var mail in toMove)
mail.Move(trackingFolder);
Outlook.Folder? sentFolder = Outlook.Folder? sentFolder =
outlookNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail) as Outlook.Folder; outlookNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail) as Outlook.Folder;
if (sentFolder != null) if (sentFolder != null)
MoveEmailsWithTrackingId(sentFolder, trackingId, trackingFolder); {
Outlook.Items restrictedSent = sentFolder.Items.Restrict(filter);
var toMoveSent = new List<Outlook.MailItem>();
foreach (object item in restrictedSent)
{
if (item is Outlook.MailItem mail)
toMoveSent.Add(mail);
}
foreach (var mail in toMoveSent)
mail.Move(trackingFolder);
}
activeRules.Add(trackingId);
SaveRules();
return true; return true;
} }
catch { return false; } catch (Exception ex)
{
MessageBox.Show($"Error: {ex.Message}", "Error");
return false;
}
} }
public bool RemoveRuleAndMoveToClosed(string trackingId) public bool RemoveRuleAndMoveToClosed(string trackingId)
@@ -83,11 +230,19 @@ namespace OutlookCaseHelper
Outlook.Folder closedFolder = GetOrCreateFolder(casesFolder, "Closed"); Outlook.Folder closedFolder = GetOrCreateFolder(casesFolder, "Closed");
trackingFolder.MoveTo(closedFolder); trackingFolder.MoveTo(closedFolder);
activeRules.Remove(trackingId);
SaveRules();
return true; return true;
} }
catch { return false; } catch { return false; }
} }
public void ProcessActiveRules()
{
// Mantido para compatibilidade mas os eventos tratam tudo em tempo real
}
private Outlook.Folder GetOrCreateFolder(Outlook.Folder parent, string name) private Outlook.Folder GetOrCreateFolder(Outlook.Folder parent, string name)
{ {
foreach (Outlook.Folder folder in parent.Folders) foreach (Outlook.Folder folder in parent.Folders)
@@ -107,21 +262,5 @@ namespace OutlookCaseHelper
} }
return null; return null;
} }
private void MoveEmailsWithTrackingId(Outlook.Folder source, string trackingId, Outlook.Folder dest)
{
var toMove = new List<Outlook.MailItem>();
foreach (object item in source.Items)
{
Outlook.MailItem? mail = item as Outlook.MailItem;
// Procura "TrackingID#" + os números no subject
if (mail != null && mail.Subject != null && mail.Subject.Contains($"TrackingID#{trackingId}"))
toMove.Add(mail);
}
foreach (var mail in toMove)
mail.Move(dest);
}
} }
} }
@@ -11,42 +11,49 @@
"SccLocalPath" = "8:" "SccLocalPath" = "8:"
"SccAuxPath" = "8:" "SccAuxPath" = "8:"
"SccProvider" = "8:" "SccProvider" = "8:"
"BackwardsCompatibleGUIDGeneration" = "8:TRUE"
"Hierarchy" "Hierarchy"
{ {
"Entry" "Entry"
{ {
"MsmKey" = "8:_3B183E44CB644AC2B8CFD4CE7E8493D5" "MsmKey" = "8:_1BB18DD6889F413ABD4B4D9154F1BE57"
"OwnerKey" = "8:_UNDEFINED" "OwnerKey" = "8:_UNDEFINED"
"MsmSig" = "8:_UNDEFINED" "MsmSig" = "8:_UNDEFINED"
} }
"Entry" "Entry"
{ {
"MsmKey" = "8:_5B02FA9AA2644553AA7EE8D7F202F0B4" "MsmKey" = "8:_230530C8E66442899F289ED7E541F937"
"OwnerKey" = "8:_UNDEFINED" "OwnerKey" = "8:_UNDEFINED"
"MsmSig" = "8:_UNDEFINED" "MsmSig" = "8:_UNDEFINED"
} }
"Entry" "Entry"
{ {
"MsmKey" = "8:_684A16145DD24AC28E88938986685418" "MsmKey" = "8:_40267211E716429082BE64E37E0BDD6D"
"OwnerKey" = "8:_UNDEFINED" "OwnerKey" = "8:_UNDEFINED"
"MsmSig" = "8:_UNDEFINED" "MsmSig" = "8:_UNDEFINED"
} }
"Entry" "Entry"
{ {
"MsmKey" = "8:_868FC254C3604138BDFCB4BF85E6CED6" "MsmKey" = "8:_54323F93E4284786AED71AEDEFC787E5"
"OwnerKey" = "8:_UNDEFINED" "OwnerKey" = "8:_UNDEFINED"
"MsmSig" = "8:_UNDEFINED" "MsmSig" = "8:_UNDEFINED"
} }
"Entry" "Entry"
{ {
"MsmKey" = "8:_FA25D952CE8540949268CBFE3D45EC23" "MsmKey" = "8:_99B87D0337064BF9AB53980132C343E6"
"OwnerKey" = "8:_UNDEFINED"
"MsmSig" = "8:_UNDEFINED"
}
"Entry"
{
"MsmKey" = "8:_FE396339D9AE492E9BE8136537F459C7"
"OwnerKey" = "8:_UNDEFINED" "OwnerKey" = "8:_UNDEFINED"
"MsmSig" = "8:_UNDEFINED" "MsmSig" = "8:_UNDEFINED"
} }
"Entry" "Entry"
{ {
"MsmKey" = "8:_UNDEFINED" "MsmKey" = "8:_UNDEFINED"
"OwnerKey" = "8:_684A16145DD24AC28E88938986685418" "OwnerKey" = "8:_40267211E716429082BE64E37E0BDD6D"
"MsmSig" = "8:_UNDEFINED" "MsmSig" = "8:_UNDEFINED"
} }
} }
@@ -106,6 +113,14 @@
"PrerequisitesLocation" = "2:1" "PrerequisitesLocation" = "2:1"
"Url" = "8:" "Url" = "8:"
"ComponentsUrl" = "8:" "ComponentsUrl" = "8:"
"Items"
{
"{EDC2488A-8267-493A-A98E-7D9C3B36CDF3}:.NETFramework,Version=v4.7.2"
{
"Name" = "8:Microsoft .NET Framework 4.7.2 (x86 and x64)"
"ProductCode" = "8:.NETFramework,Version=v4.7.2"
}
}
} }
} }
} }
@@ -146,80 +161,9 @@
} }
"File" "File"
{ {
"{1FB2D0AE-D3B9-43D4-B9DD-F88EC61E35DE}:_3B183E44CB644AC2B8CFD4CE7E8493D5" "{1FB2D0AE-D3B9-43D4-B9DD-F88EC61E35DE}:_1BB18DD6889F413ABD4B4D9154F1BE57"
{ {
"SourcePath" = "8:..\\OutlookCaseHelper\\bin\\Release\\net8.0-windows\\publish\\OutlookCaseHelper.pdb" "SourcePath" = "8:..\\OutlookCaseHelper\\bin\\Release\\net8.0-windows\\OutlookCaseHelper.exe"
"TargetName" = "8:OutlookCaseHelper.pdb"
"Tag" = "8:"
"Folder" = "8:_AC75D4F5EDF14629A9F064045458FC4F"
"Condition" = "8:"
"Transitive" = "11:FALSE"
"Vital" = "11:TRUE"
"ReadOnly" = "11:FALSE"
"Hidden" = "11:FALSE"
"System" = "11:FALSE"
"Permanent" = "11:FALSE"
"SharedLegacy" = "11:FALSE"
"PackageAs" = "3:1"
"Register" = "3:1"
"Exclude" = "11:FALSE"
"IsDependency" = "11:FALSE"
"IsolateTo" = "8:"
}
"{1FB2D0AE-D3B9-43D4-B9DD-F88EC61E35DE}:_5B02FA9AA2644553AA7EE8D7F202F0B4"
{
"SourcePath" = "8:..\\OutlookCaseHelper\\bin\\Release\\net8.0-windows\\publish\\OutlookCaseHelper.deps.json"
"TargetName" = "8:OutlookCaseHelper.deps.json"
"Tag" = "8:"
"Folder" = "8:_AC75D4F5EDF14629A9F064045458FC4F"
"Condition" = "8:"
"Transitive" = "11:FALSE"
"Vital" = "11:TRUE"
"ReadOnly" = "11:FALSE"
"Hidden" = "11:FALSE"
"System" = "11:FALSE"
"Permanent" = "11:FALSE"
"SharedLegacy" = "11:FALSE"
"PackageAs" = "3:1"
"Register" = "3:1"
"Exclude" = "11:FALSE"
"IsDependency" = "11:FALSE"
"IsolateTo" = "8:"
}
"{9F6F8455-1EF1-4B85-886A-4223BCC8E7F7}:_684A16145DD24AC28E88938986685418"
{
"AssemblyRegister" = "3:1"
"AssemblyIsInGAC" = "11:FALSE"
"AssemblyAsmDisplayName" = "8:OutlookCaseHelper, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL"
"ScatterAssemblies"
{
"_684A16145DD24AC28E88938986685418"
{
"Name" = "8:OutlookCaseHelper.dll"
"Attributes" = "3:512"
}
}
"SourcePath" = "8:..\\OutlookCaseHelper\\bin\\Release\\net8.0-windows\\publish\\OutlookCaseHelper.dll"
"TargetName" = "8:"
"Tag" = "8:"
"Folder" = "8:_AC75D4F5EDF14629A9F064045458FC4F"
"Condition" = "8:"
"Transitive" = "11:FALSE"
"Vital" = "11:TRUE"
"ReadOnly" = "11:FALSE"
"Hidden" = "11:FALSE"
"System" = "11:FALSE"
"Permanent" = "11:FALSE"
"SharedLegacy" = "11:FALSE"
"PackageAs" = "3:1"
"Register" = "3:1"
"Exclude" = "11:FALSE"
"IsDependency" = "11:FALSE"
"IsolateTo" = "8:"
}
"{1FB2D0AE-D3B9-43D4-B9DD-F88EC61E35DE}:_868FC254C3604138BDFCB4BF85E6CED6"
{
"SourcePath" = "8:..\\OutlookCaseHelper\\bin\\Release\\net8.0-windows\\publish\\OutlookCaseHelper.exe"
"TargetName" = "8:OutlookCaseHelper.exe" "TargetName" = "8:OutlookCaseHelper.exe"
"Tag" = "8:" "Tag" = "8:"
"Folder" = "8:_AC75D4F5EDF14629A9F064045458FC4F" "Folder" = "8:_AC75D4F5EDF14629A9F064045458FC4F"
@@ -237,9 +181,9 @@
"IsDependency" = "11:FALSE" "IsDependency" = "11:FALSE"
"IsolateTo" = "8:" "IsolateTo" = "8:"
} }
"{1FB2D0AE-D3B9-43D4-B9DD-F88EC61E35DE}:_FA25D952CE8540949268CBFE3D45EC23" "{1FB2D0AE-D3B9-43D4-B9DD-F88EC61E35DE}:_230530C8E66442899F289ED7E541F937"
{ {
"SourcePath" = "8:..\\OutlookCaseHelper\\bin\\Release\\net8.0-windows\\publish\\OutlookCaseHelper.runtimeconfig.json" "SourcePath" = "8:..\\OutlookCaseHelper\\bin\\Release\\net8.0-windows\\OutlookCaseHelper.runtimeconfig.json"
"TargetName" = "8:OutlookCaseHelper.runtimeconfig.json" "TargetName" = "8:OutlookCaseHelper.runtimeconfig.json"
"Tag" = "8:" "Tag" = "8:"
"Folder" = "8:_AC75D4F5EDF14629A9F064045458FC4F" "Folder" = "8:_AC75D4F5EDF14629A9F064045458FC4F"
@@ -257,6 +201,97 @@
"IsDependency" = "11:FALSE" "IsDependency" = "11:FALSE"
"IsolateTo" = "8:" "IsolateTo" = "8:"
} }
"{9F6F8455-1EF1-4B85-886A-4223BCC8E7F7}:_40267211E716429082BE64E37E0BDD6D"
{
"AssemblyRegister" = "3:1"
"AssemblyIsInGAC" = "11:FALSE"
"AssemblyAsmDisplayName" = "8:OutlookCaseHelper, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL"
"ScatterAssemblies"
{
"_40267211E716429082BE64E37E0BDD6D"
{
"Name" = "8:OutlookCaseHelper.dll"
"Attributes" = "3:512"
}
}
"SourcePath" = "8:..\\OutlookCaseHelper\\bin\\Release\\net8.0-windows\\OutlookCaseHelper.dll"
"TargetName" = "8:"
"Tag" = "8:"
"Folder" = "8:_AC75D4F5EDF14629A9F064045458FC4F"
"Condition" = "8:"
"Transitive" = "11:FALSE"
"Vital" = "11:TRUE"
"ReadOnly" = "11:FALSE"
"Hidden" = "11:FALSE"
"System" = "11:FALSE"
"Permanent" = "11:FALSE"
"SharedLegacy" = "11:FALSE"
"PackageAs" = "3:1"
"Register" = "3:1"
"Exclude" = "11:FALSE"
"IsDependency" = "11:FALSE"
"IsolateTo" = "8:"
}
"{1FB2D0AE-D3B9-43D4-B9DD-F88EC61E35DE}:_54323F93E4284786AED71AEDEFC787E5"
{
"SourcePath" = "8:..\\OutlookCaseHelper\\bin\\Release\\net8.0-windows\\OutlookCaseHelper.pdb"
"TargetName" = "8:OutlookCaseHelper.pdb"
"Tag" = "8:"
"Folder" = "8:_AC75D4F5EDF14629A9F064045458FC4F"
"Condition" = "8:"
"Transitive" = "11:FALSE"
"Vital" = "11:TRUE"
"ReadOnly" = "11:FALSE"
"Hidden" = "11:FALSE"
"System" = "11:FALSE"
"Permanent" = "11:FALSE"
"SharedLegacy" = "11:FALSE"
"PackageAs" = "3:1"
"Register" = "3:1"
"Exclude" = "11:FALSE"
"IsDependency" = "11:FALSE"
"IsolateTo" = "8:"
}
"{1FB2D0AE-D3B9-43D4-B9DD-F88EC61E35DE}:_99B87D0337064BF9AB53980132C343E6"
{
"SourcePath" = "8:..\\OutlookCaseHelper\\bin\\Release\\net8.0-windows\\OutlookCaseHelper.deps.json"
"TargetName" = "8:OutlookCaseHelper.deps.json"
"Tag" = "8:"
"Folder" = "8:_AC75D4F5EDF14629A9F064045458FC4F"
"Condition" = "8:"
"Transitive" = "11:FALSE"
"Vital" = "11:TRUE"
"ReadOnly" = "11:FALSE"
"Hidden" = "11:FALSE"
"System" = "11:FALSE"
"Permanent" = "11:FALSE"
"SharedLegacy" = "11:FALSE"
"PackageAs" = "3:1"
"Register" = "3:1"
"Exclude" = "11:FALSE"
"IsDependency" = "11:FALSE"
"IsolateTo" = "8:"
}
"{1FB2D0AE-D3B9-43D4-B9DD-F88EC61E35DE}:_FE396339D9AE492E9BE8136537F459C7"
{
"SourcePath" = "8:..\\OutlookCaseHelper\\bin\\Release\\net8.0-windows\\casenew.ico"
"TargetName" = "8:casenew.ico"
"Tag" = "8:"
"Folder" = "8:_AC75D4F5EDF14629A9F064045458FC4F"
"Condition" = "8:"
"Transitive" = "11:FALSE"
"Vital" = "11:TRUE"
"ReadOnly" = "11:FALSE"
"Hidden" = "11:FALSE"
"System" = "11:FALSE"
"Permanent" = "11:FALSE"
"SharedLegacy" = "11:FALSE"
"PackageAs" = "3:1"
"Register" = "3:1"
"Exclude" = "11:FALSE"
"IsDependency" = "11:FALSE"
"IsolateTo" = "8:"
}
} }
"FileType" "FileType"
{ {
@@ -323,17 +358,17 @@
"Product" "Product"
{ {
"Name" = "8:Microsoft Visual Studio" "Name" = "8:Microsoft Visual Studio"
"ProductName" = "8:CaseEmailHandler" "ProductName" = "8:Outlook Case Manager"
"ProductCode" = "8:{DC06A175-18E5-4D5E-8F04-6245F0C6B696}" "ProductCode" = "8:{D8A20294-9095-4817-808A-8B9144CD168F}"
"PackageCode" = "8:{EB395FB1-1C30-4195-B77B-07AF0FD08B31}" "PackageCode" = "8:{A2E06DA5-CC01-49D2-BD55-1A03E20DF29B}"
"UpgradeCode" = "8:{AAF43788-D89F-4285-AC74-CA5E1413DADC}" "UpgradeCode" = "8:{AAF43788-D89F-4285-AC74-CA5E1413DADC}"
"AspNetVersion" = "8:2.0.50727.0" "AspNetVersion" = "8:2.0.50727.0"
"RestartWWWService" = "11:FALSE" "RestartWWWService" = "11:FALSE"
"RemovePreviousVersions" = "11:FALSE" "RemovePreviousVersions" = "11:TRUE"
"DetectNewerInstalledVersion" = "11:TRUE" "DetectNewerInstalledVersion" = "11:TRUE"
"InstallAllUsers" = "11:FALSE" "InstallAllUsers" = "11:FALSE"
"ProductVersion" = "8:1.0.0" "ProductVersion" = "8:1.0.1"
"Manufacturer" = "8:Well" "Manufacturer" = "8:Wells"
"ARPHELPTELEPHONE" = "8:" "ARPHELPTELEPHONE" = "8:"
"ARPHELPLINK" = "8:" "ARPHELPLINK" = "8:"
"Title" = "8:OutlookCaseHelperSetup" "Title" = "8:OutlookCaseHelperSetup"
@@ -445,15 +480,15 @@
} }
"Shortcut" "Shortcut"
{ {
"{970C0BB2-C7D0-45D7-ABFA-7EC378858BC0}:_408AF6BD4D5C4E58B4F99B8856B2BF5E" "{970C0BB2-C7D0-45D7-ABFA-7EC378858BC0}:_ADACFD25B8814E05AC900592A062FAE2"
{ {
"Name" = "8:OutlookCaseHelper.exe" "Name" = "8:Outlook Case Helper"
"Arguments" = "8:" "Arguments" = "8:"
"Description" = "8:" "Description" = "8:"
"ShowCmd" = "3:1" "ShowCmd" = "3:1"
"IconIndex" = "3:0" "IconIndex" = "3:0"
"Transitive" = "11:FALSE" "Transitive" = "11:FALSE"
"Target" = "8:_868FC254C3604138BDFCB4BF85E6CED6" "Target" = "8:_1BB18DD6889F413ABD4B4D9154F1BE57"
"Folder" = "8:_F66DBC3EB3F247C29BAF8010BFB5C784" "Folder" = "8:_F66DBC3EB3F247C29BAF8010BFB5C784"
"WorkingFolder" = "8:_AC75D4F5EDF14629A9F064045458FC4F" "WorkingFolder" = "8:_AC75D4F5EDF14629A9F064045458FC4F"
"Icon" = "8:" "Icon" = "8:"
@@ -9,7 +9,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
ProjectSection(SolutionItems) = preProject ProjectSection(SolutionItems) = preProject
App.config = App.config App.config = App.config
OutlookCaseHelper\bin\Debug\net8.0-windows\casenew.ico = OutlookCaseHelper\bin\Debug\net8.0-windows\casenew.ico OutlookCaseHelper\bin\Debug\net8.0-windows\casenew.ico = OutlookCaseHelper\bin\Debug\net8.0-windows\casenew.ico
OutlookHelper.cs = OutlookHelper.cs
EndProjectSection EndProjectSection
EndProject EndProject
Project("{54435603-DBB4-11D2-8724-00A0C9A8B90C}") = "OutlookCaseHelperSetup", "OutlookCaseHelperSetup\OutlookCaseHelperSetup.vdproj", "{45984EC3-3682-760C-C1F0-4987692FB84A}" Project("{54435603-DBB4-11D2-8724-00A0C9A8B90C}") = "OutlookCaseHelperSetup", "OutlookCaseHelperSetup\OutlookCaseHelperSetup.vdproj", "{45984EC3-3682-760C-C1F0-4987692FB84A}"
@@ -25,7 +24,9 @@ Global
{6541931B-814B-42A0-8FD3-3F10FF401964}.Release|Any CPU.ActiveCfg = Release|Any CPU {6541931B-814B-42A0-8FD3-3F10FF401964}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6541931B-814B-42A0-8FD3-3F10FF401964}.Release|Any CPU.Build.0 = Release|Any CPU {6541931B-814B-42A0-8FD3-3F10FF401964}.Release|Any CPU.Build.0 = Release|Any CPU
{45984EC3-3682-760C-C1F0-4987692FB84A}.Debug|Any CPU.ActiveCfg = Debug {45984EC3-3682-760C-C1F0-4987692FB84A}.Debug|Any CPU.ActiveCfg = Debug
{45984EC3-3682-760C-C1F0-4987692FB84A}.Debug|Any CPU.Build.0 = Debug
{45984EC3-3682-760C-C1F0-4987692FB84A}.Release|Any CPU.ActiveCfg = Release {45984EC3-3682-760C-C1F0-4987692FB84A}.Release|Any CPU.ActiveCfg = Release
{45984EC3-3682-760C-C1F0-4987692FB84A}.Release|Any CPU.Build.0 = Release
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
+95 -52
View File
@@ -1,6 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.IO;
using System.Text.Json;
using System.Windows.Forms;
using Outlook = Microsoft.Office.Interop.Outlook; using Outlook = Microsoft.Office.Interop.Outlook;
namespace OutlookCaseHelper namespace OutlookCaseHelper
@@ -9,52 +12,81 @@ namespace OutlookCaseHelper
{ {
private Outlook.Application outlookApp; private Outlook.Application outlookApp;
private Outlook.NameSpace outlookNamespace; private Outlook.NameSpace outlookNamespace;
private HashSet<string> activeRules = new HashSet<string>();
private readonly string rulesFilePath;
public OutlookHelper() public OutlookHelper()
{ {
outlookApp = new Outlook.Application(); outlookApp = new Outlook.Application();
outlookNamespace = outlookApp.GetNamespace("MAPI"); outlookNamespace = outlookApp.GetNamespace("MAPI");
rulesFilePath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"OutlookCaseHelper",
"active_rules.json");
Directory.CreateDirectory(Path.GetDirectoryName(rulesFilePath)!);
LoadRules();
} }
// Get Tracking ID from selected email subject private void LoadRules()
{
try
{
if (File.Exists(rulesFilePath))
{
var json = File.ReadAllText(rulesFilePath);
var rules = JsonSerializer.Deserialize<List<string>>(json);
if (rules != null)
activeRules = new HashSet<string>(rules);
}
}
catch { }
}
private void SaveRules()
{
try
{
var json = JsonSerializer.Serialize(new List<string>(activeRules));
File.WriteAllText(rulesFilePath, json);
}
catch { }
}
public HashSet<string> GetActiveRules() => activeRules;
public string? GetSelectedEmailTrackingId() public string? GetSelectedEmailTrackingId()
{ {
try try
{ {
Outlook.Explorer activeExplorer = outlookApp.ActiveExplorer(); Outlook.Explorer activeExplorer = outlookApp.ActiveExplorer();
if (activeExplorer == null) if (activeExplorer == null) return null;
return null;
Outlook.Selection selection = activeExplorer.Selection; Outlook.Selection selection = activeExplorer.Selection;
if (selection.Count == 0) if (selection.Count == 0) return null;
return null;
Outlook.MailItem email = selection[1] as Outlook.MailItem; Outlook.MailItem? email = selection[1] as Outlook.MailItem;
if (email == null) if (email == null) return null;
return null;
// Subject pattern: "Something - TrackingID1234567890123456"
string pattern = @"TrackingID(\d+)";
Match match = Regex.Match(email.Subject, pattern);
Match match = Regex.Match(email.Subject, @"TrackingID#(\d+)");
if (match.Success) if (match.Success)
return match.Groups[1].Value; return match.Groups[1].Value;
return null; return null;
} }
catch catch { return null; }
{
return null;
}
} }
// Create folder and move all emails with this tracking id
public bool CreateFolderAndMoveEmails(string trackingId) public bool CreateFolderAndMoveEmails(string trackingId)
{ {
try try
{ {
Outlook.Folder inboxFolder = Outlook.Folder? inboxFolder =
outlookNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder; outlookNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder;
if (inboxFolder == null)
{
MessageBox.Show("Inbox folder is null", "Debug");
return false;
}
Outlook.Folder casesFolder = GetOrCreateFolder(inboxFolder, "Cases"); Outlook.Folder casesFolder = GetOrCreateFolder(inboxFolder, "Cases");
Outlook.Folder activeFolder = GetOrCreateFolder(casesFolder, "Active"); Outlook.Folder activeFolder = GetOrCreateFolder(casesFolder, "Active");
@@ -62,51 +94,78 @@ namespace OutlookCaseHelper
MoveEmailsWithTrackingId(inboxFolder, trackingId, trackingFolder); MoveEmailsWithTrackingId(inboxFolder, trackingId, trackingFolder);
Outlook.Folder sentFolder = Outlook.Folder? sentFolder =
outlookNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail) as Outlook.Folder; outlookNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail) as Outlook.Folder;
if (sentFolder != null) if (sentFolder != null)
MoveEmailsWithTrackingId(sentFolder, trackingId, trackingFolder); MoveEmailsWithTrackingId(sentFolder, trackingId, trackingFolder);
activeRules.Add(trackingId);
SaveRules();
MessageBox.Show($"Rules file: {rulesFilePath}\nRules count: {activeRules.Count}", "Debug");
return true; return true;
} }
catch catch (Exception ex)
{ {
MessageBox.Show($"Error in CreateFolderAndMoveEmails:\n{ex.Message}\n\n{ex.StackTrace}", "Debug Error");
return false; return false;
} }
} }
// Remove rule: move folder from Active to Closed
public bool RemoveRuleAndMoveToClosed(string trackingId) public bool RemoveRuleAndMoveToClosed(string trackingId)
{ {
try try
{ {
Outlook.Folder inboxFolder = Outlook.Folder? inboxFolder =
outlookNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder; outlookNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder;
if (inboxFolder == null) return false;
Outlook.Folder casesFolder = GetFolder(inboxFolder, "Cases"); Outlook.Folder? casesFolder = GetFolder(inboxFolder, "Cases");
if (casesFolder == null) return false; if (casesFolder == null) return false;
Outlook.Folder activeFolder = GetFolder(casesFolder, "Active"); Outlook.Folder? activeFolder = GetFolder(casesFolder, "Active");
if (activeFolder == null) return false; if (activeFolder == null) return false;
Outlook.Folder trackingFolder = GetFolder(activeFolder, trackingId); Outlook.Folder? trackingFolder = GetFolder(activeFolder, trackingId);
if (trackingFolder == null) return false; if (trackingFolder == null) return false;
Outlook.Folder closedFolder = GetOrCreateFolder(casesFolder, "Closed"); Outlook.Folder closedFolder = GetOrCreateFolder(casesFolder, "Closed");
MoveAllItemsFromFolder(trackingFolder, closedFolder); trackingFolder.MoveTo(closedFolder);
try activeRules.Remove(trackingId);
{ SaveRules();
activeFolder.Folders.Remove(trackingId);
}
catch { }
return true; return true;
} }
catch catch { return false; }
}
public void ProcessActiveRules()
{
if (activeRules.Count == 0) return;
try
{ {
return false; Outlook.Folder? inboxFolder =
outlookNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder;
if (inboxFolder == null) return;
Outlook.Folder? sentFolder =
outlookNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail) as Outlook.Folder;
Outlook.Folder casesFolder = GetOrCreateFolder(inboxFolder, "Cases");
Outlook.Folder activeFolder = GetOrCreateFolder(casesFolder, "Active");
foreach (var trackingId in activeRules)
{
Outlook.Folder trackingFolder = GetOrCreateFolder(activeFolder, trackingId);
MoveEmailsWithTrackingId(inboxFolder, trackingId, trackingFolder);
if (sentFolder != null)
MoveEmailsWithTrackingId(sentFolder, trackingId, trackingFolder);
}
} }
catch { }
} }
private Outlook.Folder GetOrCreateFolder(Outlook.Folder parent, string name) private Outlook.Folder GetOrCreateFolder(Outlook.Folder parent, string name)
@@ -116,8 +175,7 @@ namespace OutlookCaseHelper
if (folder.Name == name) if (folder.Name == name)
return folder; return folder;
} }
return (Outlook.Folder)parent.Folders.Add(name);
return parent.Folders.Add(name) as Outlook.Folder;
} }
private Outlook.Folder? GetFolder(Outlook.Folder parent, string name) private Outlook.Folder? GetFolder(Outlook.Folder parent, string name)
@@ -136,23 +194,8 @@ namespace OutlookCaseHelper
foreach (object item in source.Items) foreach (object item in source.Items)
{ {
Outlook.MailItem mail = item as Outlook.MailItem; Outlook.MailItem? mail = item as Outlook.MailItem;
if (mail != null && mail.Subject.Contains($"TrackingID{trackingId}")) if (mail != null && mail.Subject != null && mail.Subject.Contains($"TrackingID#{trackingId}"))
toMove.Add(mail);
}
foreach (var mail in toMove)
mail.Move(dest);
}
private void MoveAllItemsFromFolder(Outlook.Folder source, Outlook.Folder dest)
{
var toMove = new List<Outlook.MailItem>();
foreach (object item in source.Items)
{
Outlook.MailItem mail = item as Outlook.MailItem;
if (mail != null)
toMove.Add(mail); toMove.Add(mail);
} }