tags:

views:

163

answers:

4

Dear community.

I have written some simple code, to send en auto generated email, using the System.mail.Net namespace.

It works like a charm, but there is one little problem.

The mail does not get send, untill my whole application is terminated.

Does any of you have a workaround for this?

Here is the code I use (c#):

try
        {
            MailMessage mail = new MailMessage();

            mail.From = new MailAddress("[email protected]");
            mail.To.Add("[email protected]");

            mail.Subject = "Test test";
            mail.Body = "blah blah";

            mail.Attachments.Add(new Attachment("c:\\file.txt"));  

            SmtpClient smtp = new SmtpClient("myserver.mail.com");
            smtp.Send(mail);
        }
        catch (SmtpFailedRecipientsException ex)
        {
            Console.WriteLine(ex);
        }

As I said, everything works, but the mail is not send, untill I terminate the application.

Is there any way to force it to send the mail now?

The reason it is a problem, is both that I want the mail to be sent instantly without the user needing to reboot the application, but also because I want to delete the attachment after the mail has been sent, and when the mail isn't sent, the file is therefore marked as "in use" and can therefore not be deleted.

Best regards

/S

+1  A: 

Calling

smtp.Send(mail);

Sends the email to your smtp server immediately. If you're not seeing the email being sent as soon as that line executes, then I would check to see if your smtp server is functioning properly.

Here's more information on the SmtpClient.

Joseph
A: 

I've had the same problem, and using the workaround posted at http://social.msdn.microsoft.com/forums/en-US/netfxnetcom/thread/6ce868ba-220f-4ff1-b755-ad9eb2e2b13d/ seems to work:

For anyone interested, I've found a solution or at least a work around to the delay.

The ServicePoint member contains a member called MaxIdleTime. Setting this value to 0 seems to have the same affect as Timeout.Infinite (which I assume is also 0). Setting it to 1 caused my email to be sent out immediately.

See this link:

http://msdn2.microsoft.com/en-us/library/system.net.servicepoint.maxidletime.aspx

Example code:

SmtpClient smtp = new SmtpClient();

// setup the mail message

smtp.ServicePoint.MaxIdleTime = 1;

smtp.Send(myMailMessage);
Jeremy Frey
I would be very careful with this. See http://stackoverflow.com/questions/930236/net-best-method-to-send-email-system-net-mail-has-issuesfor more info
Chris Lively
Thanks to all who answered - this seemed to work :D
Sagi1981
+2  A: 

I wonder if your application has a lock on the attachment which is preventing the mail component from doing its work until your application starts shutting down. Can you verify that all locks are cleared prior to sending the mail?

You might start with a new small app that only sends an email. Once that it working properly, add a hard coded attachment. Verify it's still working. If everything is good, add your code for how you are handling attachments. If it breaks, tweak your code. Rinse and repeat. ;)

Chris Lively
A: 

I have seen this problem before. I had to turn off Norton Antivirus outgoing email scan to get emails to go right away.

HTH

unclepaul84