Skip to content

Commit

Permalink
add log4net and fix missing updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ahaenggli committed Oct 23, 2020
1 parent a919e3c commit 6295c64
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 234 deletions.
2 changes: 1 addition & 1 deletion AddIn/AboutForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs

private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
ProcessStartInfo website = new ProcessStartInfo(@"https://github.com/ahaenggli/OutlookAddIn_KeepAttachmentsOnReply");
ProcessStartInfo website = new ProcessStartInfo(@"https://github.com/ahaenggli/OutlookAddIn_KeepAttachmentsOnReply#readme");
Process.Start(website);
}
}
Expand Down
11 changes: 10 additions & 1 deletion AddIn/OutlookAddIn_KeepAttachmentsOnReply.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<PublishUrl>\\asyn\web\PlugIn\outlook\</PublishUrl>
<InstallUrl />
<TargetCulture>de</TargetCulture>
<ApplicationVersion>1.0.7.0</ApplicationVersion>
<ApplicationVersion>1.0.8.1</ApplicationVersion>
<AutoIncrementApplicationRevision>false</AutoIncrementApplicationRevision>
<UpdateEnabled>true</UpdateEnabled>
<UpdateInterval>0</UpdateInterval>
Expand Down Expand Up @@ -123,11 +123,16 @@
-->
<ItemGroup>
<Reference Include="Accessibility" />
<Reference Include="log4net, Version=2.0.11.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
<HintPath>..\packages\log4net.2.0.11\lib\net45\log4net.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Data" />
<Reference Include="System.Deployment" />
<Reference Include="System.Drawing" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.Web" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="System.Core" />
Expand Down Expand Up @@ -205,6 +210,10 @@
</EmbeddedResource>
<None Include="app.config" />
<None Include="KeepAttachmentsOnReply_TemporaryKey.pfx" />
<Content Include="log4net.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down
109 changes: 105 additions & 4 deletions AddIn/OutlookExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
using Microsoft.Office.Interop.Outlook;
using System.IO;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace KeepAttachmentsOnReply
{
public static class OutlookExtensions
{
private static readonly log4net.ILog _logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

// signed / crypted
private static string PR_SECURITY_FLAGS = @"http://schemas.microsoft.com/mapi/proptag/0x6E010003";
private static string AttachmentFlags = @"http://schemas.microsoft.com/mapi/proptag/0x37140003";
private static readonly string PR_SECURITY_FLAGS = @"http://schemas.microsoft.com/mapi/proptag/0x6E010003";
private static readonly string AttachmentFlags = @"http://schemas.microsoft.com/mapi/proptag/0x37140003";

public static void Unsign(this MailItem mailItem)
{
Expand Down Expand Up @@ -42,8 +46,6 @@ public static bool IsEmbedded(this Attachment attachment)

return true;
}


public static int CountNonEmbeddedAttachments(this Attachments attachments)
{
int cnt = 0;
Expand All @@ -62,5 +64,104 @@ public static int CountNonEmbeddedAttachments(this Attachments attachments)
return cnt;
}

/// <summary>
/// Parse an opend item if it is a mail
/// </summary>
public static void ParseItemForAttachments(object item)
{
if (item == null) return;

// is it a mail?
if (!(item is Outlook.MailItem)) return;

// cast to MailItem
if (!(item is MailItem mailItem)) return;

Application app = Globals.ThisAddIn.Application;

// it is a new one
if (mailItem.EntryID == null && mailItem.Sent == false)
{
// is it a reply?
bool isReply = !string.IsNullOrEmpty(mailItem.To) || mailItem.Recipients.Count > 0;
// it is!
if (isReply)
{
// keep attachments from original mail
// get selected item (reply is open, so this should be the original mail)
MailItem src = null;
if (app.ActiveWindow() is Outlook.Explorer)
{
//Debug.WriteLine("Explorer");
if (app.ActiveExplorer().Selection.Count == 1)
{
object selectedItem = app.ActiveExplorer().Selection[1];
if (selectedItem is Outlook.MailItem)
{
src = selectedItem as Outlook.MailItem;
}
}
}
else
if (app.ActiveWindow() is Outlook.Inspector)
{
//Debug.WriteLine("Inspector");
object selectedItem = app.ActiveInspector().CurrentItem;
if (selectedItem is Outlook.MailItem)
{
src = selectedItem as Outlook.MailItem;
}
}

if (src != null)
mailItem.CopyAttachmentsFrom(src);

}
}
}

/// <summary>
/// Copy attachments from original mail to the reply
/// </summary>
public static void CopyAttachmentsFrom(this MailItem newMail, MailItem attFrom)
{
// is it a mail?
if (attFrom is Outlook.MailItem)
{
// cast to MailItem.
MailItem mailItem = attFrom;

// any attachments?
if (mailItem.Attachments.CountNonEmbeddedAttachments() > 0)
{
// iterate attachments
foreach (Attachment attachment in mailItem.Attachments)
{
if (!attachment.IsEmbedded())
{
// does our temp dir exists? if not -> create
string tmpDir = Path.GetTempPath() + "OutlookAddIn_KeepAttachmentsOnReply" + Path.DirectorySeparatorChar.ToString();
if (!Directory.Exists(tmpDir)) Directory.CreateDirectory(tmpDir);

// generate a temp path
string tmp = tmpDir + attachment.FileName;

// save file to tmp
attachment.SaveAsFile(tmp);

// add tmp file as new attachment to the reply mail
newMail.Attachments.Add(tmp, Outlook.OlAttachmentType.olByValue, 1, attachment.DisplayName);

// save replay mail
//newMail.Save();

// delete tmp file
File.Delete(tmp);
}
}
}
}
}

}
}
5 changes: 3 additions & 2 deletions AddIn/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
// übernehmen, indem Sie "*" eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.7.0")]
[assembly: AssemblyFileVersion("1.0.7.0")]
[assembly: AssemblyVersion("1.0.8.1")]
[assembly: AssemblyFileVersion("1.0.8.1")]

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config")]
81 changes: 39 additions & 42 deletions AddIn/Ribbon.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Microsoft.Office.Interop.Outlook;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
Expand All @@ -16,9 +15,9 @@ namespace KeepAttachmentsOnReply
public class Ribbon : Office.IRibbonExtensibility
{

private Office.IRibbonUI ribbon;
private static readonly log4net.ILog _logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

private void getAttachmentsFromConversation(MailItem mailItem, string src)
private void GetAttachmentsFromConversation(MailItem mailItem)
{
if (mailItem == null)
{
Expand All @@ -45,9 +44,9 @@ private void getAttachmentsFromConversation(MailItem mailItem, string src)
// Obtain Table that contains rows
// for each item in the conversation.
Outlook.Table table = conv.GetTable();
Debug.WriteLine("Conversation Items Count: " + table.GetRowCount().ToString());
_logger.Debug("Conversation Items Count: " + table.GetRowCount().ToString());
_logger.Debug("Conversation Items from Root:");

Debug.WriteLine("Conversation Items from Root:");
// Obtain root items and enumerate the conversation.
Outlook.SimpleItems simpleItems = conv.GetRootItems();
foreach (object item in simpleItems)
Expand All @@ -60,9 +59,9 @@ private void getAttachmentsFromConversation(MailItem mailItem, string src)
Outlook.MailItem mail = item as Outlook.MailItem;
Outlook.Folder inFolder = mail.Parent as Outlook.Folder;
string msg = mail.Subject + " in folder [" + inFolder.Name + "] EntryId [" + (mail.EntryID.ToString() ?? "NONE") + "]";
Debug.WriteLine(msg);
Debug.WriteLine(mail.Sender);
Debug.WriteLine(mail.ReceivedByEntryID);
_logger.Debug(msg);
_logger.Debug(mail.Sender);
_logger.Debug(mail.ReceivedByEntryID);

if (mail.EntryID != null && (mail.Sender != null || mail.ReceivedByEntryID != null))
{
Expand All @@ -82,7 +81,7 @@ private void getAttachmentsFromConversation(MailItem mailItem, string src)

if (it.Attachments.CountNonEmbeddedAttachments() > 0)
{
//Debug.WriteLine(it.Attachments.CountNonEmbeddedAttachments());
//_logger.Debug(it.Attachments.CountNonEmbeddedAttachments());

try
{
Expand All @@ -102,7 +101,7 @@ private void getAttachmentsFromConversation(MailItem mailItem, string src)
break;
}

ThisAddIn.addParentAttachments(mailItem, it);
mailItem.CopyAttachmentsFrom(it);
mailItem.Save();
}
catch (System.Exception ex)
Expand All @@ -117,10 +116,8 @@ private void getAttachmentsFromConversation(MailItem mailItem, string src)
st.Clear();

Marshal.ReleaseComObject(mailItem);
mailItem = null;
}


private void EnumerateConversation(System.Collections.Generic.Stack<MailItem> st, object item, Outlook.Conversation conversation)
{
Outlook.SimpleItems items =
Expand All @@ -138,9 +135,9 @@ private void EnumerateConversation(System.Collections.Generic.Stack<MailItem> st
Outlook.Folder inFolder = mailItem.Parent as Outlook.Folder;

string msg = mailItem.Subject + " in folder [" + inFolder.Name + "] EntryId [" + (mailItem.EntryID.ToString() ?? "NONE") + "]";
Debug.WriteLine(msg);
Debug.WriteLine(mailItem.Sender);
Debug.WriteLine(mailItem.ReceivedByEntryID);
_logger.Debug(msg);
_logger.Debug(mailItem.Sender);
_logger.Debug(mailItem.ReceivedByEntryID);

if (mailItem.EntryID != null && (mailItem.Sender != null || mailItem.ReceivedByEntryID != null))
{
Expand All @@ -153,23 +150,43 @@ private void EnumerateConversation(System.Collections.Generic.Stack<MailItem> st
}
}

public Ribbon()
{
}

#region IRibbonExtensibility-Member

public string GetCustomUI(string ribbonID)
{
return GetResourceText("KeepAttachmentsOnReply.Ribbon.xml");
}

#endregion

#region Menübandrückrufe
//Erstellen Sie hier Rückrufmethoden.
//Weitere Informationen zum Hinzufügen von Rückrufmethoden finden Sie unter https://go.microsoft.com/fwlink/?LinkID=271226.

public void Ribbon_Load(Office.IRibbonUI ribbonUI)
{
}

public void OnInfo(Office.IRibbonControl e)
{
Form frm = new AboutForm();
frm.ShowDialog();
frm.Close();
frm.Dispose();
}

public void OnFindMissingAttachments(Office.IRibbonControl e)
{
List<MailItem> mailItems = new List<MailItem>();
Inspector m = e.Context as Inspector;
string src = "";

// single email opend
if (m != null)
if (e.Context is Inspector m)
{
mailItems.Add(m.CurrentItem as MailItem);
src = "mail";
}
else
{
Expand All @@ -178,7 +195,6 @@ public void OnFindMissingAttachments(Office.IRibbonControl e)
if (selectedItem is Outlook.MailItem)
{
mailItems.Add(selectedItem as Outlook.MailItem);
src = "explorer";
}
}
}
Expand All @@ -194,9 +210,10 @@ public void OnFindMissingAttachments(Office.IRibbonControl e)
break;
}

//if (mailitem.Attachments.Count > 0) return;
getAttachmentsFromConversation(mailItem, src);
// if (mailitem.Attachments.Count > 0) return;
GetAttachmentsFromConversation(mailItem);

// Abbruch-Bedingung
if (mailItems.Count > 1 && mailItems.First().Equals(mailItem))
{
if (MessageBox.Show(null, "Es wurden mehrere Nachrichten ausgewählt. " + Environment.NewLine + "Sollen die Anhänge für alle " +
Expand All @@ -211,26 +228,6 @@ public void OnFindMissingAttachments(Office.IRibbonControl e)

mailItems.Clear();
}
public Ribbon()
{
}

#region IRibbonExtensibility-Member

public string GetCustomUI(string ribbonID)
{
return GetResourceText("KeepAttachmentsOnReply.Ribbon.xml");
}

#endregion

#region Menübandrückrufe
//Erstellen Sie hier Rückrufmethoden. Weitere Informationen zum Hinzufügen von Rückrufmethoden finden Sie unter https://go.microsoft.com/fwlink/?LinkID=271226.

public void Ribbon_Load(Office.IRibbonUI ribbonUI)
{
ribbon = ribbonUI;
}

#endregion

Expand Down
6 changes: 3 additions & 3 deletions AddIn/Ribbon.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<group id="group1" label="Anhänge">
<button id="button1" onAction="OnFindMissingAttachments" label="Fehlende ermitteln" showImage="true" imageMso="GroupAttach" size="large" />
<buttonGroup id="btnGrp1">
<button id="btn_info1" onAction="OnInfo" label="Info" showLabel="false" showImage="true" imageMso="ControlToolboxOutlook" />
<button id="btn_info1" onAction="OnInfo" label="Info" showLabel="false" showImage="true" imageMso="Info" />
</buttonGroup>
</group>
</tab>
Expand All @@ -24,7 +24,7 @@
<group id="group3" label="Anhänge">
<button id="button3" onAction="OnFindMissingAttachments" label="Fehlende ermitteln" showImage="true" imageMso="GroupAttach" size="large" />
<buttonGroup id="btnGrp3">
<button id="btn_info3" onAction="OnInfo" label="Info" showLabel="false" showImage="true" imageMso="ControlToolboxOutlook" />
<button id="btn_info3" onAction="OnInfo" label="Info" showLabel="false" showImage="true" imageMso="Info" />
</buttonGroup>
</group>
</tab>
Expand All @@ -35,7 +35,7 @@
<group id="group4" label="Anhänge">
<button id="button4" onAction="OnFindMissingAttachments" label="Fehlende ermitteln" showImage="true" imageMso="GroupAttach" size="large" />
<buttonGroup id="btnGrp4">
<button id="btn_info4" onAction="OnInfo" label="Info" showLabel="false" showImage="true" imageMso="ControlToolboxOutlook" />
<button id="btn_info4" onAction="OnInfo" label="Info" showLabel="false" showImage="true" imageMso="Info" />
</buttonGroup>
</group>
</tab>
Expand Down
Loading

0 comments on commit 6295c64

Please sign in to comment.