Attribute VB_Name = "EmailBlat" Option Explicit #Const SendEmailMessageDebug = 0 ' -1 ' 0 ' -1 ' 20100317: do NOT use a fixed location for this DLL! 'Declare Function SendBlat Lib "c:\windows\system32\blat.dll" Alias "Send" (ByVal sCmd As String) As Integer Declare Function SendBlat Lib "blat.dll" Alias "Send" (ByVal sCmd As String) As Integer Public Function SendEmailViaBlat(ByVal sAddressee$, ByVal sNameOfFileContainingMessageText$, ByVal sSmtpServerName$, ByVal sSmtpPortNumber$, ByVal sEmailUserName$, ByVal sEmailPassword$, ByVal sEmailAddressOfSender$, Optional ByVal sTextNameOfSender$, Optional ByVal sSubjectLine$, Optional ByVal sNameOfFileToAttach$) As Integer Dim sCmd$, sQuote$, sOptionalQuote$ Dim iRc% 'Debug.Print "SendEmailViaBlat" 'Debug.Print "Recipient: " + sAddressee$ 'Debug.Print "File: " + sNameOfFileContainingMessageText$ #If SendEmailMessageDebug = -1 Then sOptionalQuote$ = "" ' Chr(34) #Else sOptionalQuote$ = "" #End If sQuote$ = Chr(34) ' BLAT parameters, from some documentation we dug up ' ' : file with the message body ('-' for console input, end with ^Z) '-to : recipient list (also -t) (comma separated) '-tf : recipient list filename '-subject : subject line (also -s) '-f : overrides the default sender address (must be known to server) '-i : a 'From:' address, not necessarily known to the SMTP server. '-cc : carbon copy recipient list (also -c) (comma separated) '-cf : cc recipient list filename '-bcc : blind carbon copy recipient list (also -bcc) (comma separated) '-bf : bcc recipient list filename '-organization : Organization field (also -o and -org) '-body : Message body '-x : Custom 'X-' header. eg: -x "X-INFO: Blat is Great!" '-r : Request return receipt. '-d : Request disposition notification. '-h : displays this help. '-q : supresses *all* output. '-debug : Echoes server communications to screen (disables '-q'). '-noh : prevent X-Mailer header from showing homepage of blat '-noh2 : prevent X-Mailer header entirely '-p : send with SMTP server, user and port defined in . '-priority : set message priority 0 for low, 1 for high. '-server : Specify SMTP server to be used. (optionally, addr:port) '-port : port to be used on the server, defaults to SMTP (25) '-hostname : select the hostname used to send the message '-mime : MIME Quoted-Printable Content-Transfer-Encoding. '-enriched : Send an enriched text message (Content-Type=text/enriched) '-html : Send an HTML message (Content-Type=text/html) '-uuencode : Send (binary) file UUEncoded '-base64 : Send (binary) file using base64 (binary Mime) '-try : how many time blat should try to send. from '1' to 'INFINITE' '-attach : attach binary file to message (may be repeated) '-attacht : attach text file to message (may be repeated) '-ti : Set timeout to 'n' seconds. '-u : Username for AUTH LOGIN (use with -pw) '-pw : Password for AUTH LOGIN (use with -u) '-log : Log everything but usage to '-plain : Use AUTH PLAIN to login to SMTP server (use with -u -pw) '-charset : User defined charset. The default is ISO-8859-1 ' 'Note that if the '-i' option is used, is included in 'Reply-to:' 'and 'Sender:' fields in the header of the message. ' 'Optionally, the following options can be used instead of the -f and -i options: '-mailfrom The RFC 821 MAIL From: statement '-from The RFC 822 From: statement '-replyto The RFC 822 Reply-To: statement '-returnpath The RFC 822 Return-Path: statement '-sender The RFC 822 Sender: statement 'For backward consistency, the -f and -i options have precedence over these 'RFC 822 defined options. If both -f and -i options are omitted then the 'RFC 821 MAIL FROM statement will be defaulted to use the installation-defined default sender address #If SendEmailMessageDebug = -1 Then Debug.Print "SendEmailViaBlat arrival with:" Debug.Print "To: " + sAddressee$ Debug.Print "From: " + sEmailAddressOfSender$ If Len(sSubjectLine$) > 0 Then Debug.Print "Re: " + sSubjectLine$ End If Debug.Print "File: " + sNameOfFileContainingMessageText$ Debug.Print "Server: " + sSmtpServerName$ + ":" + sSmtpPortNumber$ If Len(sEmailUserName$) > 0 Then Debug.Print "User: " + sEmailUserName$ + ":" + sEmailPassword$ End If If Len(sNameOfFileToAttach$) > 0 Then Debug.Print "Attach: " + sNameOfFileToAttach$ End If #End If ' 20060104: trim unnecessary leading angle brackets from ' the send-to address sAddressee$ = Trim(sAddressee$) Do While Mid(sAddressee$, 1, 1) = ">" DoEvents sAddressee$ = Trim(Mid(sAddressee$, 2)) Loop sCmd$ = sOptionalQuote$ + Trim(sNameOfFileContainingMessageText$) + sOptionalQuote$ #If SendEmailMessageDebug = -1 Then sCmd$ = sCmd$ + " -l " + sOptionalQuote$ + "c:\d\work\blatlog.txt" + sOptionalQuote$ #End If sCmd$ = sCmd$ + " -server " + sOptionalQuote$ + Trim(sSmtpServerName$) + sOptionalQuote$ If Len(sSmtpPortNumber$) > 0 Then sCmd$ = sCmd$ + " -port " + Trim(sSmtpPortNumber$) End If If Len(sEmailUserName$) > 0 Then sCmd$ = sCmd$ + " -u " + sOptionalQuote$ + Trim(sEmailUserName$) + sOptionalQuote$ If Len(sEmailPassword$) > 0 Then sCmd$ = sCmd$ + " -pw " + sOptionalQuote$ + Trim(sEmailPassword$) + sOptionalQuote$ End If End If sCmd$ = sCmd$ + " -f " + sOptionalQuote$ + Trim(sEmailAddressOfSender$) + sOptionalQuote$ sCmd$ = sCmd$ + " -to " + sOptionalQuote$ + Trim(sAddressee$) + sOptionalQuote$ If Len(sTextNameOfSender$) > 0 Then sCmd$ = sCmd$ + " -i " + sOptionalQuote$ + Trim(sTextNameOfSender$) + sOptionalQuote$ Else 'iRc% = InStr(sEmailAddressOfSender$, "@") 'If iRc% > 0 Then ' sCmd$ = sCmd$ + " -i " + sOptionalQuote$ + Trim(Mid(sEmailAddressOfSender$, 1, iRc% - 1)) + sOptionalQuote$ 'Else sCmd$ = sCmd$ + " -i " + sOptionalQuote$ + Trim(sEmailAddressOfSender$) + sOptionalQuote$ 'End If End If sSubjectLine$ = Trim(sSubjectLine$) If Len(sSubjectLine$) > 0 Then 'If InStr(sSubjectLine$, " ") > 0 Then sCmd$ = sCmd$ + " -s " + sQuote$ + Trim(sSubjectLine$) + sQuote$ 'Else ' sCmd$ = sCmd$ + " -subject " + sSubjectLine$ 'End If End If If Len(sNameOfFileToAttach$) > 0 Then sCmd$ = sCmd$ + " -attach " + sOptionalQuote$ + Trim(sNameOfFileToAttach$) + sOptionalQuote$ End If '#If SendEmailMessageDebug = -1 Then Debug.Print "Assembled command line: >" + sCmd$ + "<" '#End If ' must be null-terminated sCmd$ = sCmd$ + Chr(0) iRc% = SendBlat(sCmd$) #If SendEmailMessageDebug = -1 Then Debug.Print "SendBlat return code: " + str(iRc%) #End If SendEmailViaBlat = iRc% ' return codes are defined here: http://www.geocities.com/toby_korn/blat/blat_return_codes.htm ' here's a summary: '0 OK '1 Unable to open SMTP socket. ' SMTP get line did not return 220. ' Command unable to write to socket. ' Server does not like To: address. ' Mail server error accepting message data. ' File name (message text) not given. ' Bad argument given '2 The server actively denied our connection. ' The mail server doesn't like the sender name. ' File (message text) does not exist '3 Error reading the file (message text) or attached file '4 File (message text) not of type FILE_TYPE_DISK '5 Error Reading File (message text) '12 -server or -f options not specified and not found in registry '13 Error opening temporary file in temp directory 'Codes From /* $Id: gensock.h 1.8 1995/01/25 23:28:11 rushing Exp $ */ 'See C code with text descriptions '4001 ERR_CANT_MALLOC '4002 ERR_SENDING_DATA '4003 ERR_INITIALIZING '4004 ERR_VER_NOT_SUPPORTED '4005 ERR_EINVAL '4006 ERR_SYS_NOT_READY '4007 ERR_CANT_RESOLVE_HOSTNAME '4008 ERR_CANT_GET_SOCKET '4009 ERR_READING_SOCKET '4010 ERR_NOT_A_SOCKET '4011 ERR_BUSY '4012 ERR_CLOSING '4013 WAIT_A_BIT '4014 ERR_CANT_RESOLVE_SERVICE '4015 ERR_CANT_CONNECT '4016 ERR_NOT_CONNECTED '4017 ERR_CONNECTION_REFUSED '-5000 ERR_NO_ERROR_CODE 'This is returned by misbehaving stacks that fail, but don't set an error code End Function