locked
Help with SMTP email RRS feed

  • Question

  • Hi,
    I am wondering if anyone can help me with this.  I am using VS2010.  The email string outlined in sub "BookTableLwb" goes out fine.  The email structured in "Email" is never sent.  I have tried all sorts of variations in smtp formatting and nothing works.  Even when I use the smtp defaults from Web.Config, the mail server ignores it.
    I am using an extenal smarthost as my mailserver - all the smtp settings are correct.
    The same code (in sub Email) works fine in another application, but there the user has to review incoming messages then select to "accept" button to trigger the out going email. In that case, I use the following string.  I've tried this, but it is still ignored.
    Dim client As New SmtpClient
    client.Send(New MailMessage(dr(FLD_EMAIL_FROM), dr(FLD_EMAIL_TO), "Booking Confirmation", strMessage))
    .Clear()
    I am trying to trigger the email when the "ExitButton" is pressed.
    gh


    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Net.Mail
    Imports Constants
    Partial Class Confirm
    Inherits System.Web.UI.Page
        Private Sub BookTableLwb()
            Try
                Dim strTitle As String = "Booking Confirmation"
                Dim strMessage As String = "Thank you ....... " _
                 & "Your ..........."
                Dim de As New DataEngineSql
                Dim dr As SqlDataReader
                With New ParamCollection
                    .Add(PAR_REST_ID, SqlDbType.Int, Session(GHWT_VAR_REST_ID))
                    dr = de.OpenDataReader(SP_GET_REST_MESSAGES, .Export)
                    While dr.Read
                        strTitle = dr(FLD_MSG_CONFIRM_TITLE)
                        strMessage = dr(FLD_MSG_CONFIRM_SUMMARY)
                    End While
                    dr.Close()
                    .Clear()
                    dr = de.OpenDataReader(SP_GET_NEXT_WEB_BOOKING_REF)
                    While dr.Read()
                        .Add(PAR_BOOKING_REF, SqlDbType.VarChar, 20, dr(FLD_BOOKING_REFERENCE))
                    End While
                    dr.Close()
                    .Add(PAR_CUSTOMER_ID, SqlDbType.VarChar, 20, Session(GHWT_VAR_USER_ID))
                    .Add(PAR_AVAILABILITY_ID, SqlDbType.BigInt, Session(GHWT_VAR_AVAILABILITY_ID))
                    .Add(PAR_HOST_DETAILS, SqlDbType.VarChar, 16, Session(GHWT_VAR_HOST_ID))
                    dr = de.OpenDataReader(SP_BOOK_WEB_TABLE, .Export)
                End With
                While dr.Read
                    strMessage = HTML_P_OPEN & HTML_B_OPEN & strTitle & HTML_B_CLOSE & HTML_P_CLOSE & HTML_BR & strMessage _
                     & HTML_BR & HTML_P_OPEN & "You will receive an email from the restaurant with written confirmation of your reservation." _
                     & HTML_P_CLOSE
                    strMessage = String.Format(strMessage, HTML_B_OPEN & dr(FLD_COVERS) & HTML_B_CLOSE, _
                     HTML_B_OPEN & Format(CDate(dr(FLD_TIME)), TIME_FORMAT_US) & HTML_B_CLOSE, _
                     HTML_B_OPEN & Format(CDate(dr(FLD_DATE)), DATE_FORMAT_US_LONG) & HTML_B_CLOSE, _
                     HTML_B_OPEN & dr(FLD_LOCATION) & HTML_B_CLOSE, _
                     HTML_B_OPEN & dr(FLD_REFERENCE) & HTML_B_CLOSE)
                End While
                dr.Close()
                With New SmtpClient
                    .Send(New MailMessage(ConfigurationManager.AppSettings(APP_EMAIL_FROM), GetSiteEmail, ConfigurationManager.AppSettings(APP_EMAIL_SUBJECT), ConfigurationManager.AppSettings(APP_EMAIL_BODY)))
                End With  (this email goes out fine)
                LblBookingResult.Text = strMessage
                LblBookingResult.Visible = True
                btnExit.Visible = True
                Session(GHWT_VAR_STATE) = "1"
            Catch ex As Exception
                With New ErrorEngine
                    .recordError(ex.Message, Me.Page, ErrorEngine.MessageType.[Error])
                End With
            End Try
        End Sub
        Private Sub Email()
            Dim de As New DataEngineSql
            Dim dr As SqlDataReader
            Dim blnResult As Boolean = False
            Dim strMessage As String = "Dear {0}," & vbCrLf & vbCrLf _
             & "We ...." & vbCrLf & vbCrLf _
             & "Your ......" & vbCrLf & vbCrLf _
             & "If ....." _
             & "please....."
            Try
                With New ParamCollection
                    .Add(PAR_REST_ID, SqlDbType.Int, Session(GHWT_VAR_REST_ID))
                    .Add(PAR_CUSTOMER_ID, SqlDbType.VarChar, 20, Session(GHWT_VAR_USER_ID))
                    .Add(PAR_VISIT_ID, SqlDbType.Int, Request(GHWT_REQ_VISIT_ID))
                    dr = de.OpenDataReader(SP_GET_WEB_EMAIL_DETAILS, .Export)
                    While dr.Read()
                        strMessage = String.Format(strMessage, dr(FLD_NAME), _
                         dr(FLD_COVERS), _
                         dr(FLD_LOCATION), _
                         Format(dr(FLD_DATE), DATE_FORMAT_US_LONG), _
                         Format(CDate(dr(FLD_TIME)), TIME_FORMAT_US), _
                         dr(FLD_REFERENCE), _
                         dr(FLD_PHONE))
                        Dim client As New System.Net.Mail.SmtpClient("mail.xxx.com", 25)
                        client.Credentials = New System.Net.NetworkCredential("info@xxx.com", "xxx")
                        Dim clientMessage As New MailMessage
                        client.Send(New MailMessage(GetSiteEmail, dr(FLD_EMAIL_TO), "Booking Confirmation", "strMessage"))
                        client.Send(clientMessage) (this email never goes out)
                        .Clear()
                    End While
                End With
            Catch Ex As Exception
                With New ErrorEngine
                    .recordError(Ex.Message, Me.Page, ErrorEngine.MessageType.Error)
                End With
            End Try
        End Sub
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            BookTableLwb()
        End Sub
    Protected Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
            Email()
            Dim strX As String = Session(GHWT_VAR_REST_ID)
            Session.RemoveAll()
            Response.Redirect(GHWT_PAG_INITIAL & "?" & GHWT_REQ_REST_ID & "=" & strX)
        End Sub
    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
            HttpContext.Current.Response.AddHeader(STR_P3P, STR_HTTP_PRIVACY_POLICY)
    End Sub
    Private Function GetSiteEmail() As String
    Dim de As New DataEngineSql
    Dim dr As SqlDataReader
    Dim email As String = String.Empty
    With New ParamCollection
    .Add(PAR_REST_ID, SqlDbType.Int, Session(GHWT_VAR_REST_ID))
    dr = de.OpenDataReader(SP_GET_REST_EMAIL, .Export)
    End With
    While dr.Read
    email = dr(FLD_EMAIL)
    End While
    dr.Close()
            Return email
        End Function
    End Class
    END
    • Moved by Liliane Teng Wednesday, August 3, 2011 9:30 AM (From:Visual Studio Class Designer)
    Monday, July 25, 2011 11:33 PM

Answers

All replies

  • Hello gh1591,

    Thanks for your post.

    Do you get any errors or exceptions? Please check this thread about how to send email using SMTP which works fine.

    http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/ba52a751-0da8-4a2c-9e5f-ee19ca3a436a (Sending Email)

    By the way, based on above code, the project is web application on your side. I recommend you could repost on ASP.NET forum for more better suggestions. 

    Have a nice day.

    Best regards


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, July 27, 2011 3:03 PM
  • This was moved out of the VSC forum.

    You can ask this in an ASP.NET forum (as suggested).

    Or I can move it into a specific MSDN forum if you'd like.

    This is regarding your code:

    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Net.Mail
    Imports Constants
    Partial Class Confirm
    Inherits System.Web.UI.Page
        Private Sub BookTableLwb()
            Try
                Dim strTitle As String = "Booking Confirmation"
                Dim strMessage As String = "Thank you ....... " _
                 & "Your ..........."
                Dim de As New DataEngineSql
                Dim dr As SqlDataReader
                With New ParamCollection
                    .Add(PAR_REST_ID, SqlDbType.Int, Session(GHWT_VAR_REST_ID))
                    dr = de.OpenDataReader(SP_GET_REST_MESSAGES, .Export)
                    While dr.Read
                        strTitle = dr(FLD_MSG_CONFIRM_TITLE)
                        strMessage = dr(FLD_MSG_CONFIRM_SUMMARY)
                    End While
                    dr.Close()
                    .Clear()
                    dr = de.OpenDataReader(SP_GET_NEXT_WEB_BOOKING_REF)
                    While dr.Read()
                        .Add(PAR_BOOKING_REF, SqlDbType.VarChar, 20, dr(FLD_BOOKING_REFERENCE))
                    End While
                    dr.Close()
                    .Add(PAR_CUSTOMER_ID, SqlDbType.VarChar, 20, Session(GHWT_VAR_USER_ID))
                    .Add(PAR_AVAILABILITY_ID, SqlDbType.BigInt, Session(GHWT_VAR_AVAILABILITY_ID))
                    .Add(PAR_HOST_DETAILS, SqlDbType.VarChar, 16, Session(GHWT_VAR_HOST_ID))
                    dr = de.OpenDataReader(SP_BOOK_WEB_TABLE, .Export)
                End With
                While dr.Read
                    strMessage = HTML_P_OPEN & HTML_B_OPEN & strTitle & HTML_B_CLOSE & HTML_P_CLOSE & HTML_BR & strMessage _
                     & HTML_BR & HTML_P_OPEN & "You will receive an email from the restaurant with written confirmation of your reservation." _
                     & HTML_P_CLOSE
                    strMessage = String.Format(strMessage, HTML_B_OPEN & dr(FLD_COVERS) & HTML_B_CLOSE, _
                     HTML_B_OPEN & Format(CDate(dr(FLD_TIME)), TIME_FORMAT_US) & HTML_B_CLOSE, _
                     HTML_B_OPEN & Format(CDate(dr(FLD_DATE)), DATE_FORMAT_US_LONG) & HTML_B_CLOSE, _
                     HTML_B_OPEN & dr(FLD_LOCATION) & HTML_B_CLOSE, _
                     HTML_B_OPEN & dr(FLD_REFERENCE) & HTML_B_CLOSE)
                End While
                dr.Close()
                With New SmtpClient
                    .Send(New MailMessage(ConfigurationManager.AppSettings(APP_EMAIL_FROM), GetSiteEmail, ConfigurationManager.AppSettings(APP_EMAIL_SUBJECT), ConfigurationManager.AppSettings(APP_EMAIL_BODY)))
                End With  (this email goes out fine)
                LblBookingResult.Text = strMessage
                LblBookingResult.Visible = True
                btnExit.Visible = True
                Session(GHWT_VAR_STATE) = "1"
            Catch ex As Exception
                With New ErrorEngine
                    .recordError(ex.Message, Me.Page, ErrorEngine.MessageType.[Error])
                End With
            End Try
        End Sub
        Private Sub Email()
            Dim de As New DataEngineSql
            Dim dr As SqlDataReader
            Dim blnResult As Boolean = False
            Dim strMessage As String = "Dear {0}," & vbCrLf & vbCrLf _
             & "We ...." & vbCrLf & vbCrLf _
             & "Your ......" & vbCrLf & vbCrLf _
             & "If ....." _
             & "please....."
            Try
                With New ParamCollection
                    .Add(PAR_REST_ID, SqlDbType.Int, Session(GHWT_VAR_REST_ID))
                    .Add(PAR_CUSTOMER_ID, SqlDbType.VarChar, 20, Session(GHWT_VAR_USER_ID))
                    .Add(PAR_VISIT_ID, SqlDbType.Int, Request(GHWT_REQ_VISIT_ID))
                    dr = de.OpenDataReader(SP_GET_WEB_EMAIL_DETAILS, .Export)
                    While dr.Read()
                        strMessage = String.Format(strMessage, dr(FLD_NAME), _
                         dr(FLD_COVERS), _
                         dr(FLD_LOCATION), _
                         Format(dr(FLD_DATE), DATE_FORMAT_US_LONG), _
                         Format(CDate(dr(FLD_TIME)), TIME_FORMAT_US), _
                         dr(FLD_REFERENCE), _
                         dr(FLD_PHONE))
                        Dim client As New System.Net.Mail.SmtpClient("mail.xxx.com", 25)
                        client.Credentials = New System.Net.NetworkCredential("info@xxx.com", "xxx")
                        Dim clientMessage As New MailMessage
                        client.Send(New MailMessage(GetSiteEmail, dr(FLD_EMAIL_TO), "Booking Confirmation", "strMessage"))
                        client.Send(clientMessage) (this email never goes out)
                        .Clear()
                    End While
                End With
            Catch Ex As Exception
                With New ErrorEngine
                    .recordError(Ex.Message, Me.Page, ErrorEngine.MessageType.Error)
                End With
            End Try
        End Sub
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            BookTableLwb()
        End Sub
    Protected Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
            Email()
            Dim strX As String = Session(GHWT_VAR_REST_ID)
            Session.RemoveAll()
            Response.Redirect(GHWT_PAG_INITIAL & "?" & GHWT_REQ_REST_ID & "=" & strX)
        End Sub
    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
            HttpContext.Current.Response.AddHeader(STR_P3P, STR_HTTP_PRIVACY_POLICY)
    End Sub
    Private Function GetSiteEmail() As String
    Dim de As New DataEngineSql
    Dim dr As SqlDataReader
    Dim email As String = String.Empty
    With New ParamCollection
    .Add(PAR_REST_ID, SqlDbType.Int, Session(GHWT_VAR_REST_ID))
    dr = de.OpenDataReader(SP_GET_REST_EMAIL, .Export)
    End With
    While dr.Read
    email = dr(FLD_EMAIL)
    End While
    dr.Close()
            Return email
        End Function
    End Class
    END
    

     


    Ed Price a.k.a User Ed, Microsoft Experience Program Manager (Blog, Twitter, Wiki)
    Thursday, December 29, 2011 5:43 AM
  • Ed,

     

    Can you move it for me?  Let me know where to find it.

    Thanks,

    Gary


    gary husbands
    Friday, December 30, 2011 7:03 PM