Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I have a VB.net 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:

VB
Dim message As New MailMessage()
Dim smtpClient As New SmtpClient()
message.From = "Fred Flintstone@Rubble.com"
message.To.Add(toList)
message.CC.Add(ccList)
message.Subject = subject
message.IsBodyHtml = True
message.Body = body
smtpClient.Host = "ExternalSmtpServerAddress@JoeRockhead.com"
smtpClient.Send(message)


Thanks so much for any advice.
Paul
Posted

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
 
Share this answer
 
Comments
DinoRondelly 7-Dec-12 18:30pm    
You should store the list in either the SessionState or ViewState
Paul Bukowski 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.

Thanks,
Paul
DinoRondelly 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
myEmailList.Add(ret)

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
message.To.Add(email.emailTo)

and so

Next

The codes not perfect youll have to play with it but this should point you in the right direction
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 !

VB
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)
        Else
            '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
            dtEmailStore.Columns.Add(colRowNum)
            dtEmailStore.Columns.Add(colTo)
            dtEmailStore.Columns.Add(colCC)
            dtEmailStore.Columns.Add(colBody)
        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") = "PersonTo@Rubble.com"
        dr("colCC") = "PersonCC@Rubble.com"
        dr("colBody") = "Testing body"
        dtEmailStore.Rows.Add(dr) 'add the new row to the table

        dr = dtEmailStore.NewRow() 'new row
        dr("colTo") = "ttttt@Rubble.com"
        dr("colCC") = "ccccccc@Rubble.com"
        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 & "<br />")

        '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") & "<br>")
        Next

    End Sub

End Class
</br>
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900