Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 361
  • Last Modified:

Problems sending email with attachments from MAPI

I am using the code below to log on and then send email with attachments. The problem I am having is that this code works fine on my Windows2000 laptop which I use for development, but, as soon as I try this on one of the network machines (running Windows 98 or NT) it seems to work but never brings the mail message on screen, further investigation found that during the compose message routine at the point of sending an error occurs and in the immediate window I am told that the attachment is not found, even though the attachment name is listed with the debug.print statement within the compose message procedure where I am adding an attachment. This all works perfectly on my development laptop that is not connected to the network. The one thing I have noticed is that on the development laptop when a session is opened it is Outlook Express that is opened, Outlook Express is not available on the network machines. Everything up to the send method appears to work fine.: -

At the form level: -

'***************************************************************************************
'* Add the attachment
'***************************************************************************************
Private Sub cmdAddAttachment_Click()
On Error GoTo ErrAddAttachment
Static i As Integer                       ' Hold the array element and retain its position
   
   If bSent = True Then                   ' Mail already sent
      i = 0                               ' Reset the static variable to zero
      bSent = False                       ' Set the flag that determines if the email has been sent
      ReDim sAttachmentName(iNumAttachments + 1) ' Re-dimension the array
   Else
      iNumAttachments = i                    ' Set the module level variable to the static amount
      ReDim Preserve sAttachmentName(iNumAttachments + 1) ' Re-dimension the array whilst preserving what it already contains
   End If
   
   sAttachmentName(i) = sFullFileName
   Debug.Print sAttachmentName(i)
   i = i + 1                              ' Increment the array element
   
   ' Enable the send button
   cmdSend.Enabled = True
   
   Exit Sub
   
ErrAddAttachment:
   MsgBox "Error No: " & Err.Number & vbCrLf & Err.Description & vbCrLf & "in the Add Attachment Click Event", vbCritical, ERRTITLE
   
End Sub

'***************************************************************************************
'* Send the selected photograph(s) to an Outlook recipient
'***************************************************************************************
Private Sub cmdSend_Click()
On Error GoTo ErrSend
   
   If fLogOn = True Then         ' Use the logon function
      If iNumAttachments = 0 Then sAttachmentName(0) = sFullFileName
      Call ComposeMessage("frmAdministrationIMP", iNumAttachments)
   End If
   
   ' If a session exists then log off the session, so that a new session can be commenced
   If (mapSess.SessionID) Then
      LogOff (Me.Name)
   End If
   
   Exit Sub
   
ErrSend:
   MsgBox "Error No: " & Err.Number & vbCrLf & Err.Description & vbCrLf & "in the Send Click Event", vbCritical, ERRTITLE
   
End Sub

Module Level: -
'***************************************************************************************
'* Check if a mail session is open and if not open one
'***************************************************************************************
Public Function fLogOn() As Boolean
On Error GoTo errLog
   
   If fLogOn = True Then fLogOn = False
   ' Check if a session is already opened and if so exit
   If frmAdministrationIMP.mapSess.NewSession Then
      MsgBox "Session already established"
      Exit Function
   End If
   
   With frmAdministrationIMP.mapSess
      .DownLoadMail = False             ' Prevent mail from downloading
      .LogonUI = True                   ' Use the underlying email system's logon UI.
      .SignOn                           ' Signon
      fLogOn = True                     ' If successful return True
      .NewSession = True                ' Set NewSession to True
      bNewSession = .NewSession         ' Set variable flag to true
      frmAdministrationIMP.mapMess.SessionID = .SessionID
   End With
   
   Exit Function
   
errLog:
   If Err.Number = 32003 Then
      MsgBox "The mail session was cancelled.", vbOKOnly + vbCritical, "Mail Session Cancelled"
      fLogOn = False
   Else
      MsgBox "Error No: " & Err.Number & vbCrLf & Err.Description & vbCrLf & "in the fLogOn Function", vbCritical, ERRTITLE
   End If
   
End Function

'***************************************************************************************
'* Compose the message
'***************************************************************************************
Public Sub ComposeMessage(sForm As String, iNumAttachments As Integer)
On Error GoTo ErrCompose
Dim i As Integer
Dim iAttachments As Integer
Dim sRecipient As String            ' Name of the mail recipient
Dim sTitle As String                ' The title of the email
Dim sInput As String                ' Used to hold the input entered by the user  into
   
   iAttachments = iNumAttachments
     
   ' Get the user to enter the recipients name and title of the email
   sRecipient = InputBox("Enter the name of the mail recipient.", "Recipient Name")
   If StrPtr(sRecipient) <> 0 Then
      sRecipient = "Enter/Select Recipient"  ' Set a feault value otherwise an error occurs
   Else
      Exit Sub
   End If
     
   sTitle = InputBox("Enter the title of the mail.", "Mail Title", "Photographs Attached")
   
   Select Case sForm
      Case "frmAdministrationIMP"
         With frmAdministrationIMP
            ' Set up the message
            .mapMess.Compose
            .mapMess.RecipAddress = sRecipient
            .mapMess.MsgSubject = sTitle
            .mapMess.MsgNoteText = "Please find attached the photographs you requested"
            ' Add all the attachments
            While sAttachmentName(i) <> ""
               .mapMess.AttachmentIndex = .mapMess.AttachmentIndex + 1
               .mapMess.AttachmentPathName = .mapMess.AttachmentPathName & sAttachmentName(i)
                Debug.Print sAttachmentName(i)
               i = i + 1
            Wend
            .mapSess.NewSession = False
            .mapMess.Send True
         End With
      Case "frmSearchResultsIMP"

   End Select
   
   bSent = True
   Exit Sub

   
ErrCompose:
   Debug.Print Err.Description
   Resume Next



Help with this is much appreciated.

LiMa
0
LiMa
Asked:
LiMa
  • 4
  • 2
  • 2
1 Solution
 
barnesd1Commented:
Are you sure the object library you are using (.olb file) works with the outlook version in the office?  Try referencing a version in "c:\program files\microsoft office\office", eg msoutl8.olb for Outlook 97.
0
 
LiMaAuthor Commented:
No, I didn't think you needed the outlook object library when using the MAPI controls. But, just in case I have tried adding this in, and creating a new setup (during the package and deployment wizard I am informed there are no reference files for the the outlook object lib). Unfortunately this still does not make any differance. I can get the above code to work perfectly with a single attachment by modifying part of the code in the compose message  procedure to: -

With frmAdministrationIMP
   .mapMess.Compose
   .mapMess.MsgSubject = sTitle
   .mapMess.MsgNoteText = "Please find attached the photographs you requested"
   .mapMess.AttachmentPathName = sAttachmentName(i)
   .mapSess.NewSession = False
   .mapMess.Send True
End With

This works fine but if I use the other code to add more attachments, I get the attachmenmt not found error. Even though I can use a debug.print statement and the correct attachment name is displayed.

LiMa
0
 
barnesd1Commented:
Public Function AddAttachment(strName As String, atAttachmentType As Variant, strSource As String, strReadFromFile As String)


Dim MapiAttachment As Object
Set MapiAttachment = MAPIMessage.Attachments.Add

With MapiAttachment
    .Name = strName
    .Type = mapiFileData
    .Source = strSource
    .ReadFromFile FileName:=strReadFromFile
End With
   
End Function

Use example
oMail.AddAttachment "Word", 0, sFilename, sFilename
This works for me!
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LiMaAuthor Commented:
I can't seem to get anything to work from either of the last two comments. It has to be something to do with trying to slot this code into my project.

Does anyone have an example of this running? Nothing elaborate just something that demonstrates how to add multiple attachments to the email. This is driving me insane I seem to be repeating the same process over and over again when trying to get this to work.

Help before I am driven totally insane.

LiMa
0
 
LiMaAuthor Commented:
Forgot to add my email address (just in case any of you experts do have a small demo): - lima_b1@excite.com

Thanx

LiMa
0
 
CD-SoftyCommented:
for a MAPI code generator, see
http://www.vbtoys.com/downloads.asp and download Visual MAPI 99 - good source code generator for VB.
0
 
LiMaAuthor Commented:
That's excellent thanks a lot. Thanks also to barnesd1 for your comments.

I think the main problem I had was that the MAPI messaging doesn't seem to be very well documented in the way that it should be used. The MAPI code generator helped me make sense of it all (well to some degree).

Thanx again.

LiMa
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now