I use the following function to send about 300 emails, but some of the emails fail to be delivered. Do I need to pace the sending by putting some delay between sending the emails?
John
John
Code:
varDum = sendEmail(CStr(rst!strEmail), "Pay Slip", "Please find attached your Pay Slip" & vbCrLf & vbCrLf & "Confidentiality: This e-mail is intended for the addressees only and is confidential. If you have received this message by mistake or are not one of the addressees, you may take no action based on it and you may not print or copy or show it to anyone; please reply to this e-mail and point out the error which has occurred and delete this e-mail from your system." & vbCrLf & vbCrLf & "Security Warning: Please note that this e-mail has been created knowing that internet e-mail is not a 100% secure way to communicate. Please take this fact into account before deciding to send me a substantive reply by internet e-mail.", False, "", CStr(rst!strFileNamePDF))
Function sendEmail(strTo As String, strSubject As String, strBody As String, bEdit As Boolean, _
Optional strBCC As Variant, Optional AttachmentPath As Variant)
'Send Email using late binding to avoid reference issues
Dim objOutlook As Object
Dim objOutlookMsg As Object
Dim objOutlookRecip As Object
Dim objOutlookAttach As Object
Dim i As Integer
Const olMailItem = 0
On Error GoTo ErrorMsgs
Set objOutlook = CreateObject("Outlook.Application")
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
Set objOutlookRecip = .Recipients.Add(strTo)
objOutlookRecip.Type = 1
' If Not IsMissing(strBCC) Then
' Set objOutlookRecip = .Recipients.Add(strBCC)
' objOutlookRecip.Type = 3
' End If
.Subject = strSubject
.Body = strBody
.Importance = 2 'Importance Level 0=Low,1=Normal,2=High
' Add attachments to the message.
If Not IsMissing(AttachmentPath) Then
If IsArray(AttachmentPath) Then
For i = LBound(AttachmentPath) To UBound(AttachmentPath) - 1
If AttachmentPath(i) <> "" And AttachmentPath(i) <> "False" Then
Set objOutlookAttach = .Attachments.Add(AttachmentPath(i))
End If
Next i
Else
If AttachmentPath <> "" Then
Set objOutlookAttach = .Attachments.Add(AttachmentPath)
End If
End If
End If
' For Each objOutlookRecip In .Recipients
' If Not objOutlookRecip.Resolve Then
' objOutlookMsg.display
' End If
' Next
If bEdit Then 'Choose btw transparent/silent send and preview send
.display
Else
.Send
End If
End With
Set objOutlookMsg = Nothing
Set objOutlook = Nothing
Set objOutlookRecip = Nothing
Set objOutlookAttach = Nothing
ErrorMsgs:
If Err.Number = "287" Then
MsgBox "You clicked No to the Outlook security warning. " & _
"Rerun the procedure and click Yes to access e-mail " & _
"addresses to send your message. For more information, " & _
"see the document at [URL]http://www.microsoft.com/office[/URL]" & _
"/previous/outlook/downloads/security.asp."
Exit Function
ElseIf Err.Number <> 0 Then
MsgBox Err.Number & " - " & Err.Description
Exit Function
End If
End Function
Last edited by a moderator: