i am developing a web application and would like to sent an email from it.hw do i do it.the following is my code.where did i go wrong as it does not work

code:--

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim emailMessage As System.Web.Mail.MailMessage
        Dim content As String
        Dim s As System.Web.Mail.SmtpMail

        content = "your user id is" + TextBox1.Text + " and your password is " + TextBox7.Text
        emailMessage = New System.Web.Mail.MailMessage
        emailMessage.From = TextBox8.Text
        emailMessage.To = TextBox7.Text
        emailMessage.Subject = "your user id and password"
        emailMessage.Body = content
        'SmtpMail.SmtpServer = "localhost"
        s.SmtpServer = "localhost"
        Try
            'SmtpMail.Send(emailMessage)
            s.Send(emailMessage)
            Response.Write("<script>alert('MESSAGE SENT');</script>")
            ' labelStatus.Text = "Message sent!"
            ' buttonSend.Enabled = False
        Catch ex As Exception
            'labelStatus.Text = "Unable to send the e-mail message"
            Response.Write("<script>alert('Unable to send the e-mail message');</script>")
        End Try
    End Sub

Recommended Answers

All 5 Replies

Did you enable SMTP in your local IIS?

Here's a function that I use to send myself emails incase there is an error. Take a look and see what's available for you to try:

Sub ErrMail(ByRef sbj As String, ByVal ex As String, Optional ByRef referrer As String = "unknown", Optional ByRef uname As String = "unknown", Optional ByRef ufn As String = "unknown", Optional ByRef uid As String = "unknown", Optional ByRef sPriority As String = "NORM")
        Dim MyMail As MailMessage = New MailMessage()
        Dim current As String = Request.ServerVariables("SCRIPT_NAME") & "?" & Request.ServerVariables("QUERYSTRING")

        MyMail.From = "site@site.com"
        MyMail.To = "email@email.com"
        MyMail.Subject = sbj
        MyMail.Body = sbj & vbCrLf & vbCrLf & "Reason:" & vbCrLf & ex & vbCrLf & vbCrLf & "Referring URL:  " & referrer & vbCrLf & vbCrLf & "Current:  " & current & vbCrLf & vbCrLf & "User: " & uname & "  [" & ufn & "  -  " & uid & "  |  DateTime: " & DateTime.Now.ToString() & "]"
        MyMail.BodyEncoding = Encoding.ASCII
        MyMail.BodyFormat = MailFormat.Text
        'You can also use HTML, but need to change the above line.

        Select Case UCase(sPriority)
            Case "HIGH" : MyMail.Priority = MailPriority.High
            Case "LOW" : MyMail.Priority = MailPriority.Low
            Case Else : MyMail.Priority = MailPriority.Normal
        End Select

        SmtpMail.SmtpServer = "mail.site.com" 'or SMTP server.

        Try
            SmtpMail.Send(MyMail)
        Catch
        End Try
    End Sub

A lot of the parameters are optional, so keep that in mind.

A way to use this function:

Explained Example:
ErrMail("Subject of Email", "a quick message to yourself, or an Exception.ToString() method.", "referrer is referring url: Request.ServerVariables('HTTP_REFERRER')", "uname is users name if you are tracking a user", "ufn is the users full name, if you are tracking it", "uid is the users userid, if you are tracking it", "sPriority is for the priority of the mail: low, normal, high")

Live Example:
ErrMail("Database Exception Occurred At [" & Request.ServerVariables("SERVER_NAME") & "]", (strException).ToString())

This will spit out the code as:
ErrMail("Database Exception Occurred At [mydomainname]", "Exception information...")

Try sending one to yourself. It's a great way to fix bugs, and you know where they occurred, who was using the files at the time, and where they came from. Possibilities are endless.. you can even track down which string caused the error, and the full SQL of that string if it is a command.

Anyway, it's a guideline.

i suppose the SMTP is enabled.would you please tel me how to check whether it is enabled or not so that i can be sure.
if thats not the problem then why didnt my code work.

Try This:

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim emailMessage As New System.Web.Mail.MailMessage()
        Dim content As String

        content = "your user id is" + TextBox1.Text + " and your password is " + TextBox7.Text
        emailMessage.From = TextBox8.Text
        emailMessage.To = TextBox7.Text
        emailMessage.Subject = "your user id and password"
        emailMessage.Body = content
        emailMessage.BodyFormat = MailFormat.Text
        System.Web.Mail.SmtpMail.SmtpServer = "localhost"
        Try
            System.Web.Mail.SmtpMail.Send(emailMessage)
            Response.Write("<script>alert('MESSAGE SENT');</script>")
        Catch ex As Exception
            Response.Write("<script>alert('Unable to send the e-mail message. ")
            Response.write(ex)
            Response.write("');</script>")
        End Try
    End Sub

i tried your suggestion and it gives the following exception


System.Web.HttpException: Could not access 'CDO.Message' object. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException (0x8004020F): The server rejected one or more recipient addresses. The server response was: 550 5.7.1

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.