locked
Email Form sent to multiple email accounts RRS feed

  • Question

  • I would like to set multiple email accounts to receive form results.  Can this be done through the following code?

    Dim SendResultsTo As String = "email address@email.com"
                Dim smtpMailServer As String = "mail.email.com"
                Dim smtpUsername As String = "yourusername"
                Dim smtpPassword As String = "yourpassword"
                Dim MailSubject As String = "Form Submission Title"

    Thursday, September 15, 2016 3:32 PM

All replies

  • Well, not through that code. :) You are showing us setting string values. You haven't shown us how you are using those values, as suggestive as the names of the strings might be.

    There are multiple ways to send email using asp.net.  Show us all the code you are using to send the email, so we can see which one you are using, and are you asking how to send to multiple "to" addresses, or additional "copy-to" addresses?



    Kathleen Wilber
    BrightWillow - Asp.Net Applications

    Thursday, September 15, 2016 10:09 PM
  • Thanks for the reply.  I would like to have the email "copy-to" other email address.  Here is the complete code:

    <script runat="server">
            Protected Sub SubmitForm_Click(ByVal sender As Object, ByVal e As System.EventArgs)
                If Not Page.IsValid Then Exit Sub

                Dim SendResultsTo As String = "email address@email.com"
                Dim smtpMailServer As String = "mail.email.com"
                Dim smtpUsername As String = "yourusername"
                Dim smtpPassword As String = "yourpassword"
                Dim MailSubject As String = "Form Submission Title"

                Try
                    Dim txtQ As TextBox = Me.FormContent.FindControl("TextBoxQ")
                    If txtQ IsNot Nothing Then
                        Dim ans As String = ViewState("hf1")
                        If ans.ToLower <> txtQ.Text.ToLower Or ans.ToUpper <> txtQ.Text.ToUpper Then
                            Me.YourForm.ActiveViewIndex = 3
                            Exit Sub
                        End If
                    End If

                    Dim FromEmail As String = SendResultsTo
                    Dim msgBody As StringBuilder = New StringBuilder()
                    Dim sendCC As Boolean = False

                
                    For Each c As Control In Me.FormContent.Controls
                        Select Case c.GetType.ToString
                            Case "System.Web.UI.WebControls.TextBox"
                                Dim txt As TextBox = CType(c, TextBox)
                                If txt.ID.ToLower <> "textboxq" Then
                                    msgBody.Append(txt.ID & ": " & txt.Text & vbCrLf & vbCrLf)
                                End If
                                If txt.ID.ToLower = "email" Then
                                    FromEmail = txt.Text
                                End If
                                If txt.ID.ToLower = "subject" Then
                                    MailSubject = txt.Text
                                End If
                            Case "System.Web.UI.WebControls.CheckBox"
                                Dim chk As CheckBox = CType(c, CheckBox)
                                If chk.ID.ToLower = "checkboxcc" Then
                                    If chk.Checked Then sendCC = True
                                Else
                                    msgBody.Append(chk.ID & ": " & chk.Checked & vbCrLf & vbCrLf)
                                End If
                                
                            Case "System.Web.UI.WebControls.RadioButton"
                                Dim rad As RadioButton = CType(c, RadioButton)
                                msgBody.Append(rad.ID & ": " & rad.Checked & vbCrLf & vbCrLf)
                            Case "System.Web.UI.WebControls.DropDownList"
                                Dim ddl As DropDownList = CType(c, DropDownList)
                                msgBody.Append(ddl.ID & ": " & ddl.SelectedValue & vbCrLf & vbCrLf)
                        End Select
                    Next
                    msgBody.AppendLine()
                    
                    msgBody.Append("Browser: " & Request.UserAgent & vbCrLf & vbCrLf)
                    msgBody.Append("IP Address: " & Request.UserHostAddress & vbCrLf & vbCrLf)
                    msgBody.Append("Server Date & Time: " & DateTime.Now & vbCrLf & vbCrLf)

                    Dim myMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
                    myMessage.To.Add(SendResultsTo)
                    myMessage.From = New System.Net.Mail.MailAddress(FromEmail)
                    myMessage.Subject = MailSubject
                    myMessage.Body = msgBody.ToString
                    myMessage.IsBodyHtml = False
                    If sendCC Then myMessage.CC.Add(FromEmail)

                    
                    Dim basicAuthenticationInfo As New System.Net.NetworkCredential(smtpUsername, smtpPassword)
                    Dim MailObj As New System.Net.Mail.SmtpClient(smtpMailServer)
                    MailObj.Credentials = basicAuthenticationInfo
                    MailObj.Send(myMessage)

                    Me.YourForm.ActiveViewIndex = 1
                Catch
                    Me.YourForm.ActiveViewIndex = 2
                End Try
            End Sub

            Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
                If Not Page.IsPostBack Then
                    Dim lbl As Label = Me.FormContent.FindControl("labelq")
                    If lbl IsNot Nothing Then
                        Dim rq(3) As String
                        rq(0) = "Is fire hot or cold?"
                        rq(1) = "Is ice hot or cold?"
                        rq(2) = "Is water wet or dry?"

                        Dim ra(3) As String
                        ra(0) = "hot"
                        ra(1) = "cold"
                        ra(2) = "wet"
                        
                        Dim rnd As New Random
                        Dim rn As Integer = rnd.Next(0, 3)
                        lbl.Text = rq(rn)
                        ViewState("hf1") = ra(rn)
                    End If
                End If
            End Sub
        </script>

    Monday, September 19, 2016 11:12 AM
  • How are you going to capture those additional email addresses?  Do you know how many there will be?

    Once you know them, for each email address string, you'd:

    Dim SendCC As MailAddress = New MailAddress(email-addresss-string)

    MyMessage.CC.Add = SendCC



    Kathleen Wilber
    BrightWillow - Asp.Net Applications

    Monday, September 19, 2016 7:48 PM
  • I have 3 email addresses I would like to copy the form results to.  Where would it fit into the code if these were the email addresses: email address1@email.com, email address2@email.com, email address3@email.com.
    Tuesday, September 20, 2016 10:40 AM
  • You put it in the code somewhere after you have defined myMessage (Dim myMessage  ...) and before you send it (MailObj.Send(myMessage)).

    Dim SendCC1 As MailAddress = New MailAddress(address1@email.com)MyMessage.CC.Add = SendCC1

    Dim SendCC2 As MailAddress = New MailAddress(address2@email.com)MyMessage.CC.Add = SendCC2

    Dim SendCC3 As MailAddress = New MailAddress(address3@email.com)MyMessage.CC.Add = SendCC3



    Kathleen Wilber
    BrightWillow - Asp.Net Applications

    Tuesday, September 20, 2016 1:32 PM