Email To Multiple Email Addresses

CharlesWhiteman

Registered User.
Local time
Today, 08:45
Joined
Feb 26, 2007
Messages
421
I found this code which enables sending outlook email without the security warning. I have a query containing email addresses and I'd like to understand a method where I can get the code to loop through to send multiple emails?

Code:
Option Explicit

[COLOR=#008000]' ACCESS VBA MODULE: Send E-mail without Security Warning
' (c) 2005 Wayne Phillips (http://www.everythingaccess.com)
' Written 07/05/2005
' Last updated v1.3 - 11/11/2005
'
' Please read the full tutorial & code here:
' http://www.everythingaccess.com/tutorials.asp?ID=Outlook-Send-E-mail-without-Security-Warning
'
' Please leave the copyright notices in place - Thank you.[/COLOR]

[COLOR=#008000]'This is a test function - replace the e-mail addresses with your own before executing!!
'(CC/BCC can be blank strings, attachments string is optional)[/COLOR]

Sub FnTestSafeSendEmail()
    Dim blnSuccessful As Boolean
    Dim strHTML As String
        
    strHTML = "<html>" & _
               "<body>" & _
               "My <b><i>HTML</i></b> message text!" & _
               "</body>" & _
               "</html>" 
    blnSuccessful = FnSafeSendEmail("myemailaddress@domain.com", _
    								"My Message Subject", _
    								strHTML)
    
    [COLOR=#008000]'A more complex example...    
    'blnSuccessful = FnSafeSendEmail("myemailaddress@domain.com; secondrecipient@domain.com", _
                                         "My Message Subject", _     
                                         strHTML, _    
                                         "C:\MyAttachmentFile1.txt; C:\MyAttachmentFile2.txt", _ 
                                         "cc_recipient@domain.com", _  
                                         "bcc_recipient@domain.com")[/COLOR]
    If blnSuccessful Then
    
        MsgBox "E-mail message sent successfully!"
        
    Else
    
        MsgBox "Failed to send e-mail!"
    
    End If

End Sub


[COLOR=#008000]'This is the procedure that calls the exposed Outlook VBA function...[/COLOR]
Public Function FnSafeSendEmail(strTo As String, _
                    strSubject As String, _
                    strMessageBody As String, _
                    Optional strAttachmentPaths As String, _
                    Optional strCC As String, _
                    Optional strBCC As String) As Boolean

    Dim objOutlook As Object ' Note: Must be late-binding.
    Dim objNameSpace As Object
    Dim objExplorer As Object
    Dim blnSuccessful As Boolean
    Dim blnNewInstance As Boolean
    
    [COLOR=#008000]'Is an instance of Outlook already open that we can bind to?[/COLOR]
    On Error Resume Next
    Set objOutlook = GetObject(, "Outlook.Application")
    On Error GoTo 0
    
    If objOutlook Is Nothing Then
    
        [COLOR=#008000]'Outlook isn't already running - create a new instance...[/COLOR]
        Set objOutlook = CreateObject("Outlook.Application")
        blnNewInstance = True    
        [COLOR=#008000]'We need to instantiate the Visual Basic environment... (messy)[/COLOR]
        Set objNameSpace = objOutlook.GetNamespace("MAPI")
        Set objExplorer = objOutlook.Explorers.Add(objNameSpace.Folders(1), 0)
        objExplorer.CommandBars.FindControl(, 1695).Execute
                
        objExplorer.Close
                
        Set objNameSpace = Nothing
        Set objExplorer = Nothing
        
    End If

    blnSuccessful = objOutlook.FnSendMailSafe(strTo, strCC, strBCC, _
                                                strSubject, strMessageBody, _
                                                strAttachmentPaths)
                                
    If blnNewInstance = True Then objOutlook.Quit
    Set objOutlook = Nothing
    
    FnSafeSendEmail = blnSuccessful
    
End Function
 
I use the following code to loop through a query to send emails to multiple recipients by CDO, therefore bypassing Outlook and its warning messages:-

Code:
Dim mydb As DAO.Database
Dim rs As DAO.Recordset
Set mydb = CurrentDb()
Set rs = mydb.OpenRecordset("6g daily email query", dbOpenSnapshot)
With rs
.MoveFirst
Do Until rs.EOF
If IsNull(rs.Fields(0)) = False Then
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Todays Testing for " & rs.Fields(1)
objMessage.From = "Project Testing <[EMAIL="bwbsl.testing@bwbsl.co.uk"]bwbsl.testing@bwbsl.co.uk[/EMAIL]>"
objMessage.Sender = "Project Testing <[EMAIL="bwbsl.testing@bwbsl.co.uk"]bwbsl.testing@bwbsl.co.uk[/EMAIL]>"
objMessage.To = rs.Fields(0)
objMessage.TextBody = "Below is a listing of testing work for you Today. If you need a detailed listing of tests, please contact the Testing Team who will provide this.  " & vbCrLf & _
                    "Number of Tests Due to Complete Today: " & rs.Fields(2) & vbCrLf & _
                    "Number of Tests Due to Start Today: " & rs.Fields(3) & vbCrLf & _
                    "Number of Tests Past Planned Completion Date: " & rs.Fields(4) & vbCrLf & _
                    "Number of Tests Past Planned Start Date: " & rs.Fields(5) & vbCrLf & _
                    "Number of Tests Due to Start Tomorrow Prep Not Complete: " & rs.Fields(6)
 

objMessage.Configuration.Fields.Item _
("[URL]http://schemas.microsoft.com/cdo/configuration/sendusing[/URL]") = 2

objMessage.Configuration.Fields.Item _
("[URL]http://schemas.microsoft.com/cdo/configuration/smtpserver[/URL]") = "smtp.server.com"

objMessage.Configuration.Fields.Item _
("[URL]http://schemas.microsoft.com/cdo/configuration/smtpserverport[/URL]") = 25
objMessage.Configuration.Fields.Update
 
objMessage.Send
End If
.MoveNext
Loop
End With
rs.Close
Set mydb = Nothing
Set rs = Nothing

You'll need to install CDO for Outlook (Add or Remove Programs)
 
I had downloaded the Collaboration Data Objects, version 1.2.1 and installed locally on my vista machine

And added CDO 1.2.1 Library as a reference in Access Vb
 
You need to replace that value--smtp.servername.com--with the url of the outgoing mail server at your isp. This will be the same outgoing server you use for any other email accounts.
 
Sorry, I should have told you what you need to change to get it to work.
 
I'm using this code now which I worked out from yours and it works fine.

Code:
Me.txtEmailSubject.SetFocus
Me.cmdSendEmail.Enabled = False
Me.lblNotification.Visible = True
Dim sToName As String
Dim sSubject As String
Dim sMessageBody As String
Dim VarHistoryID As String
Dim VarPDId As String
Dim varUser As String
Dim varHistoryDate As String
Dim varHistoryDetail As String
Dim varNextAction As String
varUser = "Admin"
varHistoryDate = Now()
varHistoryDetail = "Email Subject: " & Me.txtEmailSubject & vbCrLf & Me.rtxtEmailBody
varNextAction = "Email Sent"
DoCmd.SetWarnings False
Set mydb = CurrentDb()
Set rs = mydb.OpenRecordset("TblEmailList", dbOpenSnapshot)
With rs
.MoveFirst
Do Until rs.EOF
If IsNull(rs.Fields(2)) = False Then
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = Me.txtEmailSubject
objMessage.From = "FROM EMAIL ADDRESS HERE>"
objMessage.Sender = "FROM NAME <EMAIL ADDRESS>"
objMessage.To = rs.Fields(2)
objMessage.TextBody = "Dear " & ![FirstName] & vbCrLf & Me.rtxtEmailBody & vbCrLf & Me.txtEmailSignature
VarHistoryID = Nz(DMax("[HistoryID]", "TblHistory"), 0) + 1
VarPD = ![PrimaryDataID]
Dim strSQLHistory As String
strSQLHistory = "INSERT INTO TblHistory (HistoryID, PrimaryDataID, HistoryDate, User, HistoryDetail, NextAction)"
strSQLHistory = strSQLHistory & " VALUES('" & [VarHistoryID] & "', '" & [VarPD] & "', '" & [varHistoryDate] & "', '" & [varUser] & "', '" & [varHistoryDetail] & "', '" & [varNextAction] & "')"
DoCmd.RunSQL strSQLHistory
'==This section provides the configuration information for the remote SMTP server.
objMessage.Configuration.Fields.Item _
("[URL]http://schemas.microsoft.com/cdo/configuration/sendusing[/URL]") = 2
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("[URL]http://schemas.microsoft.com/cdo/configuration/smtpserver[/URL]") = "MAIL GATEWAY"
'Type of authentication, NONE, Basic (Base64 encoded), NTLM
objMessage.Configuration.Fields.Item _
("[URL]http://schemas.microsoft.com/cdo/configuration/smtpauthenticate[/URL]") = cdoBasic
'Your UserID on the SMTP server
objMessage.Configuration.Fields.Item _
("[URL]http://schemas.microsoft.com/cdo/configuration/sendusername[/URL]") = "USER NAME"
'Your password on the SMTP server
objMessage.Configuration.Fields.Item _
("[URL]http://schemas.microsoft.com/cdo/configuration/sendpassword[/URL]") = "PASSWORD"
'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("[URL]http://schemas.microsoft.com/cdo/configuration/smtpserverport[/URL]") = 25
'Use SSL for the connection (False or True)
objMessage.Configuration.Fields.Item _
("[URL]http://schemas.microsoft.com/cdo/configuration/smtpusessl[/URL]") = False
'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
objMessage.Configuration.Fields.Item _
("[URL]http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout[/URL]") = 60
objMessage.Configuration.Fields.Update
'==End remote SMTP server configuration section==
objMessage.Send
End If
.MoveNext
Loop
End With
rs.Close
Set mydb = Nothing
Set rs = Nothing
DoCmd.SetWarnings True
DoCmd.Close
 

Users who are viewing this thread

Back
Top Bottom