I have a web form that pulls data entered by the user, and then sends out an email to the user and an email group based on the category selected by the user. The groups used for the categories are stored within an XML doc. I'm able to parse the XML doc without a problem. I know this because I assigned the email variable to a label on the page, and I can see the result after the form is submitted.
But when I try to apply that same email address variable from that XML parse into the email job within the code file, I receive the following error:
The specified string is not in the form required for an e-mail address.
I've included my code below. If anyone could let me know what steps I'm missing, that would be great. Thanks.
test.aspx
Code:
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" CodeFile="test.aspx.vb" Inherits="feedback_test" AutoEventWireup="true" title="Feedback Form - TEST" %>
<asp:content id="Content1" contentplaceholderid="ContentPlaceHolder1" runat="server">
<% If Not Page.IsPostBack Then %>
<form name="formFeedback" action="" method="post" runat="server">
<asp:ValidationSummary ID="valSummary" runat="server"
HeaderText="You must fix the following form entries:"
Font-Names="verdana"
Font-Size="10pt"
Font-Bold="True"
/>
<br />
<table class="tableborder_black" width="98%" id="Feedback Form">
<tbody>
<tr class="tablecell_black">
<th scope="col" colspan="2">Feedback Form</th>
</tr>
<tr>
<td class="tablecell1" align="right" valign="top">Category</td>
<td class="tablecell3">
<asp:DropDownList id="selectCategory" runat="server">
<asp:ListItem Value="gen" Text="General" Selected="True" />
<asp:ListItem Value="cat1" Text="Category 1" />
<asp:ListItem Value="cat2" Text="Category 2" />
<asp:ListItem Value="cat3" Text="Category 3" />
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="tablecell1" align="right" valign="top">Email Address</td>
<td class="tablecell3"><asp:Textbox id="txtEmailAddress" columns="20" runat="server" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator5" runat="server" ControlToValidate="txtEmailAddress"
ErrorMessage="Email must be valid (Format: you@domain.com)." ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td class="tablecell1" align="right" valign="top" style="height: 50px">Comments</td>
<td class="tablecell3" style="height: 50px">
<asp:TextBox ID="txtComments" Columns="40" Rows="4" runat="server" Height="60px" TextMode="MultiLine"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtComments"
ErrorMessage="Must enter comments to complete this form." Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator8" runat="server" ControlToValidate="txtComments"
ErrorMessage="Must enter between 5 and 1000 characters." ValidationExpression="^[a-zA-Z-0-9]{5,1000}$" Display="Dynamic"></asp:RegularExpressionValidator>
</td>
</tr>
<tr class="tablecell_black">
<td scope="col" colspan="2" align="center">
<asp:HiddenField ID="hfDateTime" runat="server" />
<input id="btnReset" type="Reset" runat="server">
<asp:Button ID="btnSubmit" Text="Submit" OnClick="SubmitBtn_Click" runat="server" />
</td>
</tr>
</tbody>
</table>
</form>
<br /><br />
<strong>lblContactsTitle: </strong><asp:label id="lblContactsTitle" runat="server" /><br />
<strong>lblContactsEmail: </strong><asp:label id="lblContactsEmail" runat="server" /><br />
<strong>lblCategory: </strong><asp:label id="lblCategory" runat="server" /><br />
<strong>lblEmailAddress: </strong><asp:label id="lblEmailAddress" runat="server" /><br />
<strong>lblComments: </strong><asp:label id="lblComments" runat="server" /><br />
<% Else %>
<asp:label id="lblErrorMsg" runat="server" /><br />
<% End If %>
</asp:content>
test.aspx.vb
Code:
Partial Class feedback_test
Inherits System.Web.UI.Page
Dim strSiteDomain As String = "/mysite"
Sub SubmitBtn_Click(ByVal Sender As Object, ByVal E As EventArgs)
'Declare form variables
Dim category_form As String = selectCategory.SelectedItem.Value
Dim email_form As String = txtEmailAddress.Text
Dim comments_form As String = txtComments.Text
'Assign date variables
Dim dtCurrDate As DateTime = DateTime.Now 'Assign current date
Dim strDateTimeISO As String = dtCurrDate.ToString("s") 'ISO format
hfDateTime.Value = strDateTimeISO
'Assign labels
lblCategory.Text = category_form
lblEmailAddress.Text = email_form
lblComments.Text = comments_form
If Page.IsValid Then
Try
'Pull group members by selected category
Dim objEmailGroup As Object = False
Dim strXMLPath_contacts As String, strXSLPath_contacts As String
strXMLPath_contacts = strSiteDomain & "/help/docs/xml/contacts.xml" 'xml doc
strXSLPath_contacts = strSiteDomain & "/help/docs/xslt/contacts.xsl" 'xsl doc
'Load MainContent XML file internal_links.xml and assign page properties
Dim strContactsTitle As String = Nothing, strContactsEmail As String = Nothing
Dim contacts_xmld As XmlDocument
Dim contacts_nodelist As XmlNodeList
Dim xml_node As XmlNode
'Create the XML Document
contacts_xmld = New XmlDocument()
'Load the Xml file
contacts_xmld.Load(MapPath(strSiteDomain & "/help/docs/xml/contacts.xml"))
'Get the list of name nodes
contacts_nodelist = contacts_xmld.SelectNodes("/contacts/dept_div[@id = '" & category_form & "']")
'Response.Write(contacts_nodelist) 'TEST
'Loop through the nodes
For Each xml_node In contacts_nodelist
'Pull XML nodes
strContactsTitle = xml_node.Item("title").InnerText
strContactsEmail = xml_node.Item("email_contacts").InnerText
lblContactsTitle.Text = strContactsTitle
lblContactsEmail.Text = strContactsEmail
Next 'end loop
'Email job to group members
Dim mail_group As New System.Net.Mail.MailMessage(email_form, strContactsEmail, "Feedback Form", "The following feedback was submitted to your department:<br /><br/ ><strong>Category: </strong>" & category_form & "<br /><strong>Comments: </strong>" & comments_form & "")
Dim mail_user As New System.Net.Mail.MailMessage("webmaster@mysite.com", email_form, "Feedback Form", "Thank you for submitting feedback through our feedback form. If requested, someone from our staff will be in contact with you withing 2-3 business days.<br /><br />Webmaster")
Response.Write("objEmailGroup = True<br />")
'strContactsEmail
'email_form = user's email address
mail_group.BodyEncoding = System.Text.Encoding.UTF8
mail_group.IsBodyHtml = True
mail_user.BodyEncoding = System.Text.Encoding.UTF8
mail_user.IsBodyHtml = True
Dim smtpUser As String = "USERNAME"
Dim smtpPassword As String = "PASSWORD"
Dim smtpServer As String = "EMAILSERVERNAME"
Dim smtp As New Net.Mail.SmtpClient()
With smtp
If smtpUser.Length > 0 Then
.Host = smtpServer
.Port = 25
.Credentials = New System.Net.NetworkCredential(smtpUser, smtpPassword)
End If
End With
'Email job to group
smtp.Send(mail_group)
'Email job to user for confirmation (if email is not empty)
If email_form <> "" Then
smtp.Send(mail_user)
End If
Catch
lblErrorMsg.Text = "There was a problem while submitting this form. Please contact the <a href='mailto:webmaster@mysite.com'>Webmaster</a>."
End Try
End If
End Sub
End Class
KWilliams