Sending Email to multiple recipients using MAPI

By | 2002-06-01

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.

Original Author: Sergei Lossev

Inputs

EmailAddress is of the form “first@first.com;second@second.com” and etc.
Subject and MessageText are self explainatory.

Assumptions

You are gonna need a form with the two MAPI controls, named like in the code.
The rest you should be able to get by yourself.

Returns

Nothing

Side Effects

Should work. It does on my computer :o)

Code

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

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.