Results 1 to 5 of 5
  1. #1
    cebrower is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2018
    Location
    Spring Lake, MI
    Posts
    110

    Sending email fails

    I use the following code to loop through a table and email invoices to customers. What is a good way to handle an error if the "To" email address is invalid? As it is now the routine chokes at mi.send. Sometimes the sender gets a bounce message in her inbox, but sometimes the program just crashes.

    Private Sub SendMail()


    Dim InvToSend As String
    Dim Sig As String
    Dim Sig2 As String


    InvToSend = "e:\EmailedInvoices" & varInvoiceNumber & ".pdf"
    Sig = Environ("appdata") & "\Microsoft\Signatures"
    Sig2 = Sig & Dir$(Signature & "*.htm")


    Dim outl As Outlook.Application
    Set outl = New Outlook.Application
    Dim mi As Outlook.MailItem
    Set mi = outl.CreateItem(olMailItem)


    mi.Attachments.Add ("e:\EmailedInvoices" & varInvoiceNumber & ".pdf")
    mi.HTMLBody = "Please see the attached invoice for your recent order." & "<br />" & "<br />" _
    & "Thank you," & "<br />" & "<br />" & "Lori Meyer" & "<br />" & "Accounting Manager" & "<br />" _
    & "Our Company, Inc." & "<br />" & "Accountant@OurCompany.com"
    mi.Subject = "Our Company Invoice #" & varInvoiceNumber
    mi.To = Forms!frmInvToEmail!BillingEmail
    If IsNull(Forms!frmInvToEmail!BillingEmail2) = False Then
    mi.CC = Forms!frmInvToEmail!BillingEmail2
    Else
    mi.CC = ""
    End If
    mi.Send
    Set mi = Nothing
    Set outl = Nothing


    End Sub

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,966
    Make sure at least one character before the @ sign and the same after it?
    Whilst there should be a domain name after, I do not think the mail programs are that clever.
    Really should do this check in the form with the email address, not after when an email needs to be sent.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,117
    Here is a function I use to validate the format of an email address:
    Code:
     Public Function IsEMailAddress(ByVal sEmail As String, _
        Optional ByRef sReason As String) As Boolean
            
        Dim sPreffix As String
        Dim sSuffix As String
        Dim sMiddle As String
        Dim nCharacter As Integer
        Dim sBuffer As String
    
    
        sEmail = Trim(sEmail)
    
    
        If Len(sEmail) < 8 Then
            IsEMailAddress = False
            sReason = "Too short"
            Exit Function
        End If
    
    
    
    
        If InStr(sEmail, "@") = 0 Then
            IsEMailAddress = False
            sReason = "Missing the @"
            Exit Function
        End If
    
    
    
    
        If InStr(InStr(sEmail, "@") + 1, sEmail, "@") <> 0 Then
            IsEMailAddress = False
            sReason = "Too many @"
            Exit Function
        End If
    
    
    
    
        If InStr(sEmail, ".") = 0 Then
            IsEMailAddress = False
            sReason = "Missing the period"
            Exit Function
        End If
    
    
        If InStr(sEmail, "@") = 1 Or InStr(sEmail, "@") = Len(sEmail) Or _
            InStr(sEmail, ".") = 1 Or InStr(sEmail, ".") = Len(sEmail) Then
            IsEMailAddress = False
            sReason = "Invalid format"
        Exit Function
    
    
    End If
    
    
    
    
    For nCharacter = 1 To Len(sEmail)
        sBuffer = Mid$(sEmail, nCharacter, 1)
        If Not (LCase(sBuffer) Like "[a-z]" Or sBuffer = "@" Or _
        sBuffer = "." Or sBuffer = "-" Or sBuffer = "_" Or _
        IsNumeric(sBuffer)) Then: IsEMailAddress = _
        False: sReason = "Invalid character": Exit Function
    Next nCharacter
    
    
    nCharacter = 0
    
    
    On Error Resume Next
    
    
    sBuffer = Right(sEmail, 4)
    If InStr(sBuffer, ".") = 0 Then GoTo TooLong:
    If Left(sBuffer, 1) = "." Then sBuffer = Right(sBuffer, 3)
    If Left(Right(sBuffer, 3), 1) = "." Then sBuffer = Right(sBuffer, 2)
    If Left(Right(sBuffer, 2), 1) = "." Then sBuffer = Right(sBuffer, 1)
    
    
    
    
    If Len(sBuffer) < 2 Then
        IsEMailAddress = False
        sReason = "Suffix too short"
        Exit Function
    End If
    
    
    TooLong:
    
    
    If Len(sBuffer) > 3 Then
        IsEMailAddress = False
        sReason = "Suffix too long"
        Exit Function
    End If
    
    
    sReason = Empty
    IsEMailAddress = True
    
    
    End Function
    Just add it to a standard module then use it in your code:
    Code:
    iF IsEMailAddress(Forms!frmInvToEmail!BillingEmail) Then mi.To = Forms!frmInvToEmail!BillingEmail
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  4. #4
    cebrower is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2018
    Location
    Spring Lake, MI
    Posts
    110
    Thanks, but the email addresses in question are properly formatted. They were just entered incorrectly (misspelled or a typo). It's an email address that should simply bounce but instead it throws an error. It's never happened until today and today it happed twice. Both emails were @aol.com if that means anything.

  5. #5
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,117
    You don't say which 'program" crashes so I assume is the Access app; it sounds like it is related to a delay in resolving the email address in the email client (Outlook), is Exchange involved at all? May want to try to use CDO emailing instead.

    https://www.access-programmers.co.uk...resses.305766/

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 14
    Last Post: 06-19-2020, 03:44 PM
  2. Email from VBA fails to attach report
    By cebrower in forum Programming
    Replies: 5
    Last Post: 12-10-2018, 06:59 AM
  3. email program fails with invlaide use of null
    By Lou_Reed in forum Access
    Replies: 40
    Last Post: 06-12-2017, 11:30 AM
  4. Replies: 1
    Last Post: 11-07-2016, 11:18 AM
  5. Email sending
    By Steven.Allman in forum Access
    Replies: 25
    Last Post: 06-21-2010, 09:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums