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

ASP.NET联系页面邮件调整:将用户咨询发送至指定邮箱

Hey there! I see exactly what's going on here—your code is currently sending the enquiry email to the user's submitted address instead of yours, and that's an easy fix. Let's walk through it step by step.

The Root Issue

In your [HttpPost] Index method, this line is directing the email to the user's email instead of your target inbox:

mail.To.Add(EmailId);

You're passing the user's submitted email (from the form) as the recipient. We just need to swap that out for your own email address.

Corrected Controller Code

Here's the updated code with key fixes and a helpful optimization:

namespace SITEEXEMPLO.Controllers 
{ 
    public class HomeController : Controller 
    { 
        // GET: /Home/ 
        [HttpGet] 
        public ActionResult Index() 
        { 
            return View(); 
        } 

        [HttpPost] 
        public ActionResult Index(string Name, string EmailId, string PhoneNo, string Subject, string Message) 
        { 
            try 
            { 
                MailMessage mail = new MailMessage(); 
                // Fix: Send to YOUR target email address instead of the user's
                mail.To.Add("xoinas23@gmail.com"); 
                mail.From = new MailAddress("xoinas23@gmail.com"); 
                mail.Subject = Subject; 

                // Optimization: Add the user's email as a reply-to address
                // This lets you easily respond to their enquiry directly from your inbox
                mail.ReplyToList.Add(new MailAddress(EmailId));

                string userMessage = ""; 
                userMessage += "<br/>Name :" + Name; 
                userMessage += "<br/>Email Id: " + EmailId; 
                userMessage += "<br/>Phone No: " + PhoneNo; 
                userMessage += "<br/>Message: " + Message; 
                string Body = "Hi, <br/><br/> A new enquiry by user. Detail is as follows:<br/><br/> " + userMessage + "<br/><br/>Thanks"; 
                mail.Body = Body; 
                mail.IsBodyHtml = true; 

                SmtpClient smtp = new SmtpClient(); 
                smtp.Host = "smtp.gmail.com"; 
                smtp.Port = 587; 
                // Important: For modern Gmail accounts, use an App Password instead of your regular password
                // You'll need 2-Step Verification enabled on your Google account to create one
                smtp.Credentials = new System.Net.NetworkCredential("xoinas23@gmail.com", "your-app-password-here"); 
                smtp.EnableSsl = true; 
                smtp.Send(mail); 

                ViewBag.Message = "Thank you for contacting us."; 
            } 
            catch 
            { 
                ViewBag.Message = "Error............"; 
            } 
            return View(); 
        } 
    }
}

Important Notes

  • Gmail Security: Google blocks regular password usage for SMTP access in most cases now. To make this work, enable 2-Step Verification on your Google account, then create an App Password specifically for this application—use that password in the smtp.Credentials line instead of your normal login password.
  • Attachment Handling: Your HTML form includes an attachment field, but your current controller doesn't process uploaded files. If you want to support attachments, let me know and I can add that code too!

内容的提问来源于stack exchange,提问作者Antonio Choinas

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 10:04:17