mSendEmail

By | 2002-06-01

If you have Outlook 98 you can send email using VB! Use this code for the basis of creating mailing programs!

Original Author: Ian Ippolito (psc)

Inputs

vcolEmailAddress–collection of string email address
vstrSubject–email subject
vstrBody–email body (use vbCrLf to create lf)

Assumptions

Requires outlook 98 installed on your machine. Also, make sure you set a reference in your VB project to the Outlook 98 Type Library or this won’t compile.

API Declarations

Code

Sub mSendEmail(ByVal vcolEmailAddress As Collection, _
  ByVal vstrSubject As String, _
  ByVal vstrBody As String)
Dim ol As New Outlook.Application
Dim ns As Outlook.NameSpace

  'Return a reference to the MAPI layer
  Dim newMail As Outlook.MailItem
  
  'Create a new mail message item
  Set ns = ol.GetNamespace("MAPI")
  Set newMail = ol.CreateItem(olMailItem)
  
  'set properties
  With newMail
    'Add the subject of the mail message
    .Subject = vstrSubject
    'Create some body text
    .Body = vstrBody
    
    '**************
    'go through all
    'addresses passed in
    '**************
    Dim strEmailAddress As String
    Dim intIndex As Integer
    For intIndex = 1 To vcolEmailAddress.Count
    
      strEmailAddress = vcolEmailAddress.Item(intIndex)
      'Add a recipient and test to make sure that the
      'address is valid using the Resolve method
      With .Recipients.Add(strEmailAddress)
        .Type = olTo
        If Not .Resolve Then
          'MsgBox "Unable to resolve address.", vbInformation
          Debug.Print "Unable to resolve address " & strEmailAddress & "."
          'Exit Sub
        End If
      End With
    
    Next intIndex
    
'    'Attach a file as a link with an icon
'    With .Attachments.Add _
'      ("\Training raining.xls", olByReference)
'      .DisplayName = "Training info"
'    End With
    
    'Send the mail message
    .Send
    End With
    'Release memory
    Set ol = Nothing
    Set ns = Nothing
    Set newMail = Nothing
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.