使用System.Net.Mail Asp.Net MVC C#通过联系表单发送电子邮件

本文关键字:Net 表单 联系 电子邮件 Mail System Asp MVC 使用 | 更新日期: 2023-09-27 18:28:03

在下面的代码中,我试图创建一个联系人表单,该表单包含名字、姓氏、电子邮件和注释字段。当按下提交按钮时,我需要它向我的Gmail地址发送电子邮件。我看了很多来自各地的指南、论坛和提示。我的大部分代码都基于http://ryanbutler.org/content/aspmvcform/documents/aspmvccontactform.pdf在我读过的其他文章的基础上,在我认为有助于实现这一功能的领域进行了修改。

我的环境:

IDE:Visual Studio 2013 Web学习版
使用IIS 8 Express
部署将转到Azure
我的操作系统:Windows 8.1

点击提交并填写表单后的错误消息:

错误。处理您的请求时出错。

我的问题是:我的代码有什么问题吗?或者问题可能不在于代码,而在于服务器IIS Express或其他区域?我问这个问题是因为我在某个地方读到IIS Express不支持SMTP。

控制器为:

using MySite.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;
namespace MySite.Controllers
{
    public class SendMailerController : Controller
    {
        //
        // GET: /SendMailer/ 
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Contact(ContactModels c)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    MailMessage msg = new MailMessage();
                    SmtpClient smtp = new SmtpClient();
                    msg.To.Add("MyGmailAddress@gmail.com");
                    msg.Subject = "Contact Us";
                    msg.Body = "First Name: " + c.FirstName;
                    msg.Body += "Last Name: " + c.LastName;
                    msg.Body += "Email: " + c.Email;
                    msg.Body += "Comments: " + c.Comment;
                    msg.IsBodyHtml = false;
                    smtp.Host = "smtp.gmail.com";
                    smtp.Port = 25;   
                    smtp.EnableSsl = true;
                    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                    smtp.UseDefaultCredentials = false; // 
                    smtp.Credentials = new   NetworkCredential("MyGmailAddress@gmail.com", "MyGmailPassword");  
                    smtp.Host = "smtp.gmail.com";
                    smtp.Send(msg);
                    msg.Dispose();
                    return View("Success");
                }
                catch (Exception)
                {
                    return View("Error");
                }    
            }
            return View();
        }
    }
}

使用System.Net.Mail Asp.Net MVC C#通过联系表单发送电子邮件

我的问题是:我的代码有什么问题吗?

是的,smtp.gmail.com需要安全连接,在端口25上不可用。试试这个:

using (var client = new SmtpClient("smtp.gmail.com", 587))
{
    client.EnableSsl = true;
    client.UseDefaultCredentials = false;
    client.Credentials = new NetworkCredential("MyGmailAddress", "Your Gmail Password");
    string body = string.Format(
        "First Name: {0}, Last Name: {1}, Email: {2}, Comment: {3}",
        c.FirstName,
        c.LastName,
        c.Email,
        c.Comment
    );
    var message = new MailMessage(
        "sender@gmail.com", 
        "MyGmailAddress@gmail.com", 
        "Contact Us", 
        "mail body"
    );
    message.IsBodyHtml = false;
    client.Send(message);
}

如果这有助于其他人,这是基于Darin Dimitrov的有用(快速)响应的略微修改的代码:

        [HttpPost]
        public ActionResult Contact(ContactModels c)
        {
            string resultMsg = "There was an error submitting your message. Please try again later.";
            if (!ModelState.IsValid)
            {
                return Content(resultMsg);
            }
            if (ModelState.IsValid)
            {
            using (var client = new SmtpClient("smtp.gmail.com", 587))
            {
                client.EnableSsl = true;
                client.UseDefaultCredentials = false;
                client.Credentials = new NetworkCredential("SendersEmail@gmail.com", "ThePassword");

                string body = string.Format(
                    "First Name: {0}'nLast Name: {1}'nEmail: {2}'nComment: {3}",
                    c.FirstName,
                    c.LastName,
                    c.Email,
                    c.Comment
                ); ''In this block I added a new line 'n to appear better when I receive the email.
                var message = new MailMessage();
                message.To.Add("RecipientEmail@gmail.com");
                message.From = new MailAddress(c.Email, c.Name);
                message.Subject = String.Format("Contact Request From: {0} ", c.Name);
                message.Body = body;
                message.IsBodyHtml = false;
                try
                {
                    client.Send(message);
                }
                catch (Exception)
                {
                    return View("Error");
                }
            }                
        }
        return View("Success");