2012-01-06 77 views
1

我必須從我的ASP.NET MVC 2聯繫表單視圖頁面發送電子郵件。我需要一個詳細的答案,描述如何創建模型,控制器和視圖爲此目的..這裏是我給的代碼在我的控制器類的動作方法..如何使用附件從ASP.NET MVC視圖頁發送電子郵件?

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult SendEMail(CareersEMailModel careersEMailModel,HttpPostedFileBase upload) 
{ 
    if (ModelState.IsValid) 
    { 
      bool isOK = false; 

      try 
      { 
       MailMessage msg = new MailMessage(); 
       msg.From = new MailAddress("[email protected]", "Website contact form"); 
       msg.To.Add("[email protected]"); 
       msg.Subject = "Resume"; 
       string body = "Name:" + careersEMailModel.Name + "\n" + "Phone:" + careersEMailModel.Phone + "\n" + "Email:" + careersEMailModel.Email; 
       string file = careersEMailModel.Resume; 
       msg.Body = body; 
       msg.IsBodyHtml = false; 
       SmtpClient smtp = new SmtpClient("mailserver_url.net", 25); 
       smtp.Send(msg); 
       msg.Dispose(); 
       isOK = true; 
       CareersMessageModel rcpt = new CareersMessageModel(); 
       rcpt.Title = "Email sent successfully!!"; 
       rcpt.Content = "Your details has been received with great thanks.We'll contact you as soon as possible."; 
       return View("CareersMessage", rcpt); 
      } 
      catch (Exception ex) 
      { 
       CareersMessageModel err = new CareersMessageModel(); 
       err.Title = "Sorry,Email sending failed!!!"; 
       err.Content = "The website is having an error with sending this mail at this time.You can send an email to our address provided in our contact us form.Thank you."; 
       return View("CareersMessage", err); 
      } 
     } 
     else 
     { 
      return View(); 
     } 
    } 

回答

0

MSDN

public static void CreateMessageWithAttachment(string server) 
    { 
     // Specify the file to be attached and sent. 
     // This example assumes that a file named Data.xls exists in the 
     // current working directory. 
     string file = "data.xls"; 
     // Create a message and set up the recipients. 
     MailMessage message = new MailMessage(
      "[email protected]", 
      "[email protected]", 
      "Quarterly data report.", 
      "See the attached spreadsheet."); 

     // Create the file attachment for this e-mail message. 
     Attachment data = new Attachment(file, MediaTypeNames.Application.Octet); 
     // Add time stamp information for the file. 
     ContentDisposition disposition = data.ContentDisposition; 
     disposition.CreationDate = System.IO.File.GetCreationTime(file); 
     disposition.ModificationDate = System.IO.File.GetLastWriteTime(file); 
     disposition.ReadDate = System.IO.File.GetLastAccessTime(file); 
     // Add the file attachment to this e-mail message. 
     message.Attachments.Add(data); 

     //Send the message. 
     SmtpClient client = new SmtpClient(server); 
     // Add credentials if the SMTP server requires them. 
     client.Credentials = CredentialCache.DefaultNetworkCredentials; 

    try { 
      client.Send(message); 
     } 
     catch (Exception ex) { 
      Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}", 
       ex.ToString());    
     } 
     // Display the values in the ContentDisposition for the attachment. 
     ContentDisposition cd = data.ContentDisposition; 
     Console.WriteLine("Content disposition"); 
     Console.WriteLine(cd.ToString()); 
     Console.WriteLine("File {0}", cd.FileName); 
     Console.WriteLine("Size {0}", cd.Size); 
     Console.WriteLine("Creation {0}", cd.CreationDate); 
     Console.WriteLine("Modification {0}", cd.ModificationDate); 
     Console.WriteLine("Read {0}", cd.ReadDate); 
     Console.WriteLine("Inline {0}", cd.Inline); 
     Console.WriteLine("Parameters: {0}", cd.Parameters.Count); 
     foreach (DictionaryEntry d in cd.Parameters) 
     { 
      Console.WriteLine("{0} = {1}", d.Key, d.Value); 
     } 
     data.Dispose(); 
    } 

編輯:

附件類接受流。所以試試這個。 (我沒有測試它,但它應該給你的,你需要做的要點)

foreach (string fileName in Request.Files) 
{ 
    HttpPostedFile file = Request.Files[fileName]; 

Attachment data = new Attachment(file.InputStream, fileName); 
    // do stuff to attach it to the Mail Message 

} 
+0

非常感謝親愛的.. :)但我需要做的是發送從網上聯繫表格一封電子郵件,在運行時的附件itself.Can我做沒有將其上傳到我的項目中的臨時文件夾?我可以直接從我的asp.net MVC網絡聯繫表單中將它發送到特定的電子郵件地址嗎? – 2012-01-06 11:56:23

+0

以及你的例子中不清楚。 – Peter 2012-01-06 20:59:02

0

用於檢索上傳的文件,你需要幫助做到這一點

foreach (string file in Request.Files) 
{ 
    var uploadFile = Request.Files[file]; 
    if (uploadFile.ContentLength == 0) continue; 
    string fileLocation = //File Location with file name, needs to be stored for temporary purpose 
    uploadFile.SaveAs(fileLocation); 
} 

然後以下代碼可以附加文件

Attachment data = new Attachment(fileLocation, MediaTypeNames.Application.Octet); 
    message.Attachments.Add(data); 

一旦完成郵件刪除在服務器上創建的文件。

希望這回答你的問題

相關問題