Click here to Skip to main content
11,790,613 members (60,816 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: .NET2.0 VB.NET email


I have a web site where users fill out a form. As they populate the form (dropdowns, radio buttons), there are postbacks which occur to send individual emails (separate body & To & CC). I'd like to change this so that all those emails don't actually get sent until the user hits the SAVE button. Does anyone have suggestions on best practices to accomplish this? If I could somehow store all the messages in an array and loop through and send them after the user hits Save - that may work. Though I also don't want to bump into any state problems if 20+ minutes elapses prior to the user finishing the form.

I'm currently using the typical email send routine:

Dim message As New MailMessage()
Dim smtpClient As New SmtpClient()
message.From = "Fred"
message.Subject = subject
message.IsBodyHtml = True
message.Body = body
smtpClient.Host = ""

Thanks so much for any advice.
Posted 7-Dec-12 10:41am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You could create an email class with all properties (To,From,Body so on)

Then create a list of your email class with all the emails

Then loop through you list sending the emails
DinoRondelly at 7-Dec-12 18:30pm
You should store the list in either the SessionState or ViewState
Paul Bukowski at 8-Dec-12 9:08am
Dino - by chance do you happen to have an example to help me better understand what you mean around the list of email class ?

I think you might be on to something there with storing it in viewstate. Though I don't really follow how to put the list together to begin with.

DinoRondelly at 8-Dec-12 13:14pm
Depending how you set up your email class you would do something like this

Declare your email object
Dim ret as New myEmail()

Declare your list of email objects
Dim myEmailList as New List(of myEmail)

Add your values to your emial object

ret.emailTo = textboxEmailTo.Text
ret.emailFrom = textboxEmailFrom.Text

..and so on

Add your email object to your list of email objects

then just do a foreach loop on your list pulling the values back out sending the email

For each email in MyEmailList

Dim message As New MailMessage()
Dim smtpClient As New SmtpClient()
message.From = email.emailFrom

and so


The codes not perfect youll have to play with it but this should point you in the right direction
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

So that I could store it in viewstate, I ended up going with a datatable. Then I can easily add rows to it and then after the user saves, I can send all the emails at once. This works pretty well. Here is the partial code where I was stumbling.

Thank you Dino !

Imports System.Data.SqlClient
Imports System.Data
Partial Class test
    Inherits System.Web.UI.Page
    Dim dtEmailStore As DataTable
    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If ViewState("_dtEmailStore") IsNot Nothing Then
            dtEmailStore = CType(ViewState("_dtEmailStore"), DataTable)
            'The Datatable isn't in view state, so we need to set it up from scratch.
            Dim colRowNum As DataColumn = New DataColumn("colRowNum", System.Type.GetType("System.Int32"))
            colRowNum.AutoIncrement = True 'Auto increment column
            colRowNum.Unique = True
            dtEmailStore = New DataTable
            Dim colTo As DataColumn = New DataColumn("colTo", Type.GetType("System.String"))
            Dim colCC As DataColumn = New DataColumn("colCC", Type.GetType("System.String"))
            Dim colBody As DataColumn = New DataColumn("colBody", Type.GetType("System.String"))
            'add the columns to our new table
        End If
    End Sub
    Protected Sub btnInsert_Click(sender As Object, e As System.EventArgs)
        'insert these 2 rows into the datatable
        Dim dr As DataRow
        dr = dtEmailStore.NewRow() 'new row
        dr("colTo") = ""
        dr("colCC") = ""
        dr("colBody") = "Testing body"
        dtEmailStore.Rows.Add(dr) 'add the new row to the table

        dr = dtEmailStore.NewRow() 'new row
        dr("colTo") = ""
        dr("colCC") = ""
        dr("colBody") = "Testing again body"
        dtEmailStore.Rows.Add(dr) 'add the new row to the table

        'store the datatable in viewstate - so that it will persist across post backs.
        ViewState("_dtEmailStore") = dtEmailStore
        Call Read()
    End Sub
    Protected Sub btnRead_Click(sender As Object, e As System.EventArgs)
        Call Read()
    End Sub
    Sub Read()
        Response.Write(System.DateTime.Now & "
        'loop thru the datatable
        For Each dr As DataRow In dtEmailStore.Rows
            Response.Write(dr.Item("colRowNum") & "    " & dr.Item("colTo") & "    " & dr.Item("colCC") & "    " & dr.Item("colBody") & "
    End Sub
End Class

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 1,154
1 Maciej Los 825
2 KrunalRohit 686
3 ppolymorphe 660
4 CPallini 621

Advertise | Privacy | Mobile
Web04 | 2.8.1509028.1 | Last Updated 10 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100