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?

Seb
sebastizAsked:
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("to@domain.com")
        Dim fromAddress As MailAddress = New MailAddress("from@domain.com")

        mailMessage = New MailMessage(fromAddress, toAddress)

        mailMessage.Subject = "new mail from to@domain.com"
        mailMessage.Body = "sample mail from to@domain.com"

        Dim smtpClient As New SmtpClient()
        smtpClient.Host = "smtp.domain.com"
        '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 = "domain.com"
        smtpClient.Credentials = networkCredentials
        'send message
        smtpClient.Send(mailMessage)

    End Sub
End Class
0
 
SethiCommented:
This site has all the help for you.
http://www.systemnetmail.com
0
 
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?
0
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.

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

Seb
0
 
nogovoiaCommented:
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
0
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.