2016-06-15 1 views
2

複数のファイルを添付してアップロードできるWebサイトで作業しています。添付されているファイルはApp_Data/uploadsフォルダに保存されています。 App_Data/uploadsフォルダ内のファイルが電子メールで送信された直後に削除される可能性はありますか?ご協力ありがとうございました。これは私のコントローラです:SmtpClient(ASP.NET MVC)経由でメールを送信した後に添付ファイルを削除する

[HttpPost] 
[ValidateAntiForgeryToken] 
public async Task<ActionResult> Index(EmailFormModel model, IEnumerable<HttpPostedFileBase> files) 
{ 
    if (ModelState.IsValid) 
    { 
     List<string> paths = new List<string>(); 

     foreach (var file in files) 
     { 
      if (file.ContentLength > 0) 
      { 
       var fileName = Path.GetFileName(file.FileName); 
       var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName); 
       file.SaveAs(path); 
       paths.Add(path); 
      } 

     } 

      var message = new MailMessage(); 
      foreach (var path in paths) 
      { 
       var fileInfo = new FileInfo(path); 
       var memoryStream = new MemoryStream(); 
       using (var stream = fileInfo.OpenRead()) 
       { 
        stream.CopyTo(memoryStream); 
       } 
       memoryStream.Position = 0; 
       string fileName = fileInfo.Name; 
       message.Attachments.Add(new Attachment(memoryStream, fileName)); 
      } 

      //Rest of business logic here 
      string EncodedResponse = Request.Form["g-Recaptcha-Response"]; 
      bool IsCaptchaValid = (ReCaptcha.Validate(EncodedResponse) == "True" ? true : false); 
      if (IsCaptchaValid) 
      { 

       var body = "<p><b>Email From:</b> {0} ({1})</p><p><b>Subject:</b> {2} </p><p><b>Message:</b></p><p>{3}</p><p><b>Software Description:</b></p><p>{4}</p>"; 
       message.To.Add(new MailAddress("***@gmail.com")); 
       message.From = new MailAddress("***@ymailcom"); 
       message.Subject = "Your email subject"; 
       message.Body = string.Format(body, model.FromName, model.FromEmail, model.FromSubject, model.Message, model.Desc); 
       message.IsBodyHtml = true; 
       using (var smtp = new SmtpClient()) 
       { 
        var credential = new NetworkCredential 
        { 
         UserName = "***@gmail.com", 
         Password = "***" 
        }; 
        smtp.Credentials = credential; 
        smtp.Host = "smtp.gmail.com"; 
        smtp.Port = 587; 
        smtp.EnableSsl = true; 
        await smtp.SendMailAsync(message); 
        ViewBag.Message = "Your message has been sent!"; 

        ModelState.Clear(); 
        return View("Index"); 
       } 
      } else 

      { 
       TempData["recaptcha"] = "Please verify that you are not a robot!"; 
      } 

     } return View(model); 

    } 
+1

を私が編集を提案します彼らは同じ問題を抱えている場合、他の人があなたの質問をよりよく見つけるのを助けるために、あなたの質問のタイトルは "SmtpClient(ASP.NET MVC)経由でメールを送信した後に添付ファイルを削除する" – hsh

答えて

4

あなたが送信した後にファイルを削除するには、SmtpClientのSendCompletedイベントを使用することができます。

smtp.SendCompleted += (s, e) => { 
         //delete attached files 
         foreach (var path in paths) 
          System.IO.File.Delete(path); 
        }; 

ので、送信部は、次のようになります。

using (var smtp = new SmtpClient()) 
{ 
     var credential = new NetworkCredential 
     { 
       UserName = "***@gmail.com", 
       Password = "***" 
     }; 
     smtp.Credentials = credential; 
     smtp.Host = "smtp.gmail.com"; 
     smtp.Port = 587; 
     smtp.EnableSsl = true; 
     smtp.SendCompleted += (s, e) => { 
          //delete attached files 
          foreach (var path in paths) 
           System.IO.File.Delete(path); 
         }; 
     await smtp.SendMailAsync(message); 
     ViewBag.Message = "Your message has been sent!"; 

     ModelState.Clear(); 
     return View("Index"); 
} 
+0

File.Delete Sirのために、 "指定されたコンテキストでは有効ではないメソッドです"というエラーがあります。ご協力ありがとうございました。 :) –

+0

@ KenHemmo、私は完全なエラーを見ることがありますか? – hsh

+1

'System.Web.Mvc.Controller.File(byte []、string)'は、特定のコンテキストで有効でない 'method'です。 " –

関連する問題