2012-04-01 129 views
2

我想使用MVC 3應用程序發送電子郵件。我在電子郵件地址數據庫中有一個名爲Reviewer的用戶表。我想要的是當我使用複選框從視圖中的表格中選擇2-3個用戶時,每個用戶的電子郵件地址應自動插入反饋視圖頁面上的「文本框」,但它不起作用。它取自控制器的值Webmail.Send()而不是反饋表單頁面。有任何想法嗎?使用MVC發送電子郵件3

[HttpPost] 
    public ActionResult Feedback(string email, string subject, string body) 
    { 
     try 
     { 
      WebMail.SmtpServer = "smtp.gmail.com"; 
      WebMail.EnableSsl = true; 
      WebMail.SmtpPort = 25; 
      WebMail.UserName = "[email protected]"; 
      WebMail.Password = "*******123"; 
      WebMail.From = "[email protected]"; 
      WebMail.Send(
        "[email protected]", 
        subject, 
        body, 
        email 
       ); 

      return RedirectToAction("FeedbackSent"); 
     } 
     catch (Exception ex) 
     { 
      ViewData.ModelState.AddModelError("_FORM", ex.ToString()); 
     } 

     return View(); 
    } 

這是反饋的查看頁面:

@using(Html.BeginForm()) { 
    <table> 
    <tr> 
     <td>Your e-mail:</td> 
     <td>@Html.TextBox("email")</td> 
    </tr> 
    <tr> 
     <td>Subject:</td> 
     <td>@Html.TextBox("subject")</td> 
    </tr> 
    <tr> 
     <td>Body:</td> 
     <td>@Html.TextArea("body")</td> 
    </tr> 
    </table> 
    <input type="submit" value="Send" /> 
} 
+0

當你說它從控制器中獲取價值時,你是什麼意思? 「from」電子郵件是「myemail @ gmail.com」,而不是表單中輸入的內容? – 2012-04-01 13:52:30

+0

我的意思是當我發送電子郵件時,它從「[email protected]」到「[email protected]」。但我想從「[email protected]」發送電子郵件給我從反饋頁面上的複選框列表中選擇的人員。 – Shaan 2012-04-01 14:06:57

+0

我找不到目標電子郵件在哪裏...您的控制器只有一個電子郵件作爲參數,其他電子郵件是經過編碼的。 – Romias 2012-04-01 18:20:54

回答

2

你應該使用這樣的:

WebMail.Send(
    email, 
    subject, 
    body 
); 

或者你也可以消除行:

WebMail.From = "[email protected]"; 

而且使用:

WebMail.Send(
    email, 
    subject, 
    body, 
    from: "[email protected]" 
); 

更多關於WebMail類,什麼參數WebMail.Send都可以在這裏找到:WebMail Class on MSDN