Click here to Skip to main content
15,892,737 members
Articles / Programming Languages / Visual Basic

Programmatically adding attachments to emails in C# and VB.NET

Rate me:
Please Sign up or sign in to vote.
4.82/5 (95 votes)
20 Apr 2007CPOL5 min read 943.7K   10K   145  
A technique for programmatically adding attachments to emails in C# and VB.NET.
Imports System
Imports System.Runtime.InteropServices
Imports System.IO

Namespace SendFileTo
    Class MAPI
        Private Const MAPI_LOGON_UI As Integer = &H1
        Private Const MAPI_DIALOG As Integer = &H8

        Public Shared Function SendMail(ByVal strAttachmentFileName As String, ByVal strSubject As String) As Integer
            Dim session As IntPtr = New IntPtr(0)
            Dim winhandle As IntPtr = New IntPtr(0)
            Dim msg As MapiMessage = New MapiMessage
            msg.subject = strSubject
            Dim sizeofMapiDesc As Integer = Marshal.SizeOf(GetType(MapiFileDesc))
            Dim pMapiDesc As IntPtr = Marshal.AllocHGlobal(sizeofMapiDesc)
            Dim fileDesc As MapiFileDesc = New MapiFileDesc
            fileDesc.position = -1
            Dim ptr As Integer = CType(pMapiDesc, Integer)
            Dim strPath As String = strAttachmentFileName
            fileDesc.name = Path.GetFileName(strPath)
            fileDesc.path = strPath
            Marshal.StructureToPtr(fileDesc, CType(ptr, IntPtr), False)
            msg.files = pMapiDesc
            msg.fileCount = 1
            Return MAPISendMail(session, winhandle, msg, MAPI_LOGON_UI Or MAPI_DIALOG, 0)
        End Function

        <DllImport("MAPI32.DLL")> _
        Private Shared Function MAPISendMail(ByVal sess As IntPtr, ByVal hwnd As IntPtr, ByVal message As MapiMessage, ByVal flg As Integer, ByVal rsv As Integer) As Integer
        End Function
    End Class

    <StructLayout(LayoutKind.Sequential)> _
    Public Class MapiMessage
        Public reserved As Integer
        Public subject As String
        Public noteText As String
        Public messageType As String
        Public dateReceived As String
        Public conversationID As String
        Public flags As Integer
        Public originator As IntPtr
        Public recipCount As Integer
        Public recips As IntPtr
        Public fileCount As Integer
        Public files As IntPtr
    End Class

    <StructLayout(LayoutKind.Sequential)> _
    Public Class MapiFileDesc
        Public reserved As Integer
        Public flags As Integer
        Public position As Integer
        Public path As String
        Public name As String
        Public type As IntPtr
    End Class
End Namespace

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
United Kingdom United Kingdom
When Dave isn't trying to play drums, fly helicopters or race cars, he can be found coding hard and herding cats at JoinIn Networks He must try harder!

You can read Dave's ramblings in his blog Aliens Ate My GUI

Or, if you can be bothered, he twitters on BoomerDave

Comments and Discussions