Results 1 to 4 of 4
  1. #1
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Emailing using APIs (VB6)

    A colleague has asked me to post the following, if any further info or explanation is needed please reply as ill check with my colleague.

    Can anyone help.

    I am trying to get an email sent with the code below.
    It will send to a single person with an attachment, but the attachment only shows as an unknown file although it will open in the associated program.

    How can I send to a CC, BCC, more than one recipient, change the FILETYPE so it is recognised as XL if thats what I am sending. I also want to change the FROM

    any help or suggestions would be much appreciated.



    Option Explicit

    '-- MODULE
    '-- used for sending mail with api and outlook objects
    '-- can be used in MS Access or Visual Basic
    '-- MAPI constants
    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
    '-- 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

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Emailing using APIs (VB6)

    There must be something missing. You posted two functions: api_SendMail and olk_SendMail, both with arguments

    sTo As String
    sSubject As String
    sMessage As String

    Neither has an argument to specify attachments.

    I don't know anything about MAPI, but with Outlook Automation (the second function), one can specify

    With objMailItem
    .SentOnBehalfOfName = "MyBoss" ' Sender - need delegate permission to do so
    .Attachments.Add Source:="C:Topsecret.doc", Type:=olByValue ' Add attachment - can be repeated
    Set olRecipient = .Recipients.Add(Name:="billg@microsoft.com") ' Add recipient - can be repeated
    olRecipient.Type = olTo ' or olCC or olBCC
    End With

    Note: for hopefully obvious security reasons, you can't change the FROM field, but if you have sufficient permissions, you can send mail on behalf of another.

  3. #3
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Emailing using APIs (VB6)

    I can understand going to MAPI to avoid Outlook if you don't have it installed or can't rely on it being there, but that usually entails use of the CDO library instead, since the latter wraps the MAPI calls in a programming interface to simplify addressing them in code. Since MAPI is where many of the security gaps in Windows communications live, I'd recommend your colleague look into the use of Redemption to bypass MAPI and its security issues and go straight to ExtendedMAPI. The programming interface for the Redemption library is similar to CDO or to Outlook, so it is very comfortable for VB/VBA programmers.
    Charlotte

  4. #4
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Emailing using APIs (VB6)

    Thanks Hans and Charlotte for your prompt replies.

    I've given the information to my colleague and await his response. I haven't worked much with APIs yet but I will get around to looking at them, just as soon as i've got a bit more VB experience behind me!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •