Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a program I have written that works great. It sends an email when something occurs.

The problem I am running into is the email isn't sent until after I close the program. Not sure how to make the email send immediately.
Any assistance would be greatly appreciated.

Here is the code:
VB
Public Sub File_Deleted(ByVal obj As Object, ByVal e As FileSystemEventArgs)
        Select Case e.ChangeType
            Case WatcherChangeTypes.Deleted
                If InStr(e.Name, ".") < 1 Then
                    'SendEmail(e.FullPath & " - Deleted")
                    Dim SmtpServer As New SmtpClient()
                    Dim mail As New MailMessage()
                    'SmtpServer.Credentials = New  _
                    'Net.NetworkCredential("username@gmail.com", "password")
                    '    SmtpServer.Port = 587
                    'SmtpServer.Host = "smtp.gmail.com"
                    SmtpServer.Host = "smtpserver"
                    mail = New MailMessage()
                    mail.From = New MailAddress("Joe@homesweethome.com")
                    mail.To.Add("Joe@homesweethome.com")
                    mail.Subject = e.FullPath & " - Deleted"
                    mail.Body = "This is for testing SMTP mail"
                    SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network
                    SmtpServer.Send(mail)
                    'SmtpServer.SendAsync(mail, New Object)
                    'MsgBox("mail send")
                    mail = Nothing
                    SmtpServer = Nothing
                    MsgBox(e.FullPath & " - Deleted")
                End If
        End Select

    End Sub
Posted
Updated 9-Jun-11 5:25am
v2
Comments
Sandeep Mewara 9-Jun-11 11:26am    
Elaborate more on The problem I am running into is the email isn't sent until after I close the program. Not sure how to make the email send immediately
Member 2244190 9-Jun-11 14:44pm    
Ok.

So the program runs and the code executes to send the email and contiues on.

The emails aren't actually recieved until after the program closes.

I was testing on my development machine, the code executed perfect no errors, but I didn't receive the email in my inbox until after the program closed.

This happened everytime I did run the program no matter how long I waited before closing the program.

Roger

1 solution

I think the answer is here:

http://weblogs.asp.net/scottgu/archive/2005/12/10/432854.aspx[^]

Someone named Russ has the same problem and figured it out:
In case anyone is interested, I tracked down the cause of the problem I mentioned above with system.net.mail not sending outgoing messages until the application ends.

It turned out being related to Symantec AntiVirus (Full version 9.0.0.338) that I have running on my machine. I was able to eliminate the problem by turning off the "Internet E-Mail Auto-Protect" feature. Turning it back on resulted in the same send delay. Using the system.web.mail namespace does not exhibit this problem (despite the IDE's warnings about it being obsolete.)
 
Share this answer
 
Comments
Member 2244190 9-Jun-11 16:26pm    
Thanks for that advice. I did look into my Virus Protection. I have Symantec Endpoint Protection. Unfortunately at the client level I cannot disable the Internet Email Auto Protect, but I will be checking if I can find somenoe who can and if it works there, but I believe this to be the answer.

Thanks for you assistance in this.

Roger

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900