Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hey Guys,

I want to place a Contact form in my web app, so if something is wrong, they can contact the IT Helpdesk. I can't use <form></form> tags as I'm using a master page.

I found this on Code Project, but the problem I'm having is that it sends to the wrong address...

How do I get around that and fix it?

Thanks in advance

ASP.NET
<asp:Panel ID="Panel1" runat="server" DefaultButton="btnSubmit">
    <p>
        Please Fill the Following to Send Mail.</p>
    <p>
        Your name:
        <asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ErrorMessage="*"
            ControlToValidate="YourName" ValidationGroup="save" /><br />
        <asp:TextBox ID="YourName" runat="server" Width="250px" /><br />
        Your email address:
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
            ControlToValidate="YourEmail" ValidationGroup="save" /><br />
        <asp:TextBox ID="YourEmail" runat="server" Width="250px" />
        <asp:RegularExpressionValidator runat="server" ID="RegularExpressionValidator23"
            SetFocusOnError="true" Text="Example: username@gmail.com" ControlToValidate="YourEmail"
            ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" Display="Dynamic"
            ValidationGroup="save" /><br />
        Subject:
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*"
            ControlToValidate="YourSubject" ValidationGroup="save" /><br />
        <asp:TextBox ID="YourSubject" runat="server" Width="400px" /><br />
        Your Question:
        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="*"
            ControlToValidate="Comments" ValidationGroup="save" /><br />
        <asp:TextBox ID="Comments" runat="server" 
                TextMode="MultiLine" Rows="10" Width="400px" />
    </p>
    <p>
        <asp:Button ID="btnSubmit" runat="server" Text="Send" 
                    OnClick="Button1_Click" ValidationGroup="save" />
    </p>
</asp:Panel>
<p>
    <asp:Label ID="DisplayMessage" runat="server" Visible="false" />
</p>  


Behind Code C#:

C#
protected void Send()
    {
        // Gmail Address from where you send the mail
        var fromAddress = "gmail@gmail.com";
        // any address where the email will be sending
        var toAddress = YourEmail.Text.ToString();
        //Password of your gmail address
        const string fromPassword = "password";
        // Passing the values and make a email formate to display
        string subject = YourSubject.Text.ToString();
        string body = "From: " + YourName.Text + "\n";
        body += "Email: " + YourEmail.Text + "\n";
        body += "Subject: " + YourSubject.Text + "\n";
        body += "Question: \n" + Comments.Text + "\n";
        // smtp settings
        var smtp = new System.Net.Mail.SmtpClient();
        {
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
            smtp.Timeout = 20000;
        }
        // Passing values to smtp object
        smtp.Send(fromAddress, toAddress, subject, body);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            //here on button click what will done 
            Send();
            DisplayMessage.Text = "Your Comments after sending the mail";
            DisplayMessage.Visible = true;
            YourSubject.Text = "";
            YourEmail.Text = "";
            YourName.Text = "";
            Comments.Text = "";
        }
        catch (Exception) { }
    }


I'd say it's something simple, I just can't get my head around it. When I enter my email in the contact form, it sends too that email... which it shouldn't.

Thanks!
Posted
Comments
[no name] 18-Jul-12 9:53am    
"var toAddress = YourEmail.Text.ToString();"... "sends too that email... which it shouldn't"... maybe you could explain where you think that the email should be going instead of where you told it to go. And why are you calling ToString on a string?

1 solution

I guess you should swith a little in your Send() method

C#
// Gmail Address from where you send the mail
var fromAddress = "gmail@gmail.com";
// any address where the email will be sending
var toAddress = YourEmail.Text.ToString();


to

C#
// Gmail Address from where you send the mail
var fromAddress = YourEmail.Text.ToString();
// any address where the email will be sending
var toAddress = "gmail@gmail.com";
 
Share this answer
 
Comments
EuphorialXTC 18-Jul-12 9:46am    
No I tried that and thought it'd work, but you need to use the email and the password. It's got me really confused ha
StianSandberg 18-Jul-12 9:48am    
ok, then you have to set the gmail-addess as sender also:
var fromAddress = "gmail@gmail.com";
var toAddress = "gmail@gmail.com";
Security in the mailserver i guess. But this should work...
StianSandberg 18-Jul-12 9:50am    
I've never sent mail using gmail-mailserver, but they require som ports and a few other things. Take a look at this thrred at stackoverflow: http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail
EuphorialXTC 18-Jul-12 9:52am    
Hey! Thanks for the help, i knew it'd be something small that i'd miss! The "var fromAddress = "gmail@gmail.com";
var toAddress = "gmail@gmail.com";" Code works great! Thanks!
StianSandberg 18-Jul-12 9:53am    
great!.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900