Send SMTP mail from MSAccess using Blat

by Joel Joel Limardo asked me to host this file on the blat web site as his web site will no longer be supporting it...
Blat is a freeware mail tool that is available online. You can use it along with a working SMTP server to mail directly from within a program. All you have to specify are a few parameters and you are done. Before you use any of the code you find here, be sure that you have thoroughly read Blat's documentation file and have correctly installed and tested Blat by sending yourself some e-mail. Once you know that Blat works, you can then use this class and the test function to test Blat from within MSAccess.

What do I need this for?

Beyond it being really cool to be able to mail directly from within MSAccess, you do not have to use or buy an Exchange Server provided you have access to a working SMTP server. Usually your ISP will offer SMTP access (Prodigy's is smtp.prodigy.net for instance) at no extra charge. So why not use what you are already paying for? The first section of the following code is from a class called clsErrorMailer. It is so named because it is a part of a special error handling solution we are working on (we'll post more of that later). You are welcome create a class module with any name you like, but remember to update the subroutine called 'Test()' after the dashed-line accordingly.

' /* ====================================================================
 '* The ForwardPhase Technology Network Software License, Version 1.1
 '*
 '* Copyright (c) 2003 The ForwardPhase Technologies, LLC.  All rights
 '* reserved.
 '*
 '* Redistribution and use in source and binary forms, with or without
 '* modification, are permitted provided that the following conditions
 '* are met:
 '*
 '* 1. Redistributions of source code must retain the above copyright
 '*    notice, this list of conditions and the following disclaimer.
 '*
 '* 2. Redistributions in binary form must reproduce the above copyright
 '*    notice, this list of conditions and the following disclaimer in
 '*    the documentation and/or other materials provided with the
 '*    distribution.
 '*
 '* 3. The end-user documentation included with the redistribution,
 '*    if any, must include the following acknowledgment:
 '*       "This product includes software developed by the
 '*        ForwardPhase Technologies Network 
 '*        (http://www.forwardphase.com/)."
 '*    Alternately, this acknowledgment may appear in the software itself,
 '*    if and wherever such third-party acknowledgments normally appear.
 '*
 '* 4. The names "ForwardPhase" and "ForwardPhase Technologies Network" 
 '*    must not be used to endorse or promote products derived from this
 '*    software without prior written permission. For written
 '*    permission, please contact licensing@forwardphase.com.
 '*
 '* 5. Products derived from this software may not be called 
 '*    "ForwardPhase", nor may "ForwardPhase" appear in their name, 
 '*    without prior written permission of ForwardPhase Technologies, LLC.
 '*
 '* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 '* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 '* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 '* DISCLAIMED.  IN NO EVENT SHALL FORWARDPHASE TECHNOLOGIES, LLC OR
 '* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 '* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 '* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 '* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 '* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 '* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 '* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 '* SUCH DAMAGE.
 '* ====================================================================
 '*
 '* This software consists of voluntary contributions made by many
 '* individuals on behalf of ForwardPhase Technologies, LLC.  For more
 '* information on the ForwardPhase Technologies Network, please see
 '* http://www.forwardphase.com.
 '* * 
 '*/

Option Compare Database
Option Explicit
Private p_blat_location As String
Property Let blat_location(strblat_location As String)
    p_blat_location = strblat_location
End Property
Property Get blat_location() As String
    blat_location = p_blat_location
End Property

Public Property Let Toggle(Decision As Boolean)
    If Decision = True Then
        Call SaveSetting("ErrorMailer", "ClassErrors", "Toggle", "True")
    ElseIf Decision = False Then
        Call SaveSetting("ErrorMailer", "ClassErrors", "Toggle", "False")
    End If
End Property

Private Sub Class_Initialize()
    'default blat location...be sure to set this property
    p_blat_location = "c:\temp\blat\blat.exe"
End Sub

Private Function GetToggleState() As Boolean
    Dim p As String
    p = GetSetting("ErrorMailer", "ClassErrors", "Toggle")
    If p = "True" Then
        GetToggleState = True
    Else
        GetToggleState = False
    End If
End Function

Public Sub EmailError(fromVar As String, ToVar As String, server As String, 
fileToSend As String)
   Dim x As String
   x = p_blat_location & " " & fileToSend & " -s frommsaccess " & _
    "-t " & ToVar & " -f " & fromVar & " -server " & server
    Debug.Print x
    
    If GetToggleState = True Then
        Shell x, vbHide
    Else
        'do nothing
    End If
End Sub

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

Sub test()
    On Error GoTo Errorhandler
    Dim objErrMail As New clsErrorMailer
    objErrMail.Toggle = True
    objErrMail.blat_location = "C:\Temp\blat\blat.exe"
    Call objErrMail.EmailError("me@mymail.com", "foobar@yourdomain.com", _
        "smtp.mail.yahoo.com", "c:\temp\crasherrors.txt")
StandardExit:
Exit Sub
Errorhandler:
    MsgBox Err.Description
    GoTo StandardExit
End Sub


Blat Tip:  Blat is not an Active X Component, but rather, a standalone program that can be used from the command line. The above class simply makes a shell call to the separate program. This means you will not know if the process failed or not. Thus, you should consider designing a log file with messages, timestamps, etc. to ensure reciept if these messages are very important.