SMTP and CDO.Message Object error

B

Brian Bischof

In my Win app I found that using the SMTP object doesn't release resources after closing the application. During testing I have to open and close the app many times and I found that after about three times it locks up when I call the Send() method and returns the error "Could not access CDO.Message Object". I found that if I create a new Win App and copy and paste the code into it, then it runs fine for the first couple of times and then starts returning that error. I also noted that I can send as many email messages as I want and they all get sent with no error as long as I don't close the program. Unfortunately, not closing the program isn't possible and I'm creating new apps constantly as I test my code.

I have two theories. The first being that the program doesn't release SMTP resources and after a couple times running all the resources are used up. But if that's the case, then why does creating a new app always work? My second theory is that somehow SMTP holds a pointer the application calling it and it only lets each app have a couple pointers allocated to it. That's why new apps always work but then get locked out after a couple tests.

I tried using garbage collection to free resources, but the SMTP object is a WinNT object and I can't instantiate it directly. So garbage collection hasn't helped solve the problem.

After doing more testing, I found that if I leave the SMTP.ServerName empty, then it works everytime. The problem with this is that since I'm not specifying the corporate email server, then this isn't a reliable method for a production system. So this isn't a practical fix.

Any ideas?


Here is my test code:


static void Main()
{
Application.Run(new Form1());
System.GC.Collect();
}

private void Form1_Load(object sender, System.EventArgs e)
{
Send([email protected]", (e-mail address removed)", "","test 2", "hello");
}

public void Send(string From, string To, string CC, string Subject, string Body)
{
System.Web.Mail.MailMessage Email=new MailMessage();
Email.From=From;
Email.To=To;
Email.Cc=CC;
Email.Subject=Subject;
Email.Body=Body;
Send(Email);
Email=null;
}

public void Send(System.Web.Mail.MailMessage Email)
{
SmtpMail.SmtpServer = "smtp.ucsd.edu";
try
{
SmtpMail.Send(Email);
}

catch(Exception e)
{
string x;
x=e.Message;
MessageBox.Show(x);
}
}

private void Form1_Closed(object sender, System.EventArgs e)
{
System.GC.Collect();
}
 
C

Cor Ligthert [MVP]

Brian,

Any question about SMTP and CDO are difficult to answer, all was it alone by
the fact that there are four different CDO products.

- CDO.DLL : CDO version 1.2.1
- CDONTS.DLL : CDO version 1.2.1 for Windows NT Server (not the same as CDO
version 1.2.1!)
- CDOSYS.DLL : CDO for Windows 2000
- CDOEX.DLL : CDO for Exchange 2000 Server

Than there are as well the security settings, which could be overuled for
the 2000 version however from after that time I have not seen much here.

However in your case I would try it in the newsgroup
microsoft.public.dotnet.languages.csharp

(And do it in plain text, a lot of people have newsreaders in which the way
you sent it (HTML) is garbage)

By the way showing that newsgroup is not because that you are not welcome
here, however to give you a change on more answers.

I hope this helps a very little bit.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top