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!

Emailing with multiple file attachments using MAPI 1

Status
Not open for further replies.

vanvb

Programmer
Feb 7, 2003
255
0
0
CA
I am trying to do exactly what I said in the subject but am beating my head against the wall. When I take out the code from below that attaches the files, the email works great. When I try to attach files as below, it doesn't send anything...it also doesn't give me any errors. Any ideas?

Code:
Option Explicit

Private Const S_OK = &H0                ' Success
Private Const S_FALSE = &H1             ' The Folder is valid, but does not exist
Private Const E_INVALIDARG = &H80070057 ' Invalid CSIDL Value

Private Const CSIDL_LOCAL_APPDATA = &H1C&
Private Const CSIDL_FLAG_CREATE = &H8000&

Private Const SHGFP_TYPE_CURRENT = 0
Private Const SHGFP_TYPE_DEFAULT = 1
Private Const MAX_PATH = 260

Private Declare Function SHGetFolderPath Lib "shfolder" _
    Alias "SHGetFolderPathA" _
    (ByVal hwndOwner As Long, ByVal nFolder As Long, _
    ByVal hToken As Long, ByVal dwFlags As Long, _
    ByVal pszPath As String) As Long
Public Function SendEmail()
    Dim strEmailMsg As String
    Dim mydocPath As String
    Dim RetVal As Long
    Dim MyName As String
    Dim fso As Scripting.FileSystemObject
    Dim myFolder As Scripting.Folder
    Dim myFile As Scripting.File
    Dim strTemp As String
    Dim intRtn, intX As Integer
    
 On Error Resume Next
    mpSession.SignOn
    mpMessage.SessionID = mpSession.SessionID
    mpMessage.RecipDisplayName = "Joe.Smith@emailing.ca"
    mpMessage.RecipAddress = "Joe Smith"
    mpMessage.AddressResolveUI = True
    mpMessage.ResolveName
    
    mpMessage.Compose
    mpMessage.MsgSubject = "This is the subject"
    strEmailMsg = "This is the email text"
    mpMessage.MsgNoteText = strEmailMsg
    
    mydocPath = "c:\batch\statistics\result\main"
    Set fso = Nothing   'these lines re-initialize the files in the array
    Set myFolder = Nothing
    Set fso = New Scripting.FileSystemObject
    Set myFolder = fso.GetFolder(mydocPath)
    For Each myFile In myFolder.Files
        mpMessage.AttachmentIndex = intX
        mpMessage.AttachmentPosition = intX
        mpMessage.AttachmentType = 0
        strTemp = myFile.Name   'turns the filename into a string variable
        mpMessage.AttachmentPathName = mydocPath & "\" & strTemp
        intX = intX + 1
    Next
    mpMessage.Send False
    mpSession.SignOff
    
End Function

Private Sub Form_Load()
SendEmail
End
End Sub

Thanks in advance.
 
Try:
Code:
    mpSession.SignOn
    mpMessage.SessionID = mpSession.SessionID
    mpMessage.Compose
    mpMessage.MsgSubject = "This is the subject"
    strEmailMsg = "This is the email text"
    mpMessage.MsgNoteText = strEmailMsg
    
    mpMessage.RecipIndex = 0
    mpMessage.RecipType = 1
    mpMessage.RecipDisplayName = _
    "[SMTP:Joe.Smith@emailing.ca]"
    mpMessage.AddressResolveUI = False
    mpMessage.ResolveName
    
    mydocPath = "c:\batch\statistics\result\main"
    Set fso = Nothing   'these lines re-initialize the files in the array
    Set myFolder = Nothing
    Set fso = New Scripting.FileSystemObject
    Set myFolder = fso.GetFolder(mydocPath)
    For Each myFile In myFolder.Files
        mpMessage.AttachmentIndex = intX
        mpMessage.AttachmentPosition = intX
        mpMessage.AttachmentType = 0
        strTemp = myFile.Name   'turns the filename into a string variable
        mpMessage.AttachmentPathName = mydocPath & "\" & strTemp
        intX = intX + 1
    Next
    mpMessage.Send False
    mpSession.SignOff

It's a good thing to use RecipDisplayName in the format above for SMTP addresses; it avoids 32002 errors when RecipAddress is used. However you shouldn't set the RecipAddress as well and AddressResolveUI should be false.

I'm guessing the non-send was a recipient resolution problem.

Paul Bent
Northwind IT Systems
 
I see you are trying to trap any potential errors that could occur (On Error Resume Next), but I don't see any error handling code.

I could be wrong, but at first glance that's what I see....
 
I give up...I don't get it.
I was looking over the code(again...for 50th time), and I found that the mpMessage.Compose had worked it's way below setting the recipient. PaulBent, you fixed this in your version. Anyways, I had tried this code earlier by commenting out the attachment stuff and it worked. Then I uncommented the attachment and it didn't work. That's why I ignored everything else. Now I move the compose part back where it should be and evrything now works. Attachments, everything.

Thanks guys...some days I hate this stuff. Then it works. For some reason though, do you ever notice that when you get it working you sometimes feel dumber than when it didn't work?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top