Advertisement
4_2005-2006 Miscellaneous #151241

Sending Email to multiple recipients using MAPI

It seems that a lot of people are having problems sending Email from VB to multiple people. Here's a procedure that should fix that.

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
Public Sub SendEmail(sEmailAddress As String,sSubject as string, sMessageText as string)
  Dim sEmailExtracted As String
  Dim sEmailLeft As String
  Dim iRecipCount As Integer
  
  If Trim(sEmailAddress) = "" Then
      Goto SendMail_End
  End If
  
  sEmailLeft = Trim(sEmailAddress)
  
  ' set the mouse pointer to indicate the app is busy
  Screen.MousePointer = vbHourglass
  
  MAPIlogon.SignOn
    
  Do While MAPIlogon.SessionID = 0
  
  
    DoEvents ' need to wait until the new session is created
    
  Loop
  
    With MAPIMessages1
      .MsgIndex = -1
      .SessionID = MAPIlogon.SessionID
      
      While sEmailLeft <> ""
      
        If InStr(1, sEmailLeft, ";") = 0 Then
          sEmailExtracted = sEmailLeft
          sEmailLeft = ""
        Else
          sEmailExtracted = Left(sEmailLeft, InStr(1, sEmailLeft, ";") - 1)
          sEmailLeft = Right(sEmailLeft, Len(sEmailLeft) - InStr(1, sEmailLeft, ";"))
        End If
      
        .RecipIndex = iRecipCount
        If iRecipCount = 0 Then
          .RecipType = mapToList
        Else
          .RecipType = mapCcList
        End If
        
        .RecipAddress = sEmailExtracted
        
        .ResolveName
        
        iRecipCount = iRecipCount + 1
        
      Wend
  
      If iRecipCount = 0 Then GoTo SendMail_End
      
      .MsgSubject = sSubject
      .MsgNoteText = sMessageText      
      .Send
    End With
    
    MAPIlogon.SignOff
SendMail_End:  
  Screen.MousePointer = vbNormal
  Exit Sub
End Sub
Original Comments (3)
Recovered from Wayback Machine