Use simple Mapi with Access 2010

TimTDP

Registered User.
Local time
Today, 03:08
Joined
Oct 24, 2008
Messages
213
How can I use simple mapi with Access 2010?
I am also running windows 7
 
How can I use simple mapi with Access 2010?
I am also running windows 7

You can use simple MAPI with VBA at least 2 ways. 1 you use the Outlook Object model of outlook you have installed. Or you use windows API to use simple MAPI.

so in a word - YES
 
You can use simple MAPI with VBA at least 2 ways. 1 you use the Outlook Object model of outlook you have installed. Or you use windows API to use simple MAPI.

so in a word - YES

Thanks
Do you have sample code using the windows API?
For a variety of reasons I don't use Outlook
 
Thanks
Do you have sample code using the windows API?
For a variety of reasons I don't use Outlook

I am sorry but I am not up to date on what options you have without outlook. I think CDO is the first choice. Redemption is available as well I think.

I cannot find where I go this from, and I have only tested it once but here it is.

Put this in a module.

Code:
Option Compare Database
Option Explicit

Public Const MAPI_AB_NOMODIFY = &H400
Public Const MAPI_BCC = 3
Public Const MAPI_BODY_AS_FILE = &H200
Public Const MAPI_CC = 2
Public Const MAPI_DIALOG = &H8
Public Const MAPI_E_AMBIGUOUS_RECIPIENT = 21
Public Const MAPI_E_AMBIG_RECIP = MAPI_E_AMBIGUOUS_RECIPIENT
Public Const MAPI_E_ATTACHMENT_NOT_FOUND = 11
Public Const MAPI_E_ATTACHMENT_OPEN_FAILURE = 12
Public Const MAPI_E_ATTACHMENT_WRITE_FAILURE = 13
Public Const MAPI_E_BAD_RECIPTYPE = 15
Public Const MAPI_E_BLK_TOO_SMALL = 6
Public Const MAPI_E_DISK_FULL = 4
Public Const MAPI_E_FAILURE = 2
Public Const MAPI_E_INSUFFICIENT_MEMORY = 5
Public Const MAPI_E_INVALID_EDITFIELDS = 24
Public Const MAPI_E_INVALID_MESSAGE = 17
Public Const MAPI_E_INVALID_RECIPS = 25
Public Const MAPI_E_INVALID_SESSION = 19
Public Const MAPI_E_LOGIN_FAILURE = 3
Public Const MAPI_E_LOGON_FAILURE = MAPI_E_LOGIN_FAILURE
Public Const MAPI_E_MESSAGE_IN_USE = 22
Public Const MAPI_E_NETWORK_FAILURE = 23
Public Const MAPI_E_NO_MESSAGES = 16
Public Const MAPI_E_NOT_SUPPORTED = 26
Public Const MAPI_E_TEXT_TOO_LARGE = 18
Public Const MAPI_E_TOO_MANY_FILES = 9
Public Const MAPI_E_TOO_MANY_RECIPIENTS = 10
Public Const MAPI_E_TOO_MANY_SESSIONS = 8
Public Const MAPI_E_TYPE_NOT_SUPPORTED = 20
Public Const MAPI_E_UNKNOWN_RECIPIENT = 14
Public Const MAPI_ENVELOPE_ONLY = &H40
Public Const MAPI_FORCE_DOWNLOAD = &H1000
Public Const MAPI_GUARANTEE_FIFO = &H100
Public Const MAPI_LOGOFF_SHARED = &H1
Public Const MAPI_LOGOFF_UI = &H2
Public Const MAPI_LOGON_UI = &H1
Public Const MAPI_NEW_SESSION = &H2
Public Const MAPI_OLE = &H1
Public Const MAPI_OLE_STATIC = &H2
Public Const MAPI_ORIG = 0
Public Const MAPI_PEEK = &H80
Public Const MAPI_RECEIPT_REQUESTED = &H2
Public Const MAPI_SENT = &H4
Public Const MAPI_SUPPRESS_ATTACH = &H800
Public Const MAPI_TO = 1
Public Const MAPI_UNREAD = &H1
Public Const MAPI_UNREAD_ONLY = &H20
Public Const MAPI_USER_ABORT = 1
Public Const MAPI_E_USER_ABORT = MAPI_USER_ABORT
Public Const SUCCESS_SUCCESS = 0

'-- MODULE
'-- used for sending mail with api and outlook objects
'-- can be used in MS Access or Visual Basic
'-- MAPI constants



'-- mapi message recipient object type


Public Type MapiRecip
    Reserved As Long
    RecipClass As Long
    Name As String
    Address As String
    EIDSize As Long
    EntryID As String
    End Type
    '-- mapi message file object type


Public Type MapiFile
    Reserved As Long
    Flags As Long
    Position As Long
    PathName As String
    FileName As String
    FileType As String
    End Type
    '-- mapi message object type


Public 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


Public Declare Function MAPILogoff Lib "MAPI32.DLL" (ByVal Session&, ByVal UIParam&, ByVal Flags&, _
ByVal Reserved&) As Long


Public Declare Function MAPILogon Lib "MAPI32.DLL" (ByVal UIParam&, ByVal User$, ByVal Password$, _
ByVal Flags&, ByVal Reserved&, Session&) As Long


Public 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




Public Function api_SendMail(sTo As String, sSubject As String, sMessage As String)

    '-- use api functions to send mail
    On Error GoTo Err_Trap
    Dim Rtn As Long '-- return value For api calls
    Dim objMsg As MAPIMessage '-- message object
    Dim objRec() As MapiRecip '-- recipient object array
    Dim objFile() As MapiFile '-- file object array
    Dim hMAPI As Long '-- session handle
    ReDim objRec(1)
    ReDim objFile(1)
    '-- file object *************************************************
    '     *************
    '-- default - not expecting to send a file
    objFile(0).Reserved = 0
    '-- values not used for file
    'objFile(0).Flags
    'objFile(0).Position
    'objFile(0).PathName
    'objFile(0).FileName
    'objFile(0).FileType
    '-- recipient object ********************************************
    '     *************
    objRec(0).Reserved = 0
    objRec(0).RecipClass = 1
    objRec(0).Name = sTo
    '-- values not used for recipient
    'objRec.Address
    'objRec.EIDSize
    'objRec.EntryID
    '-- message object **********************************************
    '     *************
    objMsg.Reserved = 0
    objMsg.Subject = sSubject
    objMsg.RecipCount = 1
    objMsg.FileCount = 0
    objMsg.NoteText = sMessage
    '-- values not used for message
    'objMsg.MessageType
    'objMsg.DateReceived
    'objMsg.ConversationID
    'objMsg.Flags
    '-- make api calls to send mail *********************************
    '     **************
    '-- logon to MAPI application
    '-- default profile is set in user name parameter of Logon
    Rtn = MAPILogon(0, "MS Exchange Settings", "", MAPI_LOGON_UI, 0, hMAPI)
    '-- send mail message through MAPI
    Rtn = MAPISendMail(hMAPI, 0, objMsg, objRec, objFile, 0, MAPI_DIALOG)
    '-- logoff MAPI application
    Rtn = MAPILogoff(hMAPI, 0, 0, 0)
    Exit Function
Err_Trap:
    ErrorCatch "MOD_MAIL.api_SendMail()"

End Function
'Public Function olk_SendMail(sTo As String, sSubject As String, sMessage As String)
'
'    -- use MS Outlook object to send mail
'    On Error GoTo Err_Trap
'    Dim objOutlook As Outlook.Application
'    Dim objMailItem As Outlook.MailItem
'    Set objOutlook = New Outlook.Application
'    Set objMailItem = objOutlook.CreateItem(olMailItem)
'
'With objMailItem
'        .To = sTo
'        .Subject = sSubject
'        .Body = sMessage
'        .Send
'    End With
'    Set objMailItem = Nothing
'    Set objOutlook = Nothing
'    Exit Function
'Err_Trap:
'
'    ErrorCatch "MOD_MAIL.olk_SendMail()"
'
'End Function

Public Function ErrorCatch(sDesc As String)

    Dim msg As String
    msg = vbTab & vbTab & "ERROR OCCURRED!!" & vbCrLf & vbCrLf
    msg = msg & "Function :" & vbTab & sDesc & vbCrLf
    msg = msg & "Number :" & vbTab & Err.Number & vbCrLf
    msg = msg & "Message :" & vbTab & Err.Description
    MsgBox msg, vbCritical, "Program Error"
    Err.Clear

End Function
 
Many thanks

The code uses Exchange. I don't use any Microsoft e-mail client. Outlook would be best but Outlook creates an address book for each account. I don't want that. I want one address book that can be accessed by all accounts!

CDO is great. The problem with CDO is that I can't automatically store e-mails sent from Access in my e-mail client's sent folder. To do this I currently have to BCC a copy to me and have a filter move the received my from my inbox to my sent box. This is not efficient, and in South Africa bandwidth is not cheap - or fast!
 
Hi TimTDP,

I can't give you any advice concerning simple MAPI, but i can share some code to send emails through Thunderbird. You can have attachments (which SendObject does not allow), but you'll have to click the Send button everytime you create a message.
Code:
Public Function Mail_Thunderbird(destinataire As String, bcc_Dest As String, cc_Dest As String, sujet As String, Corps As String, Optional piece_jointe As String)
'-------------------------------------------
' To attach multiple files :
' 'file///full_path_File1,file///full_path_File2'

' Edit and send an email
strCommand = "C:\Program Files\Mozilla Thunderbird\thunderbird"
strCommand = strCommand & " -compose " & "to=" & destinataire & ""
strCommand = strCommand & "," & "cc=" & cc_Dest & ","
strCommand = strCommand & "," & "bcc=" & bcc_Dest & ","
strCommand = strCommand & "," & "subject='" & sujet & "', "
strCommand = strCommand & "body='" & Corps & "'"
strCommand = strCommand & "," & "attachment='" & piece_jointe & "'"
 
Call Shell(strCommand, vbNormalFocus)
 

End Function

I'm still trying top figure out how to use simple MAPI in VBA...
 
Using simple Mapi with Access 2000

Just picked up this thread as I have a new client who does not have Outlook. Your Thunderbird code does exactly as you said, displaying [but not actually sending] emails via Thunderbird, but I have to program in Access 2000 up to 400 addressees, so clicking "send" on each of the created emails would be onerous. As this is not a new thread, have you since found a solution to the auto-send problem using Mapi?

I also used the following code which uses SendObject [with an attachment]. This works OK if the final parameter is zero [i.e. display the email] but as soon as I change it to -1 [Send], I get an error message:
"Microsoft Access can't send a message for the reason stated in the preceding alert" ???? whatever that was! I think it was "someone is trying to
use your email system" or similar, but I can't replicate the first message to find out. Any clues as to how to do so appreciated.
Regards
dibby44 @ Hove UK
===========================
Public Function SendEmailsViaThunderbird()
On Error GoTo Err_email

' Header
MyStr = "Test email - Report data in XL file attached"
' Body
MyStr2 = "This is a message to test the email function - no reply required"
MyStr2 = MyStr2 & vbCrLf & "Leaders in XL attached - data not verified"
MyStr2 = MyStr2 & vbCrLf & "Regards"
MyStr2 = MyStr2 & vbCrLf & "XXX"
DoCmd.SendObject acSendQuery, "q_Rpt_SomeReport", acFormatXLS, "MAILTO:ADDRESS", "MAILTO:CCADDRESS", , MyStr, MyStr2, 0


Exit_email:
DoCmd.Hourglass False
Exit Function

Err_email:
MsgBox "Email Error: " & Err.Description
Resume Exit_email

End Function
=======================
 
The problem with CDO is that I can't automatically store e-mails sent from Access in my e-mail client's sent folder.
 
But how can you send an e-mail using your client's credentials without supplying the credentials? CDO will store it in the sent folder if you give it the credentials.
 
How can I provide CDO with the client's e-mail credentials?
 
You don't know what their credentials are. The client provides their credentials to CDO via a form that you provide (for example).
 
OK
I use Mozilla's Thunderbird e-mail client
How do I pass this information to CDO so that the e-mail is stored in the sent folder?

Many thanks
 
Like I mentioned previously, you will need to pass the credentials to CDO and any sent message will be saved in the user's Sent folder. This saving process is not handled by CDO, but by the e-mail server, in your case Thunderbird.

You will need to give CDO the SMTP server, username and password. It's all in the link.
 

Users who are viewing this thread

Back
Top Bottom