How to Drag and Drop Outlook Mail Items in VB.NET

We have run across the need to drag and drop Outlook Mail Messages into a Windows Forms .NET application and after looking for a proper method, we have found out it was a lot easier to drag and drop attachments found in a mail message than the actual text of a mail message. After a lot of thinking, we cracked the solution and decided to share it with the rest of the world, in case they have to do something similar.
I assume you all know about the dragEnter and DragDrop methods so I shall not dwell into too much detail.

Here’s the function:

Private Sub lstLocalFiles_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lstLocalFiles.DragDrop
‘copy file(s) to destination folder
Dim destinationPath As String = “C:Temp”

If Not e.Data.GetDataPresent(DataFormats.Text) = False Then
‘check if this is an outlook message. The outlook messages, all contain a FileContents attribute. If not, exit.
Dim formats() As String = e.Data.GetFormats()

f formats.Contains(“FileContents”) = False Then Exit Sub

‘they are dragging the attachment
If (e.Data.GetDataPresent(“Object Descriptor”)) Then
Dim app As New Microsoft.Office.Interop.Outlook.Application() ‘ // get current selected items
Dim selection As Microsoft.Office.Interop.Outlook.Selection
Dim myText As String = “”
selection = app.ActiveExplorer.Selection
If selection IsNot Nothing Then


Dim mailItem As Outlook._MailItem
For i As Integer = 0 To selection.Count – 1
Dim strFilename As String = “”
                        mailItem = TryCast(selection.Item(i + 1), Outlook._MailItem)
If mailItem IsNot Nothing Then
Try
myText = “”
strFilename = mailItem.Subject + ” ”
strFileName += mailItem.ReceivedTime.ToShortDateString + ” ” +                 mailItem.ReceivedTime.ToShortTimeString + “.txt”
myText = e.Data.GetData(“Text”)  ‘header text
myText += vbCrLf + vbCrLf
myText += mailItem.Body  ‘Plain Text Body Message


For Each _Chr As Char In Path.GetInvalidFileNameChars()
strFileName = strFileName.Replace(_Chr, “_”)
Next


Dim attachment_count As Integer = 1
‘now save the attachments with the same file name and then 1,2,3 next to it
For Each att As Attachment In mailItem.Attachments
Dim att_filename As String = att.FileName
Dim att_extension As String = att_filename.Substring(att_filename.LastIndexOf(“.”))
att.SaveAsFile(destinationPath + strFileName.Replace(“.txt”, CStr(attachment_count) + att_extension))


myText += vbCrLf + “Attachment ” + CStr(attachment_count) + ” : ” + att_filename


attachment_count += 1
Next
 ‘save the mail message

Dim strw As New StreamWriter(destinationPath + strFileName, False)
strw.WriteLine(myText)
strw.Close()
strw.Dispose()
Catch ex As System.Exception
Finally
Marshal.ReleaseComObject(mailItem)
End Try

End If
Next
End If

End If
End If

End Sub
You need to include Office Interop Outlook into your project references.
If you ever need something like dragging and dropping Outlook Mail Messages into your application, give us a call or contact us on our website: http://www.carra-lucia-ltd.co.uk

Advertisements