Exception when using threads to send several mails at the same time

Migrating question from old forum:

Error:
SMTP_Singleton_DIAGNOSTIC_LOGGER_NAME

Code:

Public Class Form1

    Private _token As Threading.CancellationToken

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim semaphore As New Threading.SemaphoreSlim(5, 5)

        For i As Integer = 0 To 20

            Task.Factory.StartNew(Sub()
                                      Try
                                          semaphore.Wait(Me._token)
                                          If Me._token.IsCancellationRequested Then Exit Sub
                                     
                                          Using client As New SmtpClient
                                              With client
                                                  .Host = "smtp.office365.com"
                                                  .Username = "bla"
                                                  .Password = "bla"
                                                  .Port = 587
                                                  .SecurityOptions = SecurityOptions.SSLExplicit
                                              End With

                                              Dim mail As Aspose.Email.MailMessage = Aspose.Email.MailMessage.Load(My.Application.Info.DirectoryPath + "\mail.msg")
                                              client.Send(mail)
                                              mail.Dispose()
                                              mail = Nothing
                                          End Using

                                      Catch ex As Exception
                                          Console.WriteLine(ex.ToString)
                                      End Try
                                  End Sub, Me._token).ContinueWith(Function(x) semaphore.Release())


        Next

    End Sub

End Class

Sample project:
AsposeThreadProblem.zip (49.7 KB)

Please note taht I have pm to Ikram on the old post, but since we can’t reply in old forum, I’m moving my question to here.

Hi,

We have tested your sample project attached here with this thread, but were not able to reproduce the exception at our end. We just modified the server and account credentials information and tested the same at our end with Gmail SMTP which didn’t raise any exception. Could you please make sure that you are using the latest version of the API at your end?

I have no problem replecating it.

See this screencast:

Using this code:

Public Class Form1

    Private _token As Threading.CancellationToken

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim semaphore As New Threading.SemaphoreSlim(5, 5)

        For i As Integer = 0 To 20

            Task.Factory.StartNew(Sub()
                                      Try
                                          semaphore.Wait(Me._token)
                                          If Me._token.IsCancellationRequested Then Exit Sub

                                          Using client As New SmtpClient
                                              With client
                                                  .Host = "smtp.office365.com"
                                                  .Username = "bla"
                                                  .Password = "bla"
                                                  .Port = 587
                                                  .SecurityOptions = SecurityOptions.SSLExplicit
                                              End With
                                          End Using
                                      Catch ex As Exception
                                          Console.WriteLine(ex.ToString)
                                      End Try
                                  End Sub, Me._token).ContinueWith(Function(x) semaphore.Release())

        Next

    End Sub

End Class

Could you please share if it raises exception even if you provide valid account credentials?

It does.

Even this code fails:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

Dim semaphore As New Threading.SemaphoreSlim(5, 5)

For i As Integer = 0 To 20

  Task.Factory.StartNew(Sub()
  						  Try
  							  semaphore.Wait(Me._token)
  							  If Me._token.IsCancellationRequested Then Exit Sub
  							  Using client As New SmtpClient                                            
  							  End Using
  						  Catch ex As Exception
  							  Console.WriteLine(ex.ToString)
  						  End Try
  					  End Sub, Me._token).ContinueWith(Function(x) semaphore.Release())

Next

End Sub

Screenshot (26.9 KB)

Error is something like … Element was already added. Key in dictionary ‘SMTP_Singleton_DIAGNOSTIC_LOGGER_NAME’

Hi,

Despite all our efforts and testing with different .NET frameworks, we are still not able to reproduce the issue at our end. Could you please try it on another PC and let us know your feedback? Meanwhile, an investigation ticket with id: EMAILNET-38773 has been logged in our issue tracking system for further investigation by our Product team. We’ll update you here once there is some information available in this regard.

That is weird.

I just installed Windows 10 and V2017 in Parallels (so it’s a clean installation) … it fails immediately.

Strang you can’t replicate it - I have no problems replicating it here.

Hi Morten,

Since the issue has already been logged, our Product team will further look into it for any such possible chance of exception. We’ll update you here once there is some information available in this regard.

The issues you have found earlier (filed as EMAILNET-38773) have been fixed in this update. This message was posted using BugNotificationTool from Downloads module by kashif.iqbal

@Morajo,

We have just released Aspose.Email for .NET 17.8 where this issue has been fixed. Please try it at your end and let us know if you have any further inquiry in this regard.