tags:

views:

20

answers:

1

I am trying to send a message with attachment using CDO object. When the SMTP Server is available and all the information is correct, the message is correctly sent with the attachment.

However, if the SMTP Server is incorrect the message is not sent (as expected), but it seems to be "stuck" somewhere. I am using:

Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2

I've searched over the Internet, and found that this option would give a 60 second timeout. But the file I attached to the message is never available.

The test that I've done is to send a message with an attached file and using an invalid SMTP Server. Then, I wait for a few minutes and try to delete the file I had attached. However, when I try do it, I have a permission problem. When I kill the sending email program, I am able to delete the file.

I want to know how to configure the timeout to make sure it gives up sending the message, how I "detach" the file when the message is not sent and how to make the program wait for the message to be sent (I want to send the message and then erase the attached file from the computer. So I need to know when the message was really sent or when it was timedout).

A: 

CDO is hopelessly obsolete, you really need to consider switching to System.Net.Mail. The specific problem sounds like a file locking issue. Quacks like a bug in CDO, it would open the attachment to compose the email message but forgets to close the file when the SMTP server balks.

This bug is probably exacerbated by the way .NET deals with COM servers, like CDO. The COM object doesn't get released until the garbage collector runs. Which can take a while, especially when your program doesn't do anything significant after trying to send the email. A workaround for that is calling Marshal.ReleaseComObject() on the CDO object. Tends to not work when you have other CDO interface references in your program, those references tend to be hidden. GC.Collect() + GC.WaitForPendingFinalizers() is the big hammer, after you nulled any object reference.

But, really, use System.Net.Mail.

Hans Passant
I will try to use System.Net.Mail, because I don't think the problem is the garbage collector, because we do clean up the objects after sending (or trying to send) the email.Thank you for the help!
jpnavarini