kvbSendMailLib Bla Bla Bla - Just Show Me The Code  
   
 
 

As featured in the November 2001 Access-VB-SQL Advisor Magazine, SendMail with Class. The SendMailLib COMponent is the product of that article, and is a drop-in replacement for CDONTS (CDO for NT Server). Actually, It's been beefed up a lot bit since it was originally published in that article. It gives you TWO different methods for sending outbound email, and the added bonus of being able to save messages to disk in the same manner as Outlook Express.

The first technique is the same method CDONTS uses for sending unauthenticated email, which can serialize a message to a common "Pickup" folder. Mail Servers such as Microsoft SMTP Service (IIS), Exchange Server, Merak Mail Server, etc., can scoop up messages from this folder and send them in the normal course of business. This can be done in a disconnected manner.

The second method uses a socket to send directly to an SMTP server via the SendMessage method. No Winsock control is required! SendMailLib uses the KillerVB WinsockLib to connect to the remote SMTP server anywhere over the LAN or WAN.

 
Why use one method over the other?

 

I actually use both. DropMessage saves a copy of my message to disk in the same .EML file format as Outlook Express does, while SendMessage sends the message directly to the SMTP server. It's nice to have a copy of the email for audit purposes. Also, a corporate network might have port 25 blocked except thru certain channels, which would preclude you from connecting directly to an SMTP server. This is becomming more common with large companies scared by recent virus alerts. When this happens, simply use DropMessage to serialize an .EML message to the pickup folder of your mail server (anywhere on the lan).

Mo Betta

 

Unfortunately, CDONTS.NewMail has a design flaw in that it chokes in tight loops, and frequently errors out on web servers because of multiple threads and CPU's. This is due to it's file naming convention which is based on the current time (FILETIME).

Moreover, it can only be called from a local NT-based server. This shuts out developers that want to use it in their client applications, or who test their ASP pages from a workstation running Personal Web Server (PWS). Big oversight on the CDONTS designers if you ask me (if I can do it, so can they).

Local & Remote Attachments

This library is unique in that you can attach local files, as well as remote files located on an FTP or HTTP server!

Custom Headers

Custom headers are a great place to hide tracking information that you don't want the average recipient to see. Add your own custom headers to stash a primary key or debugging information.

Extended Features 

This library also supports extended features such as importance, reply-by, reply-to, follow-up flags, delivery receipts, read receipts, and so on.

Sample Usage 


Dim Msg         As SendMail

' Build the email message.
Set Msg = New SendMail
With Msg
  
  ' Basic stuff
  .From = "KillerVB<noname@nospam.com>"
  .SendTo = "Joe Customer<noname@nospam.com>"
  .Cc = "Jane Customer<noname@nospam.com>"
  .Body = "Your order has been shipped. Please find attached statement."
  .Subject = "Your order #99"
  .ReplyTo = "Who Cares<noname@whocares.com>"
  
  ' Add some hidden tracking stuff
  .AddHeaderItem "X-CustomerId", "8675309"
  
  ' Add follow-up flag
  .FlagFollowUp = DateAdd("d", 7, Now)
  
  ' Importance
  .Importance = smHighImportance
      
  ' Attach files
  .AttachURL "ftp://MyFtpSite.com/downloads/packslip.gif"
  .AttachFile "c:\temp\stmt.pdf"

  ' Drop it to the IIS or Exchange SMTP Pickup folder:
  .DropMessage "\\ServerName\C$\Inetpub\mailroot\Pickup"

  ' -Or- send it off to an SMTP Server using a socket:
  .SmtpConnect Host:="mail.nospam.com", Port:=25
  .SendMessage
  .Disconnect

End With

 
kvbSendMailLib Code View License   The Code
kvbSendMail.Vbp
kvbSendMail Sample.Vbp
 
 
 
 
Visitor CommentsPost Comment
Peter
This sample project can't run.It encounted error "ActiveX control can't creat..." error.It seems you used Winsock control? Public Sub SmtpConnect( _ ByVal RemoteHost As String, _ Optional ByVal port As Long = 25) ' Socket should be closed. If m_Socket Is Nothing Then Set m_Socket = New Winsock ... End Sub You claim this DLL is independ on Microsoft Winsock but why you should declare "Private WithEvents m_Socket As Winsock"? Thanks.
Paul de Courcel
It seems to be necessary to add "<" and ">" before and after addresses MAIL FROM: and RCP TO: to avoid the "Wrong protocol or connection state ..." message. So, replace SendCommand("MAIL FROM:" & StripFriendlyName(m_SendTo)) by SendCommand("MAIL FROM:" & "<" StripFriendlyName(m_From) & ">") and SendCommand("RCPT TO:" & StripFriendlyName(m_SendTo)) by SendCommand("RCPT TO:" & "<" & StripFriendlyName(m_SendTo) & ">") and idem for m_Cc and m_Bcc. Thanks for this very good library !
Dimitris Papadimitriou
Great tool! The only thing I found to be missing is the option to use an smtp server which requires authentication!
Monte Hansen - KillerVB.Com
Released a new version that corrects a bug related to CC and BCC recipients. Cheers to Christof Ugau for reporting this problem.
Dan Hagerman
Hey KillerVB, nice job! The drop function is a great alternative for sending mail. One important note to users, however. If you attempt to attach more than 4 files, you will eventually receive an error 75 Path/File Access Error when dropping the file. This is because the array that holds the attachment info gets re-dimensioned but not preserved when approaching the 5th attachment. Adding the Preserve keyword in the Redim line solves the problem.
Peter
Blows CDO SendMail away! I really like being able to send mail with TCP/IP, while still being able to save it to disk. The FTP attachments thing is a nice bonus. Keep up the good work guys!
Shaun Hess - TaxWorks
How can one send to multiple recipients? I can't seem to figure out how to delimit the email addresses in any of the [to] fields. I've tried comma, vbCrLF, space. It blows up when I try any of them. As a work-around, I created a group on our mail server, then I can send to the group. Other than that, this is a great piece of code. Thanks, Shaun Hess
Darrell R. Pitzer
I have had the SendMessage method hang on me several times. I thought that there was a timeout that would kick in to prevent something like this. I'm using the SendMailLib from a Sub Main "batch" type program. Since I could not capture events from a BAS module, I did not know what was being sent to and received from the SMTP server. Recently, I built a wrapper class to pick up the events from SendMailLib and write them to a file. The application has not hung since then. I am, however, seeing a number of event messages like this "!!! Error: Application-defined or object-defined error" just before the message is accepted for delivery. Does this error mean anything to you?
Jonathan
great code, but sendmail example doesn't seem to .Disconnect from the mail server. Many thanks
Miles
How to send a HTML body?
Pete Mitchell
This is a slick piece of code, although the recipient I list in the .CC property never get their mail. Thanks for the short cut.
 
Copyright © 1999-2002 by Monte Hansen, All Rights Reserved Worldwide.