Help => Array in Function MAPISendMail

Hello !!

It works fine with just one message attatched or RecipCount <=1 but...
If .FileCount > 1 or .RecipCount > 1 in the Array MAPI generate an error (Nr 2) MAPI_E_FAILURE
Parts of my code:
    'More code before this....
    For X = 0 To Y
    'Other code......
ReDim Preserve MAPIMottagare(X)
    MapiRecip(X).Name = sMottagare$
    MapiRecip(X).Address = s
    MapiRecip(X).Reserved = 0
    MapiRecip(X).RecipClass = 1
    MapiRecip(X).EIDSize = 1000
    MapiRecip(X).EntryID = "" 
    'Send mail w/o dialog
    MAPIMeddelande.RecipCount = Y + 1
    lReturn = MAPISendMail(lSession, hwnd, MAPIMsg, MapiRecip(0), MAPIFiles(0), lDialog, 0)
    'lReturn returns 2 instead of 0 here.
    'More code after this....

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


here is simple MAPI application which does not use MAPI controls. Probably you can modify it to your purposes. It works well in my Win95 based network, where I use Windows Messaging as mail provider.

In a module I have following declarations

Option Explicit
Type MapiFile
    Reserved As Long
    Flags As Long
    Position As Long
    PathName As String
    FileName As String
    FileType As String
End Type

Type MAPIMessage
    Reserved As Long
    Subject As String
    NoteText As String
    MessageType As String
    DateReceived As String
    ConversationID As String
    Flags As Long
    RecipCount As Long
    FileCount As Long
End Type

Type MapiRecip
    Reserved As Long
    RecipClass As Long
    Name As String
    Address As String
    EIDSize As Long
    EntryID As String
End Type

Declare Function MAPILogoff Lib "MAPI32.DLL" (ByVal Session&, ByVal UIParam&, ByVal Flags&, ByVal Reserved&) As Long
Declare Function MAPILogon Lib "MAPI32.DLL" (ByVal UIParam&, ByVal User$, ByVal Password$, ByVal Flags&, ByVal Reserved&, Session&) As Long
Declare Function MAPISendMail Lib "MAPI32.DLL" Alias "BMAPISendMail" (ByVal Session&, ByVal UIParam&, Message As MAPIMessage, Recipient() As MapiRecip, File() As MapiFile, ByVal Flags&, ByVal Reserved&) As Long
Declare Function MAPIResolveName Lib "MAPI32.DLL" Alias "BMAPIResolveName" (ByVal Session&, ByVal UIParam&, ByVal UserName$, ByVal Flags&, ByVal Reserved&, Recipient As MapiRecip) As Long

Public Const MAPI_LOGON_UI = &H1
Public Const MAPI_LOGOFF_UI = &H2
Public Const SUCCESS_SUCCESS = 0
Public Const MAPI_TO = 1
Public Const MAPI_DIALOG = &H8

and the under a command button in a form, which has a couple of text boxes where subject and message body are read in. The routine is heavily simplified. You need to add especially error handling there.

Private Sub Command1_Click()
    Dim szMsgID As String
    Dim szMsgType As String
    Dim lhSession As Long
    Dim lResult As Long
    Dim szPathName As String
    Dim szRecipName(2) As String
    Dim lSuccess As Long
    Dim MyMessage As MAPIMessage
    Dim I As Integer
    Dim J As Integer
    szMsgID = ""
    szMsgType = "IPM.Microsoft Mail.Note"
    MyMessage.FileCount = 0&
    MyMessage.Reserved = 0&
    MyMessage.MessageType = szMsgType
    MyMessage.DateReceived = ""
    MyMessage.Flags = 0&
    ReDim mrTemp(0 To 0) As MapiRecip
    ReDim mr(0 To 0) As MapiRecip
    ReDim MF(0 To 0) As MapiFile
    MF(0).Reserved = 0&
    MF(0).Flags = 0&
    MF(0).Position = -1
    MF(0).FileType = ""

    ' **************************************************
    ' Build Message
    ' **************************************************
    MyMessage.Subject = txtSubject
    MyMessage.NoteText = txtMessage
    ' Attachment part removed
    MF(0).PathName = ""
    MF(0).FileName = ""
    MyMessage.FileCount = 0

    ' **************************************************
    ' Build Recipient List
    ' This sample list is hard coded here
    ' **************************************************
    szRecipName(0) = "Lea Rantanen"
    szRecipName(1) = "Mikko Rantanen"
    szRecipName(2) = "Lasse Rantanen"
    ' **************************************************
    ' Login and start the MAPI Session
    ' **************************************************
    lResult = MAPILogon(Form1.hWnd, "", "", 0&, 0&, lhSession)
    If lResult <> SUCCESS_SUCCESS Then
         MsgBox "Error logging in"
         Exit Sub
    End If

    ' **************************************************
    ' Check Addresses Accepting only Valid Ones
    ' **************************************************
    J = 0
    For I = 0 To UBound(szRecipName)
        If Not IsEmpty(szRecipName(I)) Then
            mrTemp(0).Name = szRecipName(I)
            mrTemp(0).RecipClass = MAPI_TO
            lSuccess = MAPIResolveName(lhSession, 0, mrTemp(0).Name, 0, 0, mrTemp(0))
            If lSuccess <> SUCCESS_SUCCESS Then
                MsgBox ("The address for " & mrTemp(0).Name & " is not valid.")
                ReDim Preserve mr(J) As MapiRecip
                mr(J) = mrTemp(0)
                J = J + 1
           End If
        End If
    Next I
    MyMessage.RecipCount = CLng(UBound(mr)) + 1&
    ' **************************************************
    ' Send Mail
    ' **************************************************
    lResult = MAPISendMail(lhSession, Form1.hWnd, MyMessage, mr(), MF(), 0&, 0&)
    ' Check lResult here
    ' **************************************************
    ' Log Off
    ' **************************************************
    lResult = MAPILogoff(lhSession, 0&, 0&, 0&)
    ' Check lResult here
End Sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
janneaAuthor Commented:
Hello and thanks !

Sorry I havent respond but I have been bussy.

In the function
lResult = MAPISendMail(lhSession, Form1.hWnd, MyMessage, mr(), MF(), 0&, 0&)
you send mr() instead of mr(0) as I have hear thats the way to do it, I mean sending the first in the array even you got more than one recips or one attatced message. So i did hope that was my problem, but when iI do like this i get the error (ByRef argument type missmatch) so how come it works for you ?

PLS Give me your e-mailadress so I can mail you my function if you can see whats wrong, it still dont works.

My e-mail is
janneaAuthor Commented:
Thanks Lasse

/Janne, Sweden
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.