Terabithia
Programmer
I need to add the ability to include attachments to email sent from a VB6 app via MAPI. The attachments paths & filenames are saved to an array previously.
If I have a single attachment the routine errors out at .Send with error 32002 "Unspecified failure has occurred".
If I have multiple attachments it errors out at .AttachmentPosition with error 380 "Invalid property value".
Any suggestions would be appreciated.
Code:
Private Function SendEmail_MAPI(SendTo As String, Optional CCTo As String = "", _
Optional Subject As String = "", Optional Body As String = "") As Boolean
Dim i As Integer
Dim strOriginalDir As String
'Save the current drive & directory
strOriginalDir = CurDir$
'Log-on to MAPI, verify log-on
If (MAPI_EmailLogOn(True) = True) Then
'Set mouse pointer to hourglass
Screen.MousePointer = vbHourglass
'Enable Error Handling
On Error GoTo ErrorHandler
With frmControls.MAPIMessages
'Set the session IDs the same on both objects
.SessionID = frmControls.MAPISession.SessionID
'Set the MsgIndex to -1, this needs to be
'done for the Compose event to work
.MsgIndex = -1
'Compose a new message
.Compose
'Don't show the resolve address interface
.AddressResolveUI = False
'Set the recipient
If (SendTo <> "") Then
.RecipIndex = 0
.RecipType = mapToList
.RecipAddress = SendTo
.RecipDisplayName = SendTo
'Resolve the recipient's email address
.ResolveName
End If
'Set the CC recipient
If (CCTo <> "") Then
.RecipIndex = 1
.RecipType = mapCcList
.RecipAddress = CCTo
.RecipDisplayName = CCTo
'Resolve the CC to email address
.ResolveName
End If
'Set the subject
.MsgSubject = Subject
'Set the Message/Body/NoteText
.MsgNoteText = Body
If (g_strEmailAttachments(1) <> "") Then
'Make sure the body has at least 1 character
'So attachment will work
If (Len(Body) <= 1) Then
.MsgNoteText = Left$(Body & Space(2), 2)
End If
'Loop and add attachments
For i = 1 To UBound(g_strEmailAttachments)
.AttachmentIndex = i - 1
'Set an attachment
.AttachmentPathName = g_strEmailAttachments(i)
.AttachmentPosition = i + 1
Next i
End If
'Set mouse pointer to default
Screen.MousePointer = vbDefault
'Restore the original path, changed by MAPI
ChDrive strOriginalDir
ChDir strOriginalDir
'Send the message, if "True" arguement is added
'The undelying mail system's form will be used.
.Send True
End With
'Set return value
SendEmail_MAPI = True
Else
'Set return value
SendEmail_MAPI = False
End If
GoTo ExitCode
ErrorHandler:
'Set mouse pointer to default
Screen.MousePointer = vbDefault
'Test for Cancel(32001) or Deny (32026)
If (Err.Number = 32001 Or Err.Number = 32026) Then
'Set return value
SendEmail_MAPI = True
Else
'Set return value
SendEmail_MAPI = False
'Display error message dialog
Call DisplayMessage_Error("Email Error!", , , "Email")
End If
ExitCode:
'Log-off
Call MAPI_EmailLogOff
'Restore the original path, changed by MAPI
ChDrive strOriginalDir
ChDir strOriginalDir
End Function
Private Function MAPI_EmailLogOn(SilentError As Boolean) As Boolean
'Set mouse pointer to hourglass
Screen.MousePointer = vbHourglass
'Enable Error Handling
On Error GoTo ErrorHandler
'Test for session already started
If (frmControls.MAPISession.NewSession = False) Then
'Initiate session
With frmControls.MAPISession
'Set DownLoadMail to False to prevent immediate download.
.DownLoadMail = False
'Use the underlying email system's logon UI
.LogonUI = True
'Sign-on method
.SignOn
'Set NewSession to True
.NewSession = True
End With
End If
ErrorHandler:
'Set mouse pointer to default
Screen.MousePointer = vbDefault
If (frmControls.MAPISession.SessionID = 0) Then
'Set return value
MAPI_EmailLogOn = False
If (SilentError = False) Then
If (Err.Number = 32003) Then
'Display message dialog
Call DisplayMessage("Canceled Email Log-in", , "Email")
Else
'Display error message dialog
Call DisplayMessage_Error("Email Log-in Error!", , , "Email")
End If
End If
Else
'Set return value
MAPI_EmailLogOn = True
End If
End Function
If I have a single attachment the routine errors out at .Send with error 32002 "Unspecified failure has occurred".
If I have multiple attachments it errors out at .AttachmentPosition with error 380 "Invalid property value".
Any suggestions would be appreciated.
Code:
Private Function SendEmail_MAPI(SendTo As String, Optional CCTo As String = "", _
Optional Subject As String = "", Optional Body As String = "") As Boolean
Dim i As Integer
Dim strOriginalDir As String
'Save the current drive & directory
strOriginalDir = CurDir$
'Log-on to MAPI, verify log-on
If (MAPI_EmailLogOn(True) = True) Then
'Set mouse pointer to hourglass
Screen.MousePointer = vbHourglass
'Enable Error Handling
On Error GoTo ErrorHandler
With frmControls.MAPIMessages
'Set the session IDs the same on both objects
.SessionID = frmControls.MAPISession.SessionID
'Set the MsgIndex to -1, this needs to be
'done for the Compose event to work
.MsgIndex = -1
'Compose a new message
.Compose
'Don't show the resolve address interface
.AddressResolveUI = False
'Set the recipient
If (SendTo <> "") Then
.RecipIndex = 0
.RecipType = mapToList
.RecipAddress = SendTo
.RecipDisplayName = SendTo
'Resolve the recipient's email address
.ResolveName
End If
'Set the CC recipient
If (CCTo <> "") Then
.RecipIndex = 1
.RecipType = mapCcList
.RecipAddress = CCTo
.RecipDisplayName = CCTo
'Resolve the CC to email address
.ResolveName
End If
'Set the subject
.MsgSubject = Subject
'Set the Message/Body/NoteText
.MsgNoteText = Body
If (g_strEmailAttachments(1) <> "") Then
'Make sure the body has at least 1 character
'So attachment will work
If (Len(Body) <= 1) Then
.MsgNoteText = Left$(Body & Space(2), 2)
End If
'Loop and add attachments
For i = 1 To UBound(g_strEmailAttachments)
.AttachmentIndex = i - 1
'Set an attachment
.AttachmentPathName = g_strEmailAttachments(i)
.AttachmentPosition = i + 1
Next i
End If
'Set mouse pointer to default
Screen.MousePointer = vbDefault
'Restore the original path, changed by MAPI
ChDrive strOriginalDir
ChDir strOriginalDir
'Send the message, if "True" arguement is added
'The undelying mail system's form will be used.
.Send True
End With
'Set return value
SendEmail_MAPI = True
Else
'Set return value
SendEmail_MAPI = False
End If
GoTo ExitCode
ErrorHandler:
'Set mouse pointer to default
Screen.MousePointer = vbDefault
'Test for Cancel(32001) or Deny (32026)
If (Err.Number = 32001 Or Err.Number = 32026) Then
'Set return value
SendEmail_MAPI = True
Else
'Set return value
SendEmail_MAPI = False
'Display error message dialog
Call DisplayMessage_Error("Email Error!", , , "Email")
End If
ExitCode:
'Log-off
Call MAPI_EmailLogOff
'Restore the original path, changed by MAPI
ChDrive strOriginalDir
ChDir strOriginalDir
End Function
Private Function MAPI_EmailLogOn(SilentError As Boolean) As Boolean
'Set mouse pointer to hourglass
Screen.MousePointer = vbHourglass
'Enable Error Handling
On Error GoTo ErrorHandler
'Test for session already started
If (frmControls.MAPISession.NewSession = False) Then
'Initiate session
With frmControls.MAPISession
'Set DownLoadMail to False to prevent immediate download.
.DownLoadMail = False
'Use the underlying email system's logon UI
.LogonUI = True
'Sign-on method
.SignOn
'Set NewSession to True
.NewSession = True
End With
End If
ErrorHandler:
'Set mouse pointer to default
Screen.MousePointer = vbDefault
If (frmControls.MAPISession.SessionID = 0) Then
'Set return value
MAPI_EmailLogOn = False
If (SilentError = False) Then
If (Err.Number = 32003) Then
'Display message dialog
Call DisplayMessage("Canceled Email Log-in", , "Email")
Else
'Display error message dialog
Call DisplayMessage_Error("Email Log-in Error!", , , "Email")
End If
End If
Else
'Set return value
MAPI_EmailLogOn = True
End If
End Function