2015-07-20 24 views
0

我應該提到這只是一個學習項目,並且永遠不會在線託管。我在本地運行應用程序。用C#ASP.NET發送電子郵件,並使用從URL傳入的參數

我有兩個問題發送電子郵件在傳遞的參數:

  1. 的主要問題:它不發送。
  2. 只有在點擊發送並重定向到同一頁面之後,參數纔會在視圖中填充表單,但它們顯示在URL中。

這裏是我的代碼:

Mail.cs(型號)

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

namespace LotManager.Models 
{ 
    public class Mail 
    { 
     public string From = "[email protected]"; 
     public string To { get; set; } 
     public string Subject = "Parking Alert"; 
     public string Body { get; set; } 
    } 
} 

MailController.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net.Mail; 
using System.Web; 
using System.Web.Mvc; 
using LotManager.Controllers; 

namespace LotManager.Controllers 
{ 
    public class MailController : Controller 
    { 
     // 
     // GET: /SendMailer/ 
     public ActionResult Index() 
     { 
      return View(); 
     } 
     [HttpPost] 
     public ViewResult Index(LotManager.Models.Mail _objModelMail) 
     { 
      var to = Request.QueryString["To"]; 
      ViewBag.To = to; 
      var body = Request.QueryString["Body"]; 
      ViewBag.Body = body; 
      if (ModelState.IsValid) 
      { 
       MailMessage mail = new MailMessage(); 
       mail.To.Add(to);     
       mail.From = new MailAddress(_objModelMail.From); 
       string Body = body; 
       mail.Body = Body; 
       mail.IsBodyHtml = true; 
       SmtpClient smtp = new SmtpClient(); 
       smtp.Host = "smtp.gmail.com"; 
       smtp.Port = 25; 
       smtp.UseDefaultCredentials = false; 
       smtp.Credentials = new System.Net.NetworkCredential 
       ("mygmailusername", "mypassword"); //My actual account info goes here 
       smtp.EnableSsl = true; 
       try 
       { 
        smtp.Send(mail); 
       } 
       catch (Exception) 
       { 
        Console.WriteLine("The email was not sent, because I couldn't get it to work. Oops!"); 
       } 
       return View("Index", _objModelMail); 
      } 
      else 
      { 
       return View(); 
      } 
     } 
    } 
} 

Index.cshtml(發送郵件查看)

@model LotManager.Models.Mail 

@{ 
    ViewBag.Title = "Send"; 
} 

<h2>Send</h2> 

@using (Html.BeginForm()) 
{ 
    @Html.ValidationSummary() 
    <p>To: </p> 
    <p>@Html.TextBoxFor(m => m.To, new { @Value = @ViewBag.To })</p> 
    <p>Body: </p> 
    <p>@Html.TextBoxFor(m => m.Body, new { @Value = @ViewBag.Body })</p> 
    <input type="submit" value="Send" /> 
} 

該參數傳遞到URL中的代碼:

@Html.Actionlink("Send Notification", "Index", "Mail", new { To = item.Employee.Email, Body = item.Description }, null) 
+0

是否有例外,還是電子郵件沒有到達? –

+0

電子郵件只是沒有到達。我忘了提及我在本地舉辦這個活動,因爲它只是一個學習項目。 –

回答

2

惡意用戶可以使用網頁來使用您的電子郵件帳戶的垃圾郵件的人。這會很快破壞你的Gmail發件人信譽。從糟糕的發件人聲譽中恢復幾乎是不可能的。

1期

您正在使用的wrong SMTP port

smtp.gmail.com要求端口465用於SSL或用於TLS的端口587。

問題2

要調用使用鏈路(ActionLink的),它創建一個GET請求的控制器。然而,由於[HttpPost]屬性,您的控制器操作只會針對POST進行調用。要麼刪除[HttpPost],要麼使用後置操作而不是鏈接來調用控制器操作。

+0

我試了端口25,465和587沒有運氣。我忘了提及我只在本地運行,因爲它只是一個學習項目。謝謝。 –

+0

您是否修復了問題2?你確定發送的代碼被調用嗎? '我在本地運行這個功能'有些ISP會阻止端口25到SMTP服務器以外的其他SMTP服務器來控制垃圾郵件,儘管我從來沒有聽說過ISP阻止465或587.檢查您是否有防止連接的出站防火牆規則。 –

+0

嘗試下載Mozilla Thunderbird等電子郵件客戶端。以與您的代碼相同的方式配置它(相同的SMTP,用戶,密碼,端口,協議)。看看你是否可以使用客戶端發送電子郵件。如果發送失敗,您應該得到一個合理詳細的錯誤信息。 –

0

該參數也不可偏好,因爲您正在使用[HttpPost]。 GET中的參數不在POST中。

關於第二個問題請看Google文檔:https://support.google.com/a/answer/176600?hl=en 如果你想使用端口25,你需要將服務器改爲smtp-relay.gmail.com。否則,請將端口更改爲465(SSL)或端口587(TLS)。

+0

我試過端口25與smtp中繼和aspmx.l.google.com,465和587與smtp.gmail.com沒有運氣。我忘了提及我在本地運行,這可能是問題所在。 –