0
votes

I need to send an email from an MS Access database with an attachment (not an Access object, but a separate file), but not tied to any one email software (Groupwise, Outlook, etc). I have found code to send an email with an attachment using Groupwise and Outlook, and there is the generic DoCmd.SendObject which only appears to support attaching Access objects. Is there a way to send an email from Access with an attachment, regardless of the email client configured on the user's PC?

Rationale: There's complications with software rollout here. The machine I work on has Access 2013 and Outlook 2013 installed. The users of the database are running Access 2010, but when I compile the database into a .accde in 2013, it does not work on 2010. The only way I can get it to work is to compile it on a much older PC also running Access 2010. However, this old PC does not have Outlook and IT won't/can't install Outlook on it. This means I can't compile the database using the Outlook library, as there is no Outlook library on the machine.

1

1 Answers

2
votes

Here is code I use to send e-mails using Gmail:

Public Function SendEmailViaGmail(SendTo As String, Optional Subject As String = "", Optional TextBody As String = "", Optional ReplyTo As String = "", Optional AttachedFiles As Variant = "") As String
On Error GoTo send_emailErr
    Dim ErrNum As Long
    Dim ErrDes As String
    SendEmailViaGmail = ""
    ErrNum = 0
    Set cdomsg = CreateObject("CDO.message")
    With cdomsg.Configuration.Fields
        .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2   'NTLM method
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
        .Item("http://schemas.microsoft.com/cdo/configuration/smptserverport") = 587
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
        .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = sendusername '
        .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = sendpassword
        .Update
    End With
    ' build email parts
    With cdomsg
        .To = SendTo
        .FROM = sendusername 
        .Subject = Subject
        .TextBody = TextBody & vbCrLf & vbCrLf & vbCrLf & "--" & vbCrLf & "Sent using Marlan Data-Systems"
        If IsArray(AttachedFiles) Then
            For Each AttachedFile In AttachedFiles
                If Len(AttachedFile) > 3 Then .AddAttachment AttachedFile
            Next
        Else
            If Len(AttachedFiles) > 3 Then .AddAttachment AttachedFiles
        End If
        .send
    End With
    SendEmailViaGmail = "Done!"
send_emailExit:
    Set cdomsg = Nothing
    Exit Function

send_emailErr:
    ErrNum = Err.Number
    ErrDes = Err.Description
    Select Case Err.Number

    Case -2147220977  'Likely cause, Incorrectly Formatted Email Address, server rejected the Email Format
        SendEmailViaGmail = "Please Format the Email Address Correctly."

    Case -2147220980  'Likely cause, No Recipient Provided (No Email Address)
        SendEmailViaGmail = "Please Provide an Email Address"

    Case -2147220960 'Likely cause, SendUsing Configuration Error
    SendEmailViaGmail = "SendUsing Configuration Error"

    Case -2147220973  'Likely cause, No Internet Connection
        SendEmailViaGmail = "Please Check Internet Connection"

    Case -2147220975  'Likely cause, Incorrect Password
        SendEmailViaGmail = "Please Check Password"

    Case Else   'Report Other Errors
        SendEmailViaGmail = ""
    End Select
    SendEmailViaGmail = SendEmailViaGmail & " Error number: " & Err.Number & " Description: " & Err.Description
    'If ErrNum = -2147220975 Then
    '    cdomsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smptserverport") = 465
    '    Resume
    'End If
    Resume send_emailExit
End Function

AttachedFiles is a String, or an Array of Strings, representing full paths to file or files that are to be attached to the email.
CDO.message is a Microsoft windows object.
You can replace value of smtpserver to some other mailing service. If you do so, please be sure to modify other parameters as well. Code is based on code I found on the web.