Tools Links Login

mSendEmail

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

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 92 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.