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();
}
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();
}