Hallo Leute,
in diesem Thread ein wenig Code, der zeigt wie man Anhänge aus gerade erstellten E-Mails in Outlook extrahiert.
Ist dann sinnvoll, oder von Nutzen, wenn man diese Dateien Beispielsweise verschlüsseln möchte vor dem Senden.
Zu beachten ist, dass pre PropertyAccessor in C#, nur relativ kleine Anhänge extrahiert werden können.
Daher gibt es im folgenden Code die Funktion GetLargeBytes(), welche keinen MAPI Proptag verwendet um an die Bytes des Anhangs zu kommen.
Große Anhänge können, meines wissens nach, nur mit extended MAPI extrahiert werden, was nur in C++ und Delphi möglich ist.
Klasse zum extrahieren der Anhänge:
Spoiler anzeigen
Erstelle E-Mail bekommen in Klasse welche sich im Projekt von ThisAddIn befindet:
Spoiler anzeigen
Erstelle E-Mail bekommen in custom Ribbon:
Spoiler anzeigen
in diesem Thread ein wenig Code, der zeigt wie man Anhänge aus gerade erstellten E-Mails in Outlook extrahiert.
Ist dann sinnvoll, oder von Nutzen, wenn man diese Dateien Beispielsweise verschlüsseln möchte vor dem Senden.
Zu beachten ist, dass pre PropertyAccessor in C#, nur relativ kleine Anhänge extrahiert werden können.
Daher gibt es im folgenden Code die Funktion GetLargeBytes(), welche keinen MAPI Proptag verwendet um an die Bytes des Anhangs zu kommen.
Große Anhänge können, meines wissens nach, nur mit extended MAPI extrahiert werden, was nur in C++ und Delphi möglich ist.
Klasse zum extrahieren der Anhänge:
C#-Quellcode
- using Outlook = Microsoft.Office.Interop.Outlook;
- /// <summary>
- /// Class to receive and add Outlook Attachments
- /// </summary>
- public static class AttachmentManager
- {
- /// <summary>
- /// Gets an attachment as byte array
- /// </summary>
- /// <param name="mailItem"><see cref="Outlook.MailItem"/> which contains the attachment</param>
- /// <param name="fileName">Filename of the desired attachment</param>
- /// <returns>Returns the specific attachment as byte array</returns>
- public static byte[] GetAttachment(Outlook.MailItem mailItem, string fileName)
- {
- if (mailItem == null) return new byte[0];
- if (mailItem.Attachments.Count <= 0) return new byte[0];
- if (string.IsNullOrEmpty(fileName)) return new byte[0];
- /* MAPI propertytag name (not a actual website) */
- const string PR_ATTACH_DATA_BIN = "http://schemas.microsoft.com/mapi/proptag/0x37010102";
- /* Index of found attachment incase that it is too big for property accessor. */
- int idx = -1;
- try
- {
- for (int i=1; i<=mailItem.Attachments.Count; i++)
- {
- if (mailItem.Attachments[i].FileName == fileName)
- {
- idx = i;
- /* Get attachment as file array via PropertyAcessor and PR_ATTACH_DATA_BIN tag.
- ** https://docs.microsoft.com/en-us/previous-versions/exchange-server/exchange-10/ms526356(v=exchg.10)
- ** Will fail if attachment exceeds certain size if using normal MAPI.
- ** Large files require extended MAPI which only works with C++ and Delphi. */
- return mailItem.Attachments[i].PropertyAccessor.GetProperty(PR_ATTACH_DATA_BIN);
- }
- }
- return new byte[0];
- }
- catch (System.Runtime.InteropServices.COMException)
- {
- if (idx < 1) return new byte[0];
- /* If file was too big for PropertyAccessor call GetLargetBytes() function. */
- return GetLargeBytes(mailItem.Attachments[idx]);
- }
- catch
- {
- return new byte[0];
- }
- }
- /*
- ** Function to get large attachments as byte array without using PropertyAccessor and MAPI tag.
- ** Will be heavy on filesystem if used extensively. Writes, reades and deletes files.
- **
- ** Returns file as byte array
- ** Returns émpty byte array if any exception was thrown
- */
- private static byte[] GetLargeBytes(Outlook.Attachment attachment)
- {
- byte[] result = null;
- try
- {
- string path = System.IO.Path.Combine(
- System.IO.Path.GetTempPath(),
- System.IO.Path.GetTempFileName());
- /* Save attachment on disk with generated TEMP path and name*/
- attachment.SaveAsFile(path);
- /* Load saved attachment into FileStream with 4k buffersize.
- ** I recommend keeping that buffersize since I/O speed is dominated by disk drive and
- ** CPU cache can easily hold that much. The .net default buffersize is 4k but
- ** in .net 4.5 they increased it to 80k which is a bit much in my opinion... */
- using (var mem = new System.IO.MemoryStream())
- using (var stream = new System.IO.FileStream(
- path,
- System.IO.FileMode.Open,
- System.IO.FileAccess.Read,
- System.IO.FileShare.None,
- 0x1000))
- {
- stream.CopyTo(mem);
- result = mem.ToArray();
- }
- System.IO.File.Delete(path);
- return result;
- }
- catch
- {
- return new byte[0];
- }
- }
- }
Erstelle E-Mail bekommen in Klasse welche sich im Projekt von ThisAddIn befindet:
C#-Quellcode
- using Outlook = Microsoft.Office.Interop.Outlook;
- public class Some_Class_In_Namespace_of_ThisAddin
- {
- /*
- ** Function to get the top most MailItem from current Outlook instance.
- ** Uses the Outlook application instance in which the AddIn runs.
- ** Uses the Microsoft.Office.Interop.Outlook.ActiveInspector object to get the Mail.
- **
- ** Returns MailItem if there is one
- ** Returns null if anything failed
- */
- private Outlook.MailItem GetMailItem()
- {
- var olApp = (Outlook.Application)Globals.ThisAddIn.Application;
- if (olApp == null) return null;
- var olInp = (Outlook.Inspector)olApp.ActiveInspector();
- if (olInp == null) return null;
- var olMal = (Outlook.MailItem)olInp.CurrentItem;
- return olMal;
- }
- /*
- ** Function to get a MailItem which is currently beeing edited (like a response MailItem).
- ** Uses the Outlook application instance in which the AddIn runs.
- ** Uses the Microsoft.Office.Interop._Explorer.ActiveInlineResponse object to get the Mail.
- **
- ** Returns MailItem if there is one
- ** Returns null if anything failed
- */
- private Outlook.MailItem GetResponseItem()
- {
- var olApp = (Outlook.Application)Globals.ThisAddIn.Application;
- if (olApp == null) return null;
- var olExplorer = (Outlook.Explorer)olApp.ActiveExplorer();
- if (olExplorer == null) return null;
- var olInlineResponse = (Outlook.MailItem)olExplorer.ActiveInlineResponse;
- return olInlineResponse;
- }
- }
Erstelle E-Mail bekommen in custom Ribbon:
C#-Quellcode
- using Outlook = Microsoft.Office.Interop.Outlook;
- using Core = Microsoft.Office.Core;
- public Ribbon_Example : Core.IRibbonExtensibility
- {
- /*
- ** Callback of Ribbon Button to get current compose MailItem
- */
- public void Btn_GetCompose_Click(Core.IRibbonControl control)
- {
- var composeMailItem = GetResponseMailItem(control);
- }
- /*
- ** Callback of Ribbon Button to get newly created MailItem
- */
- public void Btn_GetNew_Click(Core.IRibbonControl control)
- {
- var newMailItem = GetMailItem(control);
- }
- /*
- ** Function to get the topmost mail item from current inspector
- */
- private Outlook.MailItem GetMailItem(Core.IRibbonControl control)
- {
- if (!(control.Context is Outlook.Inspector item)) return null;
- var mailItem = item.CurrentItem as Outlook.MailItem;
- return mailItem;
- }
- /*
- ** Function to get topmost composing mail item from current explorer
- */
- private Outlook.MailItem GetResponseMailItem(Core.IRibbonControl control)
- {
- if (!(control.Context is Outlook.Explorer item)) return null;
- var response = item.ActiveInlineResponse as Outlook.MailItem;
- return response;
- }
- }