SMTP Connection takes time to connect after Dispose

206 views Asked by At

I have an issue with an App i made to send out Emails in Bulk. The steps which are taken are described below:

  • First, the SMTP connection is made (anonymous access is used)
  • The app sends out 10 emails per batch (10 is the limit the SMTP accepts for each connection)
  • When 10 emails are reached, the SMTP session is disposed and new one is created.

The problem i am facing is that after 3rd batch, i start to face alot of Time out issues from the SMTP(i.e the app either says timeout Or Failure to sent email) and this goes out till the end and there are about 20 to 30 failures out of 221 emails.

Below is the code i have written for this:

      Try

        'create the mail message
        Dim mail As New MailMessage()

        'set the addresses

        mail.From = New MailAddress("[email protected]")


        If (ConfigurationManager.AppSettings("Setup").Equals("Testing")) Then
            mail.[To].Add("[email protected]")
            'mail.[To].Add("[email protected]")
        Else
            mail.[To].Add(recepient)
            mail.[Bcc].Add("[email protected]")
        End If

        'set the content

        mail.Subject = subject

        mail.Body = body


        smtp.Port = 25



        'send the message


        objWriter.WriteLine("Case No: " + CaseNo + "From: " + mail.From.ToString + "           " + "To: " + mail.To.ToString + "       " + "subject: " + mail.Subject.ToString + "  Email Sent")
        Console.WriteLine("Email Sent to the address: " + recepient)

        ' Update handback email column
        Updatehandback(CaseNo, "Yes")


        mailcount = mailcount + 1

    Catch ex As Exception
        ex.StackTrace.ToString()
        'objWriter.WriteLine(ex.Message.ToString + " " + ex.InnerException.ToString)
        objWriter.WriteLine(ex.Message.ToString)
        objWriter.WriteLine("Case No: " + CaseNo + "  Email Not Sents")
        ' Update handback email column
        Updatehandback(CaseNo, "No")
    End Try



    ' Check to see if mail count has reached 9 
    If (mailcount = 9) Then
        Console.WriteLine("***************MailCount Exhausted, Re-creating SMTP link******************")
        smtp.Dispose()
        mailcount = 0
        Threading.Thread.Sleep(18000)
        smtp = New SmtpClient("87.101.183.161")
    End If

Pleas, any help on this would be highly appreciated as i have been stuck in this for a week, i have already contacted SMTP provider and they say that everything has been configured correctly at their end and they are not facing any issues with other clients who use the same SMTP server for Emails.

Also, the SMTP object is a static Object before the start of the code and is being re-used.

Thanks.

1

There are 1 answers

0
Haris Tasawar On BEST ANSWER

Well i resolved the issue by buying an API. If anybody wants to know, its from afterlogic called Mailbee and that usually solved the problem.