存储多封电子邮件,并在用户点击“保存”按钮时发送 [英] Store multiple Email messages and send when user hits Save button
问题描述
您好,
我有一个VB.net网站,用户填写表格。当他们填写表格(下拉菜单,单选按钮)时,会发送回发邮件以发送个人电子邮件(单独的正文和收件人)。我想改变这一点,以便所有这些电子邮件实际上都不会被发送直到用户点击SAVE按钮。有没有人有关于实现这一目标的最佳实践的建议?如果我可以以某种方式将所有消息存储在一个数组中并循环并在用户点击保存后发送它们 - 这可能有效。虽然如果在用户完成表格之前超过20分钟,我也不想遇到任何状态问题。
我正在使用典型的电子邮件发送例程:
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
推荐答案
您可以创建一个包含所有属性的电子邮件类(To,From,Body等)
然后创建一个电子邮件类列表所有的电子邮件
然后循环通过你的列表发送电子邮件
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
这样我就可以将它存储在viewstate中,我结束了和数据表一起去。然后我可以轻松地向其中添加行,然后在用户保存后,我可以立即发送所有电子邮件。这非常有效。这是我绊倒的部分代码。
谢谢Dino!
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 & "<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>
这篇关于存储多封电子邮件,并在用户点击“保存”按钮时发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!