簡體   English   中英

通過與System.Net.Mail Asp.Net MVC C#的聯系表發送電子郵件

[英]Sending an email via contact form with System.Net.Mail Asp.Net MVC C#

在下面的代碼中,我試圖創建一個包含名字,姓氏,電子郵件,注釋字段的聯系表單。 按下“提交”按鈕后,我需要它向我的Gmail地址發送電子郵件。 我看過許多指南,論壇和來自各地的技巧。 我的大部分代碼都基於http://ryanbutler.org/content/aspmvcform/documents/aspmvccontactform.pdf ,並已在我認為將有助於根據我已閱讀的其他文章實現此功能的領域進行了修改。

我的環境:

IDE:適用於Web的Visual Studio 2013 Express
使用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();

        }
    }
}

我的問題是:我的代碼有什么問題嗎?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM