[Last Call] Learn how to a build a cloud-first strategyRegister Now


Send an email from a form

Posted on 2007-10-18
Medium Priority
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
  • 3
  • 2
LVL 18

Expert Comment

ID: 20106861
This site has all the help for you.

Accepted Solution

nogovoia earned 2000 total points
ID: 20107109
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

    End Sub
End Class

Author Comment

ID: 20110813
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?
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 20110925
just add this snippet at the end of the existing routine

Author Comment

ID: 20113898
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.


Expert Comment

ID: 20117245
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month17 days, 16 hours left to enroll

831 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