locked
System.Net.Mail RRS feed

  • Question

  • User1859773028 posted

    Hi, everyone!

    I've got a web form which a user fills out to request their private "access code". The form appears to work (it authenticates the user and displays the 'email sent' dialogue box), but no emails are actually going out. Below is the code being used to generate the email:

    Retmsg = "SendMsg"
    ' Retmsg = Retmsg.Replace("SAYT=", "SAYT=" & strSAYT)
    Dim linkad As String
    Dim msg As New System.Net.Mail.MailMessage
    Dim smtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient()
    smtpClient.Host = "smtp4.email.edu"
    smtpClient.Port = 25
    smtpClient.Timeout = 10
    msg.To.Add(EmailID)
    msg.IsBodyHtml = True
    msg.From = New System.Net.Mail.MailAddress("test@email.com")
    msg.Body = "Please click the link to gain access to your personal Access Code and ESIGN#. If the full link is not underlined, copy and paste the link into your browser."
    linkad = " https://test.website.com/resetaccess.aspx?SAYT="
    linkad = linkad.Replace("SAYT=", "SAYT=" & strSAYT)
    msg.Body = msg.Body & "<BR><BR> " & linkad
    msg.Subject = "My Aid - Access Code/ESIGN Recovery"
    Try
    smtpClient.Send(msg)

    This has been working for months, so I'm not sure what's gone awry. Any guidance is appreciated!

    Tuesday, July 28, 2015 11:22 AM

Answers

  • User753101303 posted

    So if an exception happens in smptClient.Send it will be just hidden from everybody and your app will keep running.

    Similarly the other catch block turns an exception into a return value which is considered bad because:
    - it is easy to just ignore the return value and once again your app could keep running when exceptions are thrown without anybody knowing about that
    - even if you test the return value, now you don't have idea about what happened (if using a catch block in the caller you can still react differenly depending on the exception type or log exception détails)

    So if you can't reproduce the same error on your dev box you'll have to change your code to stop just ignoring any error that could happen.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 28, 2015 1:43 PM

All replies

  • User753101303 posted

    Hi,

    What do you do in your catch clause? Could it be that if an exception happens it will be just ignored?

    Else it could be that it does work but that the mail is blocked later (wrong destination address, blocked as a spam which can happen especially for short messages or placed in the Junk folder by the mail client).

    Tuesday, July 28, 2015 11:42 AM
  • User1859773028 posted

    What do you do in your catch clause? Could it be that if an exception happens it will be just ignored?

    Here is the full section of code:

    Try
                Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings("MyaidConn").ToString)
    
          
                    Dim cmdCheck As New SqlCommand(strSQL, cn)
                    cn.Open()
                    Dim rdrCheck As SqlDataReader = cmdCheck.ExecuteReader(Data.CommandBehavior.Default)
                    If rdrCheck.HasRows Then
                        rdrCheck.Read()
                        strSAYT = rdrCheck(0).ToString
                        strDOB = Left(rdrCheck(2).ToString, 10)
                        strZIP = rdrCheck(1).ToString
                        strQ = rdrCheck(3).ToString
                        strA = rdrCheck(4).ToString
                        If strDOB <> bdate Then
                            Retmsg = "2"
                       
                        End If
                        If strZIP <> zip Then
                            Retmsg = "3"
                        End If
                        If strQ <> "" Or strQ.Length <> 0 Then
                            If strQ <> quest Then
                                Retmsg = "4"
                            Else
                                If strA <> ans Then
                                    Retmsg = "5"
                                Else
                                     
                                    Retmsg = "SendMsg"
                                    ' Retmsg = Retmsg.Replace("SAYT=", "SAYT=" & strSAYT)
                                    Dim linkad As String
                                    Dim msg As New System.Net.Mail.MailMessage
                                    Dim smtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient()
                                    smtpClient.Host = "smtp4.example.edu"
                                    smtpClient.Port = 25
                                    smtpClient.Timeout = 10
                                    msg.To.Add(EmailID)
                                    msg.IsBodyHtml = True
                                    msg.From = New System.Net.Mail.MailAddress("example@example.edu")
                                    msg.Body = "Please click the link to gain access to your personal Access Code and ESIGN#. If the full link is not underlined, copy and paste the link into your browser."
                                    linkad = " https://test.example.edu/resetaccess.aspx?SAYT="
                                    linkad = linkad.Replace("SAYT=", "SAYT=" & strSAYT)
                                    msg.Body = msg.Body & "<BR><BR>  " & linkad
                                    msg.Subject = "My Aid - Access Code/ESIGN  Recovery"
                                    Try
                                        smtpClient.Send(msg)
                        
                                    Catch ex As Exception
                        
                                    End Try
                                End If
                            End If
                        End If
                    Else
                        Retmsg = "1"
                    End If
                    cn.Close()
                End Using
            Catch ex As Exception
                Retmsg = "Exception"
            End Try
     
            
            Return Retmsg

    I have rebooted the server hoping there might be a service issue of some type, but that doesn't seem to have helped.

    Tuesday, July 28, 2015 12:05 PM
  • User269602965 posted

    smtp4.example.edu

    smtp server usually require authentication (password) or sometimes other ports to relay your send mail.

    Tuesday, July 28, 2015 1:33 PM
  • User753101303 posted

    So if an exception happens in smptClient.Send it will be just hidden from everybody and your app will keep running.

    Similarly the other catch block turns an exception into a return value which is considered bad because:
    - it is easy to just ignore the return value and once again your app could keep running when exceptions are thrown without anybody knowing about that
    - even if you test the return value, now you don't have idea about what happened (if using a catch block in the caller you can still react differenly depending on the exception type or log exception détails)

    So if you can't reproduce the same error on your dev box you'll have to change your code to stop just ignoring any error that could happen.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 28, 2015 1:43 PM
  • User1859773028 posted

    I rebooted it for a second time and it's back working like a charm. Our mail system was down for an extended time Friday and through the weekend, so I imagine there were issues related to that. Thanks for y'alls help!

    Tuesday, July 28, 2015 1:54 PM
  • User753101303 posted

    Great it is solved, but I would still strongly suggest to change your code. So you had an exception when calling Send because the mail server couldn't be reached or used but your code just ignore that exception and pretend that everything is ok...

    Tuesday, July 28, 2015 2:04 PM
  • User1859773028 posted

    ...an exception when calling Send because the mail server couldn't be reached or used but your code just ignore that exception and pretend that everything is ok...

    Isn't that what this does?

    Try
      smtpClient.Send(msg)
                        
    Catch ex As Exception
    End Try

    Tuesday, July 28, 2015 2:38 PM