0

I've taken over a project, and after sending an email, the details are supposed to be logged along with whether or not the email send was successful.

However, I noticed that the subroutine to log the data is never being called. So, I tried to include this in the bottom of the email send code like so

Smtp_Server.Send(e_mail) ' Here, Smtp_Server is System.Net.Mail.SmtpClient
                         ' e_mail is System.Net.Mail.MailMessage

sendComplete(Me, New AsyncCompletedEventArgs)   ' This is the call for the log subroutine

However, the bottom line gives the error:

'Public Sub New()' is obsolete: 'This API supports the .NET framework infrastructure and is not intended to be used directly from your code

This is the start of the sendComplete subroutine.

Public Sub sendComplete(ByVal sender As Object, e As AsyncCompletedEventArgs)

Try
   If e.Error IsNot Nothing Then
      PictureBox1.Visible = False
      MsgBox("Failed to send email!", MessageBoxIcon.Warning + MsgBoxStyle.OkOnly, "Error")
      mailSent = True

What do I need to put into the second parameter to make the code call the subroutine? Or does the sendComplete subroutine itself need changing?

EDIT

After adding the following Try...Catch statement to the code, as suggested, the code executes fine and doesn't hit the Catch, however, after sending the message, doesn't go to the sendComplete subroutine.

Smtp_Server.UseDefaultCredentials = False
Smtp_Server.Credentials = New Net.NetworkCredential(senderAdd, senderPass)
Smtp_Server.EnableSsl = False
Smtp_Server.Host = SMTPserver

Try
   AddHandler Smtp_Server.SendCompleted, AddressOf sendComplete
Catch ex As Exception
   silentErrorLog(ex)
End Try
Harambe
  • 423
  • 3
  • 29
  • Smtp_Server.Send is a synchronous call, but sendComplete seems to be expecting the result of an Asynchronous call. That's probably not what is expected. – Slugsie May 18 '17 at 13:54
  • @Slugsie, hmmm, so how can I call it in this case? – Harambe May 18 '17 at 14:03
  • I don't think you should be calling it. It looks like an event handler. Somewhere you should probably have a line similar to this: `AddHandler Smtp_Server.SendCompleted, AddressOf sendComplete` When the `SendAsync` method completes, the event will be raised and the handler will run. Also `Smtp_Server` is a poor variable name for an instance of the `SmtpClient` class! – Chris Dunaway May 18 '17 at 22:08
  • @ChrisDunaway Yeah, like I said, not my code, I've just had to make changes too it - That's on the previous dev! – Harambe May 19 '17 at 08:07
  • @ChrisDunaway Hmmm, I added in the code you said (see edit) but it still isn't getting called? – Harambe May 19 '17 at 08:51
  • Did you also change the code to call `Smtp_Server.SendAsync`? That would have to happen _after_ the call to `AddHandler` – Chris Dunaway May 19 '17 at 14:25
  • @ChrisDunaway Ah, okay, noted. What is the second parameter required? I've replaced `Smtp_Server.Send(e_mail)` with `Smtp_Server.SendAsync(e_mail, "")` for now, but in the `sendComplete` sub it's got the line `If e.Error IsNot Nothing Then`, where e is the second parameter? – Harambe May 22 '17 at 09:40

1 Answers1

1

After running into a similar issue myself, @ChrisDunaway's comments have actually proved to be very helpful. My emails were failing but I wasn't sure why, so adding in an asynchronous call has helped me detect why.

In your code, you need to have the AddHandler, so as you've written in your example:

Try
    AddHandler Smtp_Server.SendCompleted, AddressOf sendComplete
Catch ex As Exception
    silentErrorLog(ex)
End Try

Then, the part that you're missing, you need to put something along the lines of

Dim userState As Object = e_mail
Smtp_Server.SendAsync(e_mail, userState)

This way, your sendComplete subroutine will pick up whether e_mail has had any issues or not and can complete the If e.Error IsNot Nothing part of the code.

For more information on this, see this site.

David
  • 2,298
  • 6
  • 22
  • 56
  • It's worked, it does call the `sendComplete` code, but the email always fails to send. Changing it back to just `Smtp_Server.Send(e_mail)` works, so not sure what is going wrong here? – Harambe May 23 '17 at 15:07
  • @Harambe There must be something rooted in a bit deeper, as far as I'm aware this has worked fine for me without too many other differences. – David May 23 '17 at 15:18