You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET C#网站用户反馈邮件应用开发报错求助

Fixes for Your ASP.NET Feedback Email App

Hey there! As someone who’s stumbled through similar email setup headaches when starting out with ASP.NET, let’s break down what’s going wrong here and get your feedback form working.

Key Issues in Your Code

Let’s tackle the problems one by one:

1. Reversed MailMessage Parameters

You’ve mixed up the order of the from and to addresses in the MailMessage constructor. The correct order is MailMessage(fromAddress, toAddress, subject, body) — your code has them flipped, which will cause delivery failures right off the bat.

2. Disabled SSL for Gmail SMTP

Gmail’s SMTP server on port 587 requires a secure SSL connection. You set client.EnableSsl = false, which is why you’re seeing that SmtpStatusCode stack trace error — the server is rejecting the unsecure connection.

3. Gmail Authentication Limits

Google no longer lets you use your regular account password for SMTP access unless you:

  • Enable 2-Step Verification on your Google account, then generate an App Password (this is the secure option), or
  • Enable "Less secure app access" (not recommended for security reasons).

Your current credential setup is likely failing because of this restriction.


Corrected Code

Here’s your code with all fixes applied, plus some quality-of-life improvements:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;

namespace my_site
{
    public partial class contact : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void send_Click(object sender, EventArgs e)
        {
            try
            {
                // Fixed: From address first, followed by To address
                MailMessage message = new MailMessage(txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text);
                message.IsBodyHtml = true;

                SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
                // Replace with your Google App Password (if 2FA is enabled)
                client.Credentials = new NetworkCredential("jameshollan79@gmail.com", "YOUR_GOOGLE_APP_PASSWORD");
                client.EnableSsl = true; // Fixed: Enabled required SSL for Gmail

                client.Send(message);
                status.Text = "Thanks for the feedback! We'll get back to you soon.";
            }
            catch (Exception ex)
            {
                // Show user-friendly error instead of raw stack trace (avoids exposing sensitive info)
                status.Text = $"Oops! Something went wrong: {ex.Message}";
                // Log the full stack trace to a file/database for your own debugging
                // System.IO.File.AppendAllText(@"C:\WebsiteLogs\EmailErrors.txt", $"{DateTime.Now}: {ex.StackTrace}\n");
            }
        }
    }
}

Extra Tips for Stability

  • Validate User Input: Add checks to ensure txtFrom.Text is a valid email address (use MailAddress.TryCreate or regex) before attempting to send — this prevents unnecessary errors from bad input.
  • Avoid Exposing Stack Traces: Showing raw stack traces to users is confusing and risky. Keep the public error simple, and log detailed errors privately for debugging.
  • Test with a Different SMTP Provider: If Gmail’s restrictions feel too cumbersome, consider using a service like SendGrid or Mailgun for easier email delivery in production.

内容的提问来源于stack exchange,提问作者Andy Hollan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:41:50