System.FormatException: The specified string is not in the form required for an e-mail address

username picture username · Apr 30, 2009 · Viewed 10k times · Source

net and xml im making a form tht send an email but it gives me an error

the email adresses come from the xml file

 The specified string is not in the form required for an e-mail address.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: The specified string is not in the form required for an e-mail address.

Source Error:

Line 82:             client.Host = "mail.host"
Line 83:             client.Port = 25
Line 84:             client.Send("from", txtTo.Text, txtSubject.Text, txtMessage.Text)
Line 85:             
Line 86:             lblMsg2.Text = "Email has been successfully sent"

Stack Trace:

[FormatException: The specified string is not in the form required for an e-mail address.]
   System.Net.Mime.MailBnfHelper.ReadMailAddress(String data, Int32& offset, String& displayName) +934335
   System.Net.Mail.MailAddress.ParseValue(String address) +245
   System.Net.Mail.MailAddress..ctor(String address, String displayName, Encoding displayNameEncoding) +87
   System.Net.Mail.Message..ctor(String from, String to) +127
   System.Net.Mail.MailMessage..ctor(String from, String to) +117
   System.Net.Mail.SmtpClient.Send(String from, String recipients, String subject, String body) +31
   ASP.adminpanel_admincontact_aspx.btnSend_Click(Object s, EventArgs e) in C:\Users\Farnu\Desktop\xmlproject\adminpanel\AdminContact.aspx:84
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1746

AdminContact.aspx

<%@ Page Language="VB" MasterPageFile="ContentMasterPage.master" Debug="true"%>  
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Net.Mail" %>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script runat="server">
    Sub Page_Load()
        If Not IsPostBack Then
            BindEmailList()
            pnlWriteEmail.Visible = False
        End If
        Dim Item As ListItem

        If chkSelectAll.Checked Then
            For Each Item In chklGuests.Items
                Item.Selected = True
            Next
        End If
    End Sub

    Sub BindEmailList()
        Dim dsGuestbookData As DataSet = New DataSet()
        Dim xmlDataDoc As XmlDataDocument
        Dim alGuest As ArrayList = New ArrayList()

        dsGuestbookData.ReadXml(Server.MapPath("../App_Data/Contact.xml"))
        xmlDataDoc = New XmlDataDocument(dsGuestbookData)

        Dim xnlGuests As XmlNodeList
        Dim xnGuest As XmlNode

        Dim xnlGuests1 As XmlNodeList
        Dim xnGuest1 As XmlNode

        xnlGuests = xmlDataDoc.GetElementsByTagName("contactmessage")
        xnlGuests1 = xmlDataDoc.GetElementsByTagName("contactmessage")

        For Each xnGuest In xnlGuests
            chklGuests.Items.Add(New ListItem(xnGuest.ChildNodes.Item(0).InnerXml))
            'lblGuests.Items.Add(New ListItem("<b>" & xnGuest.ChildNodes.Item(0).InnerXml & "</b><br/><b>" & "Msg:</b>" & xnGuest.ChildNodes.Item(1).InnerXml & "<br/><b>" & "Subject:</b>" & xnGuest.ChildNodes.Item(2).InnerXml & "<br/><br/>"))


            'lblGuests.Items.Add(New ListItem("<b>" & xnGuest.ChildNodes.Item(0).InnerXml & "</b><br/><b>" & "Msg:</b>" & xnGuest.ChildNodes.Item(1).InnerXml & "<br/><b>" & "Subject:</b>" & xnGuest.ChildNodes.Item(2).InnerXml & "<br/><br/>"))
        Next
    End Sub

    Sub btnNext_Click(ByVal s As Object, ByVal e As EventArgs)
        Dim item As ListItem
        Dim isSelected As Boolean = False

        For Each item In chklGuests.Items
            If item.Selected Then
                isSelected = True
                Exit For
            End If
        Next
        If isSelected Then
            lblMsg.Visible = False
            pnlSelectEmail.Visible = False
            pnlWriteEmail.Visible = True

            Dim strTo As String = ""

            For Each item In chklGuests.Items
                If item.Selected Then
                    strTo &= item.Value & ","
                End If
            Next

            strTo = Left(strTo, Len(strTo) - 1)
            txtTo.Text = strTo
        Else
            lblMsg.Visible = True
            lblMsg.Text = "Please select person(s)"
        End If
    End Sub

    Sub btnSend_Click(ByVal s As Object, ByVal e As EventArgs)
        If IsValid Then
            Dim client As New SmtpClient()
            client.Host = "mail.host"
            client.Port = 25
            client.Send(txtTo.Text, txtSubject.Text, txtMessage.Text, "Company Name")

            lblMsg2.Text = "Email has been successfully sent"
            pnlEmailForm.Visible = False
        End If
    End Sub

</script>

<html>
<head><title>Contact</title></head>
<div id="content">
    <div class="about">
<body>
    <p>Contact messages</p>
    <p>List of all the users that contacted us</p>
    <p>Select user to send email, and then click the 'Next' button.</p>
    <br />
    <%--<asp:HyperLink ID="hlAdmin" Text="Go back to admin home" NavigateUrl="admindefault.aspx" runat="server"/>
    <br /><br />--%>
    <asp:Label ID="lblMsg" runat="server" />

<%--    <form runat="server">--%>
        <asp:Panel ID="pnlSelectEmail" runat="server">
            <asp:CheckBox ID="chkSelectAll" AutoPostBack="true" Text="Select All" runat="server" />
            <div style="margin-left:-3px;">
                <asp:CheckBoxList ID="chklGuests" runat="server" />
                <asp:CheckBoxList ID="lblGuests" runat="server" />
            </div>
            <p>
                <asp:Button ID="btnNext" type="Submit" OnClick="btnNext_Click" Text="Next" runat="server" />
            </p>
        </asp:Panel>

        <asp:Panel ID="pnlWriteEmail" runat="server">
        <br />
        <asp:HyperLink ID="hlSelectEmail" Text="Go back to select email list" NavigateUrl="AdminContact.aspx" runat="server" />
        <br /><br /><br />

        <asp:Label ID="lblMsg2" runat="server" />

        <asp:Panel ID="pnlEmailForm" runat="server">
            <table cellpadding="3px" cellspacing="0px" border="0px">
                <tr>
                    <td>To</td>
                    <td><asp:TextBox ID="txtTo" Text="" Columns="50" runat="server" /></td>
                </tr>
                <tr>
                    <td>Subject</td>
                    <td><asp:TextBox ID="txtSubject" Columns="50" runat="server" /></td>
                </tr>
                <tr>
                    <td>Message</td>
                    <td><asp:TextBox ID="txtMessage" TextMode="MultiLine" Columns="50" Rows="7" runat="server" /></td>
                </tr>
                <tr>
                    <td>&nbsp;&nbsp;</td>
                    <td>
                        <p>
                            <asp:Button ID="btnSend" type="submit" OnClick="btnSend_Click" Text="Send" runat="server" />
                        </p>
                    </td>
                </tr>
            </table>
        </asp:Panel>

        </asp:Panel>

<%--    </form>
--%>    

</body>

</html>

   </div>
        <img src="../App_Themes/theme/html/images/phone.jpg" alt="" width="953" height="87" /><br />
    </div>
</asp:Content>

contact.xml

<?xml version="1.0" encoding="utf-8"?>
<contactmessages>
  <contactmessage>
    <useremail>[email protected]</useremail>
    <subject>hello</subject>
    <message>Good Job!</message>
    <date>4/13/2009 8:03:25 PM</date>
  </contactmessage> 
 <contactmessage>
    <useremail>[email protected]</useremail>
    <subject>hello</subject>
    <message>Good Job!</message>
    <date>4/13/2009 8:03:25 PM</date>
  </contactmessage>
<contactmessages>

Answer

Joshua Belden picture Joshua Belden · Apr 30, 2009

Your argument for the from address isn't a valid email address.

client.Send("from", 

From should be a valid email address.