Dmitry Kostochko

HowTo: Exchange Data using the Outlook Attachments collection

Normally Outlook developers use the MailItem.UserProperties collection to send and receive some additional information with email messages. In this sample add-in for Microsoft Outlook, I suggest a slightly different approach – using the Attachments collection and such a powerful .NET feature as Serialization.

We will need a .NET class which we are going to serialize and send as attachment:

Public Class DataClass
    Public ID As String
    Public Name As String
    Public Discontinued As Boolean
    Public Count As Decimal
    Public [Date] As DateTime
End Class

In the OnClick event handler of a button located on a command bar or a ribbon tab of the Compose Inspector window, we create and initialize an instance of our class, serialize it and add to the Attachments collection:

Dim inspector As Outlook.Inspector = OutlookApp.ActiveInspector()
If inspector IsNot Nothing Then
    Try
        Dim item As Outlook.MailItem =
            DirectCast(inspector.CurrentItem, Outlook.MailItem)
        If item IsNot Nothing Then
            Try
                ' .NET object
                Dim obj As New DataClass()
                obj.ID = Guid.NewGuid().ToString()
                obj.Name = "My Name"
                obj.Discontinued = False
                obj.Count = 1000
                obj.[Date] = DateTime.Now
                ' serialization
                Dim TempFileTmp As String = Path.GetTempFileName()
                Dim TempFileXml As String = _
                    Path.ChangeExtension(TempFileTmp, "xml")
                If File.Exists(TempFileTmp) Then
                    File.Delete(TempFileTmp)
                End If
                Dim textWriter As TextWriter = New _
                    StreamWriter(TempFileXml)
                Dim serializer As New XmlSerializer(GetType(DataClass))
                serializer.Serialize(textWriter, obj)
                textWriter.Close()
                ' add to the Attachments collection
                item.Attachments.Add(TempFileXml, _
                    Outlook.OlAttachmentType.olByValue, _
                    Type.Missing, Type.Missing)
                File.Delete(TempFileXml)
            Finally
                Marshal.ReleaseComObject(item)
                item = Nothing
            End Try
        End If
    Finally
        Marshal.ReleaseComObject(inspector)
        inspector = Nothing
    End Try
End If

To display the received information, use an Advanced Outlook Form Region. In the ADXBeforeFormShow event handler, check if the current MailItem has any attachments, try to deserialize them, and in case the deserialization goes successfully, display the received data:

Dim success As Boolean = False
Dim inspector As Outlook.Inspector = _
    TryCast(InspectorObj, Outlook.Inspector)
If inspector IsNot Nothing Then
    Dim item As Outlook.MailItem =
        DirectCast(inspector.CurrentItem, Outlook.MailItem)
    If item IsNot Nothing Then
        Try
            If Not item.Sent Then
                Me.Visible = False
                Exit Sub
            End If
            If item.Attachments.Count > 0 Then
                For i As Integer = 1 To item.Attachments.Count
                    Dim TempFileTmp As String = Path.GetTempFileName()
                    Dim TempFileXml As String = _
                        Path.ChangeExtension(TempFileTmp, "xml")
                    If File.Exists(TempFileTmp) Then
                        File.Delete(TempFileTmp)
                    End If
                    item.Attachments.Item(i).SaveAsFile(TempFileXml)
                    Try
                        Dim newObj As AddinModule.DataClass
                        Dim fileStream As New _
                            FileStream(TempFileXml, FileMode.Open)
                        Try
                            Dim newSerialization As New _
                                XmlSerializer(_
                                    GetType(AddinModule.DataClass))
                            newObj = DirectCast(_
                                newSerialization.Deserialize(_
                                    fileStream), AddinModule.DataClass)
                            label1.Text = "ID = " & newObj.ID
                            label2.Text = "Name = " & newObj.Name
                            label3.Text = "Discontinued = " & _
                                newObj.Discontinued.ToString()
                            label4.Text = "Count = " & _
                                newObj.Count.ToString()
                            label5.Text = "Date = " & _
                                newObj.[Date].ToString()
                            success = True
                        Finally
                            fileStream.Close()
                        End Try
                    Catch
                        ' skip exception (serialization)
                    End Try
                    File.Delete(TempFileXml)
                    If success Then Exit For
                Next
            End If
            If Not success Then
                Me.Visible = False
                Exit Sub
            End If
        Finally
            Marshal.ReleaseComObject(item)
            item = Nothing
        End Try
    End If
End If

Here we are!

You may also be interested in:

How to program an Outlook COM add-in step-by-step

Available downloads:

This sample add-in was developed using Add-in Express 2008 for Microsoft Office and .net

C# sample Outlook add-in for VS 2005
VB.NET sample Outlook add-in for VS 2005

2 Comments

  • https://secure.gravatar.com/avatar/46dd0170b9152c31079284a41958994f?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G I Khan says:

    Really its very helpful. Though it was in VB.net, it helped a lot.
    Thanx.

    -I khan

  • https://secure.gravatar.com/avatar/ab4ec2858cfdf1e44dadf8c50fae314d?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Dmitry Kostochko (Add-in Express Team) says:

    This sample is also available in C#. Plese see the Available downloads section.

Post a comment

Have any questions? Ask us right now!