Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MAPI Email Attachments 1

Status
Not open for further replies.

Terabithia

Programmer
Aug 31, 2004
70
0
0
US
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
 
I cannot see anthing obviously wrong with your code; however your definition/ content of g_strEmailAttachments(i) array could be interesting.

You may like to try something based on the code I posted in thread222-1196390 which is still working; tested on Vista 32 bit with Outlook 2007 just now.
 
Thanks for taking the time to help.

I had already reviewed the posting you suggested, indeed it provided the inspiration for the code you see in my listing for the attachments.

The string array is initially dimensioned to 0, then is resized each time a file is added to the list to be included in the email, and each entry would read something like "V:\Programming\TestFile.txt". Do I need to do more for the path-filenames? The test for a null string is actually not needed given the way the array is designed and used, however it seemed like a good idea to keep it.

Most of the parameters for the message are passed into the function, I (right or wrong) chose to use a global array for the attachments that is populated prior to the function call. Could it have been a single string?

My development machine is running 32-bit XP Pro and Outlook 2007.

Is MAPI only used for Outlook and Outlook Express?

Thanks again for the help.

 
Try this;

If (Len(Trim$(Body)) <4) Then

.MsgNoteText = Left$(trim$(Body) & String$(3,160),3)

End If

 
>Most of the parameters for the message are passed into the function ...
I suggest you pass all information to the function via parameters.

Use of actual MAPI on-form controls can be avoided if you create a project reference to MAPI32.OCX
You can then create MAPI Objects with code like;

Dim MAPISess As MAPISession, MAPIMess As MAPIMessages
Set MAPISess = New MSMAPI.MAPISession
Set MAPIMess = New MSMAPI.MAPIMessages

OrigDir$ = CurDir$
With MAPISess
...

>Is MAPI only used for Outlook and Outlook Express?

It is good for any MAPI compliant email program which in practice generally does mean Outlook and Outlook express.
MAPI will not generally work for Lotus, although I believe Lotus can be set up to support/use it.
 
Thanks again for the help.

Your suggestion for .MsgNoteText got me to the point that I could attach a single attachment, however multiple attachments failed.

Then it dawned on me that I am using Base 0 and you are probably using Base 1, so I adjusted the code accordingly and multiple attachments now work.

Attachment code:

If (g_strEmailAttachments(0) <> "") Then

'Make sure the body has at least 4 characters
'So attachment will work
If (Len(Trim$(Body)) < 4) Then

.MsgNoteText = Left$(Trim$(Body) & String$(3, 160), 3)

End If

'Loop and add attachments
For i = 0 To UBound(g_strEmailAttachments)

.AttachmentIndex = i

'Set an attachment
.AttachmentPathName = g_strEmailAttachments(i)

.AttachmentPosition = i

Next i

End If

Thanks once again for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top