[AccessD] Email problem. Again

Dan Waters dwaters at usinternet.com
Mon Mar 1 15:35:05 CST 2010


Rocky,

Open a new mdb and put this into a class module named 'EmailSMTP':
----------------------------------------------------------
' C Copyright ProMation Systems, Inc.
' Module    : EmailSMTP
' Author    : Dan Waters

Option Compare Database
Option Explicit

Private WithEvents poSendMail As vbSendMail.clsSendMail

Private Sub poSendMail_SendFailed(Explanation As String)

    '-- The SMTPEmailFailed procedure is called so that ErrEx will record
the Call Stack and also the value of Explanation
    Call Run("SMTPErrorRaise", 600, "poSendMail_SendFailed", Explanation)

End Sub

'Private Sub poSendMail_Progress(PercentComplete As Long)
'
'    Debug.Print "poSendMail_Progress " & PercentComplete
'
'    Exit Sub
'    ErrEx.Bookmark = BOOKMARK_ONERROR
'
'End Sub

'Private Sub poSendMail_SendSuccesful()
'
'    Debug.Print "poSendMail_SendSuccesful "
'
'    Exit Sub
'    ErrEx.Bookmark = BOOKMARK_ONERROR
'
'End Sub

'Private Sub poSendMail_Status(Status As String)
'
'    Debug.Print "poSendMail_Status " & Status
'
'    Exit Sub
'    ErrEx.Bookmark = BOOKMARK_ONERROR
'
'End Sub

Public Sub SendEmailsNowSMTP(stgSMTPHost As String, _
    stgSMTPFromDisplayName As String, _
    stgSMTPFromAddress As String, _
    stgSMTPSubject As String, _
    stgSMTPRecipientDisplayName As String, _
    stgSMTPRecipientAddress As String, _
    Optional blnSMTPAsHTML As Boolean, _
    Optional stgSMTPMessage As String, _
    Optional stgSMTPAttachmentList As String, _
    Optional stgSMTPReplyToAddress)
    
    If stgSMTPRecipientDisplayName = "" Or stgSMTPRecipientAddress = "" Or
stgSMTPRecipientAddress = "No Email Address" Then
        Call Run("SMTPErrorRaise", 601, "SendEmailsNowSMTP", "Cannot Send
Email")
        Exit Sub
    End If
    
    Set poSendMail = New vbSendMail.clsSendMail

'    -- Set up email paramaters
    poSendMail.SMTPHost = stgSMTPHost
    poSendMail.RecipientDisplayName = stgSMTPRecipientDisplayName
    poSendMail.Recipient = stgSMTPRecipientAddress
    poSendMail.FromDisplayName = stgSMTPFromDisplayName
    poSendMail.from = stgSMTPFromAddress
    poSendMail.Subject = stgSMTPSubject
    
    If Not IsMissing(blnSMTPAsHTML) Then
        poSendMail.AsHTML = blnSMTPAsHTML
    End If
    If Not IsMissing(stgSMTPMessage) Then
        If stgSMTPMessage <> "" Then
            poSendMail.message = stgSMTPMessage
        End If
    End If
    If Not IsMissing(stgSMTPAttachmentList) Then
        If stgSMTPAttachmentList <> "" Then
            poSendMail.Attachment = stgSMTPAttachmentList
        End If
    End If
    If Not IsMissing(stgSMTPReplyToAddress) Then
        If stgSMTPReplyToAddress <> "" Then
            poSendMail.ReplyToAddress = stgSMTPReplyToAddress
        End If
    End If

    '-- When email is originated from the developer's PC, don't actually
send email
    If Environ("ComputerName") <> "DanWaters" Then
        poSendMail.Connect
        DoEvents
        poSendMail.Send
        DoEvents
        poSendMail.Disconnect
        DoEvents
    End If
    
    Set poSendMail = Nothing

End Sub
----------------------------------------------------------

 
 
Now put this into a standard module:
----------------------------------------------------------
Option Compare Database
Option Explicit

Public ES As New EmailSMTP
----------------------------------------------------------

A nice feature of vbSendMail is that it will give you a report of when and
why an SMTP email failed.  But you need to use 'WithEvents' in a class
module for this to work.

You must register both the vbSendMail.dll file and the mswinsck.ocx file.

If you comment out the .SMTPHost line, vbSendMail will try to find the host
name.  That's OK if there's only one.  But if there's more than one and you
want to use a specific host, then you need that host name.

The instruction manual for vbSendMail is really good.

The only significant thing I see different is that I use the
poSendMail.Connect and .Disconnect commands.  Can't remember if those are
necessary or not.

Good Luck!
Dan




More information about the AccessD mailing list