Send an email from a form

Posted on 2007-10-18
Last Modified: 2013-11-26
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?

Question by:sebastiz
    LVL 18

    Expert Comment

    This site has all the help for you.
    LVL 7

    Accepted Solution

    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

    Author Comment

    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?
    LVL 7

    Expert Comment

    just add this snippet at the end of the existing routine

    Author Comment

    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.

    LVL 7

    Expert Comment

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now