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

时间:2014-10-26 17:08:58

标签: c# asp.net-mvc azure iis-express system.net.mail

在我的下面的代码中,我正在尝试创建一个包含字段名字,姓氏,电子邮件,评论的联系表单。按下提交按钮后,我需要它发送电子邮件到我的Gmail地址。我看过很多来自各地的指南,论坛和小贴士。我的大部分代码都基于http://ryanbutler.org/content/aspmvcform/documents/aspmvccontactform.pdf,并且已经根据我读过的其他文章在我认为有助于实现这一功能的领域进行了修改。

我的环境:

IDE:Visual Studio 2013 Express for Web
使用IIS 8 Express
部署将转到Azure
我的Os: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();

        }
    }
}

2 个答案:

答案 0 :(得分:1)

  

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

是的,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);
}

答案 1 :(得分:0)

如果这有助于其他人,这是基于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");