Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET2.0 VB.NET email
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:
 
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 7-Dec-12 10:41am
Rate this: bad
good
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
  Permalink  
Comments
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.
 
Thanks,
Paul
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
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
Rate this: bad
good
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)
        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 & "
")
 
        '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") & "
")
        Next
 
    End Sub
 
End Class
</br>
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,656
1 Sergey Alexandrovich Kryukov 6,400
2 CPallini 5,240
3 George Jonsson 3,584
4 Gihan Liyanage 2,625


Advertise | Privacy | Mobile
Web04 | 2.8.140921.1 | Last Updated 10 Dec 2012
Copyright © CodeProject, 1999-2014
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