Ответ 1
Предварительные требования и настройка
- Windows 10 Pro
- Visual Studio 2013 Ultimate с разработкой Office
- Outlook 2013 с учетной записью электронной почты
Проект
- В Visual Studio выберите New Project → Visual С# → Office/SharePoint → Надстройки Office → Надстройка Outlook 2013
- Щелкните правой кнопкой мыши по проекту → Добавить → Элемент управления пользователя
-
Откройте "ThisAddIn.cs" и добавьте следующий код в метод "ThisAddIn_Startup":
var myCustomPane= this.CustomTaskPanes.Add(new UserControl1(), "My Pane"); myCustomPane.Visible = true;
Сообщения перетаскивания
- Дважды щелкните UserControl1 в обозревателе решений. Откроется окно конструктора.
-
В свойствах установите AllowDrop = True и подключите два обработчика событий DragDrop и DragEnter.
private void UserControl1_DragEnter(object sender, DragEventArgs e) { // if you want to read the message data as a string use this: if (e.Data.GetDataPresent(DataFormats.UnicodeText)) { e.Effect = DragDropEffects.Copy; } // if you want to read the whole .msg file use this: if (e.Data.GetDataPresent("FileGroupDescriptorW") && e.Data.GetDataPresent("FileContents")) { e.Effect = DragDropEffects.Copy; } } private void UserControl1_DragDrop(object sender, DragEventArgs e) { // to read basic info about the mail use this: var text = e.Data.GetData(DataFormats.UnicodeText).ToString(); var message = text.Split(new string[] { "\r\n" }, StringSplitOptions.None)[1]; var parts = message.Split('\t'); var from = parts[0]; // Email From var subject = parts[1]; // Email Subject var time = parts[2]; // Email Time // to get the .msg file contents use this: // credits to "George Vovos", http://stackoverflow.com/a/43577490/1093508 var outlookFile = e.Data.GetData("FileGroupDescriptor", true) as MemoryStream; if (outlookFile != null) { var dataObject = new iwantedue.Windows.Forms.OutlookDataObject(e.Data); var filenames = (string[])dataObject.GetData("FileGroupDescriptorW"); var filestreams = (MemoryStream[])dataObject.GetData("FileContents"); for (int fileIndex = 0; fileIndex < filenames.Length; fileIndex++) { string filename = filenames[fileIndex]; MemoryStream filestream = filestreams[fileIndex]; // do whatever you want with filestream, e.g. save to a file: string path = Path.GetTempPath() + filename; using (var outputStream = File.Create(path)) { filestream.WriteTo(outputStream); } } } }
Вы можете получить "iwantedue.Windows.Forms.OutlookDataObject" из CodeProject или вы можете использовать этот GitHub gist.