Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

ASP.net Emailing only works in debug mode

Hi
The following ASP.net VB.net code works in debug mode but does not work after I publish the site. Why might this be? Thanks

    Protected Sub btnContactMe_Click(sender As Object, e As EventArgs) Handles btnContactMe.Click

        Try
            Dim S As String = "Office2Web Enquiry "
            'NOTE OUNCTUATION MESSES WITH EMAILING
            S = S & "Name " & Me.txtName.Text & " Email " & Me.txtEmail.Text & " Contact Number " & Me.txtContactNumber.Text & " Company " & Me.txtCompany.Text
            S = S & " Description " & Me.TextBox1.Text
            Dim sEmailResult As String
            ''sEmailResult = oSendMailMessage("info@online-excel.com", "consulting@office2web.com.au", "murray@office2web.com.au", "", "Office2Web Enquiry", S, True)
            sEmailResult = oSendMailMessage("info@online-excel.com", "consulting@office2web.com.au", "murbro9@yahoo.com", "murray@office2web.com.au", "Office2Web Enquiry", S, True)

            If sEmailResult = "Email Success" Then
                Me.lblEmailResult.Text = "Message Sent. Thank you."
            Else
                Me.lblEmailResult.Text = "Sorry! Message not sent. " & sEmailResult
            End If
        Catch ex As Exception
            Me.lblEmailResult.Text = "Error message not sent " & ex.Message
        End Try

    End Sub

    Public Function oSendMailMessage(ByVal from As String, ByVal recepient As String, ByVal bcc As String,
                             ByVal cc As String, ByVal subject As String, ByVal body As String,
                             ByVal blnUseHTML As Boolean) As String
        Try

            ' Instantiate a new instance of MailMessage
            Dim mMailMessage As New MailMessage()

            ' Set the sender address of the mail message
            mMailMessage.From = New MailAddress(from)
            ' Set the recepient address of the mail message
            mMailMessage.To.Add(New MailAddress(recepient))

            ' Check if the bcc value is nothing or an empty string
            If Not bcc Is Nothing And bcc <> String.Empty Then
                ' Set the Bcc address of the mail message
                mMailMessage.Bcc.Add(New MailAddress(bcc))
            End If

            ' Check if the cc value is nothing or an empty value
            If Not cc Is Nothing And cc <> String.Empty Then
                ' Set the CC address of the mail message
                mMailMessage.CC.Add(New MailAddress(cc))
            End If

            ' Set the subject of the mail message
            mMailMessage.Subject = subject
            ' Set the body of the mail message
            mMailMessage.Body = body

            ' Set the format of the mail message body as HTML
            If blnUseHTML = True Then
                mMailMessage.IsBodyHtml = True
            Else
                mMailMessage.IsBodyHtml = False
            End If

            ' Set the priority of the mail message to normal
            mMailMessage.Priority = MailPriority.Normal

            ' Instantiate a new instance of SmtpClient
            Dim mSmtpClient As New SmtpClient()

            ' Send the mail message
            mSmtpClient.Send(mMailMessage)
            oSendMailMessage = "Email Success"

        Catch ex As Exception
            Dim oEX As String = ex.Message
            oSendMailMessage = "Email Not Sent! " & oEX
        End Try
    End Function
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

try write the Exception to a log file for debugging


you can do that using libraries such as Log4net and NLog.


You can get both libraries from NuGet Package Manager


References:


log4net

https://www.nuget.org/packages/log4net/


NLog

https://www.nuget.org/packages/NLog/


Ultimate Guide to Logging

https://www.loggly.com/ultimate-guide/net-logging-libraries/

Avatar of Murray Brown

ASKER

Thanks. I am trapping the error using a Try Catch and just getting "Failure Sending Mail"
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Are you running debug on a different server or as a different account?  Does your published site account/server have access to the SMTP server?
Hi Dustin. No to the first question and I don't know to the second
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The exact same SMTP settings work in a web app that I created in Visual Studio 2017. So something strange is going on