简体   繁体   中英

Contact us form in asp.net c# not working

I'm trying to use gmail to send a simple contact us form but I keep getting "failure send mail" in the exception. I have been pulling my hair out the past 2 days trying to figure this out with no results. Here's my cs code:

        protected void Button1_Click(object sender, EventArgs e)
{

    try
    {
        requestSend.Visible = false;
        confirmation.Visible = true;
        MailMessage msg = new MailMessage();
        msg.From = new MailAddress(YourEmail.Text);
        msg.To.Add("MyGmail@gmail.com");
        msg.Subject = "Contact from: " + " " +YourName.Text;
        msg.Body = "Name: " + YourName.Text + "<br/>Email: " + YourEmail.Text + "<br/>Phone: " + YourPhone.Text + "<br/>Comments: " + Comments.Text;
        msg.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
        smtp.EnableSsl = true;
        smtp.Credentials = new System.Net.NetworkCredential("MyGmail@gmail.com", "MyPassword");
        smtp.Send(msg);
    }
    catch (Exception ex)
    {
        ResultLabel.Text = ex.Message;
    }



}

And here's my .aspx code:

<asp:Panel ID="Panel1" runat="server" DefaultButton="btnSubmit">
    <h3>To Request Information or get a Free Estimate:</h3>
    <p>
    <b>Fields marked with <span class="red">*</span> are mandatory</b>
    </p>

<p>
    <span class="red">*</span>Your name:
    <asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ErrorMessage="*"
        ControlToValidate="YourName" ValidationGroup="save" /><br />
    <asp:TextBox ID="YourName" runat="server" Width="250px" /><br /><br />
    <span class="red">*</span>Your email address:
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
        ControlToValidate="YourEmail" ValidationGroup="save" /><br />
    <asp:TextBox ID="YourEmail" runat="server" Width="250px" /><br />
    <span class="red"><asp:RegularExpressionValidator runat="server" ID="RegularExpressionValidator23" SetFocusOnError="true" Text="Please correct(Example: username@gmail.com)" ControlToValidate="YourEmail" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" Display="Dynamic" ValidationGroup="save" /></span><br />
    <span class="red">*</span>Your Phone:
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*"
        ControlToValidate="YourPhone" ValidationGroup="save" /><br />
    <asp:TextBox ID="YourPhone" runat="server" Width="400px" /><br />
    <span class="red"><asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" Display="Dynamic" ErrorMessage="Enter valid Phone number" ControlToValidate="YourPhone" ValidationExpression="^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$" ></asp:RegularExpressionValidator></span><br />
    <span class="red">*</span>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>
</div>
<div runat="server" id="confirmation" style="margin-left: auto; margin-right: auto; width: 65%;">
<h2>Your information has been sent</h2>
<asp:Label ID="ResultLabel" runat="server"></asp:Label>
</div>

What am I doing wrong (fyi, I am hosting with ixwebhosting.com if that matters, I'm not running my own server)? Any help would be greatly appreciated!

It might be due to the wrong port being used. Gmail is using port 465 for SSL. Try changing your port:

SmtpClient smtp = new SmtpClient("smtp.gmail.com", 465);

原来,宁愿尝试使用gmail 9(无论如何我都想使用我自己的域电子邮件),我只在端口25的服务器上使用“ localhost”,我很好!

You also have a very common error a lot of people have with "Contact Us" forms.

 msg.From = new MailAddress(YourEmail.Text);

This will break SPF and also cause DMARC to fail and you will never get the message from some people, if your mail server you use has DMARC enabled on it and GMAIL does.

Since DMARC is a more recent protocol, a lot of the old cookie cutter code for contact us forms - doesn't take this into account.

You can read more about that here: "DMARC - Contact Us Form Nightmare"

The suggested workaround will be to do:

msg.From = new MailAddress("MyGmail@gmail.com");
msg.Subject = "Contact from: " + " " +YourName.Text + " "  + YourEmail.Text;

This way - you avoid the issue outline in the article. You won't quickly be able to hit the "Reply" button, but at least you'll get the emails from those customers who have DMARC enabled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM