3 Replies - 437 Views - Last Post: 20 May 2012 - 11:23 AM Rate Topic: -----

#1 trickstar34  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 05-June 09

C# e-mail form not working, need help.

Posted 19 May 2012 - 09:08 PM

Hi I'm currently trying to develop an e-mail form for my site and I'm having trouble getting it to work. Everything works (no runtime errors) except I can't get it to send because of my validation section of the code that makes sure that the e-mail is real. Even with a correct e-mail typed in, it's still saying "Please enter a valid e-mail address" in the error spot. I've been trying to find if the validation code doesn't work but its really hard to tell so I was wondering if any of you have any insights on my problem. :/

Code at top of document :
<%@ Import Namespace="System.Web.Mail" %>
<script language="c#" runat="server">

private void btnSend_Click(object sender, System.EventArgs e)
{                                                
    MailMessage msg = new MailMessage();
 
    msg.To = "email@website.com";
    msg.From = txtFrom.Text;
    msg.Subject = "Site contact page.";
    msg.Body = "Name: " + txtName.Text + "\n" + txtContent.Value;
    lblStatus.Text = "Sending...";
                                             
    SmtpMail.Send(msg);
    lblStatus.Text = "Your e-mail was sent!";                                         
}
</script>


Code for form embedded in document:
<form id="MailForm" method="post" runat="server">
        <asp:Label ID="Label0" runat="server">Name: <span class="required">*</span>
        </asp:Label>
        <asp:TextBox ID="txtName"
          runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator0" runat="server" ErrorMessage="Please enter your name." Width="200px"
            Height="23px" ControlToValidate="txtName"></asp:RequiredFieldValidator><br />
        <asp:Label ID="Label1" runat="server">E-mail: <span class="required">*</span>
        </asp:Label>
        <asp:TextBox ID="txtFrom"
          runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="FromValidator1" runat="server" ErrorMessage="Please enter your e-mail address." Width="200px"
            Height="23px" ControlToValidate="txtFrom"></asp:RequiredFieldValidator>
        <asp:RegularExpressionValidator ID="FromValidator2" runat="server" ErrorMessage="Please enter a valid e-mail address"
            ControlToValidate="txtFrom" ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)
            *"></asp:RegularExpressionValidator>
        <p>Message: <span class="required">*</span></p>
        <textarea runat="server" id="txtContent" rows="7" cols="30"></textarea><br />
        <asp:Button ID="btnSend" runat="server"
            Text="Send" onclick="btnSend_Click"></asp:Button>
        <asp:Label ID="lblStatus" runat="server"> 
        </asp:Label>
    </form>


Is This A Good Question/Topic? 0
  • +

Replies To: C# e-mail form not working, need help.

#2 tlhIn`toq  Icon User is offline

  • Closing in on 5,000
  • member icon

Reputation: 4929
  • View blog
  • Posts: 10,465
  • Joined: 02-June 10

Re: C# e-mail form not working, need help.

Posted 20 May 2012 - 07:22 AM

When I Google "RegEx Email" I get several articles showing various Regular Expression formulae for doing this. None of them resemble yours.

This page seems to do a nice explanation and provide an improved standard regex for this.

http://www.regular-e...info/email.html
Was This Post Helpful? 1
  • +
  • -

#3 trickstar34  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 05-June 09

Re: C# e-mail form not working, need help.

Posted 20 May 2012 - 10:10 AM

View PosttlhIn`toq, on 20 May 2012 - 08:22 AM, said:

When I Google "RegEx Email" I get several articles showing various Regular Expression formulae for doing this. None of them resemble yours.

This page seems to do a nice explanation and provide an improved standard regex for this.

http://www.regular-e...info/email.html

Thank you, this really helped, this is getting book marked.
Was This Post Helpful? 0
  • +
  • -

#4 trickstar34  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 05-June 09

Re: C# e-mail form not working, need help.

Posted 20 May 2012 - 11:23 AM

Well now that the validation is working, the message wont send. I'm new to using ASP.Net and I don't know if I have to put a value in the configuration (web.config) or what. The section of code is taken from
<%@ Import Namespace="System.Web.Mail" %>
<script language="c#" runat="server">

private void btnSend_Click(object sender, System.EventArgs e)
{                                                
    MailMessage msg = new MailMessage();
 
    msg.To = "trickstar34@yahoo.com";
    msg.From = txtFrom.Text;
    msg.Subject = "BudgetWebsiteDesigners contact page.";
    msg.Body = txtContent.Value;
    lblStatus.Text = "Sending...";
                                             
    SmtpMail.Send(msg);
    lblStatus.Text = "Your e-mail was sent!";                                         
}
</script>


This is the error that keeps getting output:

The "SendUsing" configuration value is invalid.

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.Runtime.InteropServices.COMException: The "SendUsing" configuration value is invalid.


Source Error:


Line 12:     lblStatus.Text = "Sending...";
Line 13:                                              
Line 14:     SmtpMail.Send(msg);
Line 15:     lblStatus.Text = "Your e-mail was sent!";                                         
Line 16: }

This post has been edited by trickstar34: 20 May 2012 - 11:24 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1