VB, ASP and sending email

Posted on 2001-06-05
Last Modified: 2006-11-17
I am working on a NT4 Server, with ActiveXDLL in VB, calling VB from ASP.  At a certain time at night our email server goes down, and then when members try to send an email from our site to our Inbox the following error occurs:
The information store could not be opened. [MAPI 1.0 - [MAPI_E_LOGON_FAILED(80040111)]]RCIJHB001

This never happened initially when we wrote this project, but after putting out a new exe one day, it started giving these errors.  I'm thinking of a work around or solution where when the emails are sent, they only get stored in the outbox or something, and as soon as the server comes up, they get sent.  Almost the same as when you use outlook express and press the send/receive button to send email.  

Any suggestions on this?
Question by:MLove
  • 2

Expert Comment

ID: 6155994

Accepted Solution

dalexis earned 170 total points
ID: 6156657
(I appologize... this will be a bit long winded.)  :)

You can gain the kind of fault tollerance you want by creating an out-of-process (EXE) COM component that acts like a queue for the email. It needs to be out of process because you want it hanging around, retrying against the email server until it successfully sends all the queued emails.

The solution may seem a little inelegant, but such are the limitations of implementing this type of thing in VB.  (A C++ sulution would be more work, but would be much cleaner.)

The component needs to be follow the Singleton design pattern (Multiuse, single threaded).  Basically, the component will accepts requests from your ASP code to send emails.  It will add each request to a queue, which can be implemented using a Collection or array internally.  It will then asynchronously iterate thru the queued requests, attempting to send each email thru the email server.  If it encounters an error (as in when the server is down) it will wait for a period of time then attempt to send emails again.  Once the last email in the queue has been sent, the component kills itself.

That's the high level overview of what the component would do.  Let's drill into slightly more detail now...

COM would load the component when it is instantiated, but will destroy it when it goes out of scope in your ASP script.  This is not good, since the component may not have sent all emails successfully yet.  COM keeps an object loaded as long as there is at least 1 reference to it.  This is why you need the Form object.  All you need to do is Load the Form, and due to some quirkiness with the way VB handles Forms, COM thinks that the component still has a reference.  Therefore the component is not destroyed until the Form un Unloaded. :)

This said, when the first request comes in to your SendEmail method, simply Load the Form.  (Do NOT Show the form!  The component will be running as a background process on the server with no UI.) When the last email has been sent, Unload the Form.

Ok, now that we've fooled our component into hanging around after teh ASP script has finished with it, we need to set up the asynchronous mechanism to process the email requests in the background.  This is where our dummy Form comes in handy again.  Place a Timer control on the form.  This timer will be used to fire off a method to process the queued requests in the background.  The Timer's event handler will simply disable the timer, call the method to process the queue, then turn itself on again.  It should also check to see if all the queued requests have been sent.  If the queue is empty, it must disable the Timer and Unload the form.

Now that the Timer and the queue-processing event handler are in place, let's revisit the SendMail method.  It has a simple algorithm - Add the request details (to, from, subject, body, etc) to the queue object, Load the dummy Form (if it is not already loaded), and enable the Timer.  At this point, control returns to the your ASP script and life is good.

When the Timer interval has expired, the event handler fires and starts processing email requests in the background.

Once the basic functionality is working, you can add things like error logging, failsafe timeouts in case the server does not come back up within a given period of time, etc, etc.

Have fun! :)

Author Comment

ID: 6158643
I will go and do this, sounds very cool.  Would you mind if I email you with any problems?

Expert Comment

ID: 6159577
Sure.  Glad to be of any help.  My address is

Good luck, and happy coding! :)

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now