Send an email from a form

I have a web form so that users can submit their details in Visual Studio 2005. I have used the formView control in the visual studio toolbox. When the user presses the insert button ( provided as part of the insertItem template for the formview) I would like the details to be written to the database ( which I have done) and at the same time, an email to be generated to the user who has inputted their details already. How do I do the latter part without disrupting the former?

Who is Participating?
nogovoiaConnect With a Mentor Commented:
this should work


Imports System.Net
Imports System.Net.Mail

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim mailMessage As MailMessage
        Dim toAddress As MailAddress = New MailAddress("")
        Dim fromAddress As MailAddress = New MailAddress("")

        mailMessage = New MailMessage(fromAddress, toAddress)

        mailMessage.Subject = "new mail from"
        mailMessage.Body = "sample mail from"

        Dim smtpClient As New SmtpClient()
        smtpClient.Host = ""
        'set smtp port if differs from default
        smtpClient.Port = 25
        'set credentials if required from smtp server
        Dim networkCredentials As New NetworkCredential()
        networkCredentials.UserName = "username"
        networkCredentials.Password = "password"
        networkCredentials.Domain = ""
        smtpClient.Credentials = networkCredentials
        'send message

    End Sub
End Class
This site has all the help for you.
sebastizAuthor Commented:
Given that the  button already writes to the database (in the html bit), will this code also do the email at the same time on the same event, or do I have to define an alternative event?
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

just add this snippet at the end of the existing routine
sebastizAuthor Commented:
The trouble is, the routine is hard coded into the control, -the code for inserting into the database is an control whereas I will have to write this in in the code-behind.

Can I suppose you call your web control from within the button_click event handler? is so just add that snippet after the call to the control. As the user click the button the web control will add all you need in the database and return to the button click handler, than it'll send the email
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.