Custom X-Header to outgoing email

Add-in Express™ Support Service
That's what is more important than anything else

Custom X-Header to outgoing email
 
Alex Carter




Posts: 59
Joined: 2019-02-21
I am trying to add a custom X-Header to an outgoing email as in https://www.add-in-express.com/projects/olxheaderexample.zip. I have converted the code to VB as best I can, however I seem to be getting an overflow error when adding the header.

The below is the converted SetHeader method:


Private Sub SetHeader(ByVal itemObj As Object, ByVal headerName As String, ByVal _value As String)
        Dim mail As IMAPIProp = Nothing
        mail = TryCast(itemObj, IMAPIProp)

        If mail IsNot Nothing Then
            Dim p As IntPtr = IntPtr.Zero
            Dim lpszStr As IntPtr = IntPtr.Zero
            Dim propTags As IntPtr = IntPtr.Zero
            Dim lpszValue As IntPtr = IntPtr.Zero
            Dim lpProblems As IntPtr = IntPtr.Zero
            Dim lpPropValue As IntPtr = IntPtr.Zero
            Dim _mapiNameID As IntPtr = IntPtr.Zero
            Dim propValue As SPropValue = New SPropValue()
            Dim mapiNameID As MAPINAMEID = New MAPINAMEID()

            Try
                Dim g As Guid = New Guid(magicGUID)
                _mapiNameID = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(MAPINAMEID)))
                p = Marshal.AllocHGlobal(Marshal.SizeOf(g))
                Marshal.StructureToPtr(g, p, False)
                mapiNameID.lpguid = p
                mapiNameID.ulKind = MAPI.MNID_STRING
                lpszStr = Marshal.StringToHGlobalUni(headerName)
                mapiNameID.lpwstrName = lpszStr
                Marshal.StructureToPtr(mapiNameID, _mapiNameID, False)

                If mail.GetIDsFromNames(1, _mapiNameID, MAPI.MAPI_CREATE, propTags) = MAPI.S_OK Then
                    Dim count As Integer = Marshal.ReadInt32(propTags)

                    If count = 1 Then
                        [COLOR=red]Dim tag As UInteger = CUInt(Marshal.ReadInt32(New IntPtr(propTags.ToInt32() + 4)))[/COLOR]
                        tag = ((tag And &HFFFF0000) Or MAPI.PT_STRING8)
                        lpPropValue = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(SPropValue)))
                        propValue.ulPropTag = tag
                        propValue.dwAlignPad = 0
                        lpszValue = Marshal.StringToHGlobalAnsi(_value)
                        propValue.Value = lpszValue.ToInt64()
                        Marshal.StructureToPtr(propValue, lpPropValue, False)

                        If mail.SetProps(1, lpPropValue, lpProblems) = MAPI.S_OK Then

                            If mail.SaveChanges(MAPI.KEEP_OPEN_READWRITE) = MAPI.S_OK Then
                            End If
                        End If
                    End If
                End If

            Finally
                If lpszValue <> IntPtr.Zero Then Marshal.FreeHGlobal(lpszValue)
                If lpPropValue <> IntPtr.Zero Then Marshal.DestroyStructure(lpPropValue, GetType(SPropValue))
                If propTags <> IntPtr.Zero Then MAPI.MAPIFreeBuffer(propTags, HBitness)
                If p <> IntPtr.Zero Then Marshal.DestroyStructure(p, GetType(Guid))
                If lpszStr <> IntPtr.Zero Then Marshal.FreeHGlobal(lpszStr)
                If _mapiNameID <> IntPtr.Zero Then Marshal.DestroyStructure(_mapiNameID, GetType(MAPINAMEID))
            End Try
        End If
    End Sub


I have highlighted the row causing the issue in red, the below is the trace from the error:

System.OverflowException
HResult=0x80131516
Message=Arithmetic operation resulted in an overflow.
Source=eFiles
StackTrace:
at eFiles.AddinModule.SetHeader(Object itemObj, String headerName, String _value) in C:\Projects\eFiles-Office-Integration\eFiles\eFiles\AddinModule.vb:line 1777

Any help is much appreciated!
Posted 25 Feb, 2019 09:28:23 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
Hello Alex,

Please use the approach described at https://www.add-in-express.com/forum/read.php?FID=5&TID=14156.


Andrei Smolin
Add-in Express Team Leader
Posted 25 Feb, 2019 09:35:12 Top
Alex Carter




Posts: 59
Joined: 2019-02-21
Hi Andrei,
That works perfectly! Thanks the swift and helpful reply!
Posted 25 Feb, 2019 10:17:42 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
You are welcome!


Andrei Smolin
Add-in Express Team Leader
Posted 25 Feb, 2019 10:19:24 Top