2011-06-08 123 views
14

我需要使用文本區域和圖像上傳字段來製作表單。當有人提交時,我希望它發送電子郵件(從textarea文本)與附件(從輸入文件上傳字段)給我帶有附件上傳和電子郵件發送的表單

我簡單的形式看起來像這樣:

@using (Html.BeginForm()) 
{ 
    @Html.ValidationSummary(true) 
    <fieldset>   
      @Html.TextArea("Question");  
      <input type="file"/> 
      <input type="submit" value="Send" /> 

    </fieldset> 

} 

我發現,正在做這樣的事情的PHP腳本,但我怎麼能做到這一點在ASP.NET MVC(可能是使用JavaScript)?在.NET

回答

23

下面是一個使用Gmail的SMTP例子,但如果你有自己的SMTP服務器,你可以很容易地調整代碼。

一如既往我將與視圖模型開始:

public class QuestionViewModel 
{ 
    [Required] 
    public string Question { get; set; } 

    public HttpPostedFileBase Attachment { get; set; } 
} 

然後控制器:

public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     return View(new QuestionViewModel()); 
    } 

    [HttpPost] 
    public ActionResult Index(QuestionViewModel model) 
    { 
     if (!ModelState.IsValid) 
     { 
      return View(model); 
     } 

     using (var client = new SmtpClient("smtp.gmail.com", 587)) 
     { 
      client.EnableSsl = true; 
      client.Credentials = new NetworkCredential("[email protected]", "secret"); 
      var mail = new MailMessage(); 
      mail.From = new MailAddress("[email protected]"); 
      mail.To.Add("[email protected]"); 
      mail.Subject = "Test mail"; 
      mail.Body = model.Question; 
      if (model.Attachment != null && model.Attachment.ContentLength > 0) 
      { 
       var attachment = new Attachment(model.Attachment.InputStream, model.Attachment.FileName); 
       mail.Attachments.Add(attachment); 
      } 
      client.Send(mail); 
     } 
     return Content("email sent", "text/plain"); 
    } 
} 

和最後一個視圖:

@model QuestionViewModel 

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
    @Html.ValidationSummary(true) 
    <fieldset>   
     <div> 
      @Html.LabelFor(x => x.Question) 
      @Html.TextAreaFor(x => x.Question) 
     </div> 
     <div> 
      <label for="attachment">Attachment</label> 
      <input type="file" name="attachment" id="attachment"/> 
     </div> 
     <input type="submit" value="Send" /> 
    </fieldset> 
} 

進一步改進這個代碼將實際發送的郵件外部化到實現某個接口並使用D的存儲庫中我爲了削弱控制器邏輯和郵件發送邏輯之間的耦合。

請注意,您也可以在web.config中配置SMTP設置:

<system.net> 
    <mailSettings> 
     <smtp from="[email protected]" deliveryMethod="Network"> 
     <network 
      enableSsl="true" 
      host="smtp.gmail.com" 
      port="587" 
      userName="[email protected]" 
      password="secret" 
     /> 
     </smtp> 
    </mailSettings> 
</system.net> 

,然後簡單地說:

using (var client = new SmtpClient()) 
{ 
    var mail = new MailMessage(); 
    mail.To.Add("[email protected]"); 
    mail.Subject = "Test mail"; 
    mail.Body = model.Question; 
    if (model.Attachment != null && model.Attachment.ContentLength > 0) 
    { 
     var attachment = new Attachment(model.Attachment.InputStream, model.Attachment.FileName); 
     mail.Attachments.Add(attachment); 
    } 
    client.Send(mail); 
} 
+0

Tkanks很多!它工作的很棒:) – Marta 2011-06-08 17:39:21

+1

toaddress?我不知道蟾蜍穿着連衣裙! – AaronLS 2011-08-31 15:00:14

+0

如果您試圖使用$ .ajax來傳遞您的'HttpPostedFileBase'文件,該怎麼辦?我試圖通過一個簡單的$ .ajax調用來完成,但我的控制器上有一個空文件。它是我缺少的內容類型嗎? – AdrianoRR 2011-10-05 17:26:31

3

類MAILMESSAGE應該能夠處理你:

http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.attachments.aspx

或者,您在找你的實際代碼的更具體的東西? (例如,如何讀取文件並將其添加到附件中)?

+0

是的,我正在尋找更具體的東西 - 如何從形式讀取文件,並將其添加爲附件的電子郵件,然後發送此郵件。 – Marta 2011-06-08 16:36:28

+0

對不起,太慢了,但很高興有人給你答案:) – 2011-06-08 18:47:36

1
if (model.Attachment != null && model.Attachment.ContentLength > 0) 
    { 
    foreach (HttpPostedFileBase item in fileUploader) 
     { 

     var attachment = new Attachment(model.Attachment.InputStream, model.Attachment.FileName); 
     mail.Attachments.Add(attachment); 
     } 
    } 
0

除了達人的答案,你不必在您的視圖中硬編碼您的文件上傳的輸入和標籤。只是在那裏拍這個:)

@Html.TextBoxFor(model => model.Attachment, new { type = "file" }) 
@Html.LabelFor(model => model.Attachment) 
相關問題