Somen Natta

Somen Natta

  • NA
  • 33
  • 3.3k

Delete Uploaded Image After Sending An Email

Apr 16 2021 9:35 AM
I want to delete the image after sending the image by email. But I get one exception as follows:
The process cannot access the file 'C:\Users\LTP-Dell-72\source\repos\Demo2\Demo2\Images\Koala.jpg' because it is being used by another process.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Net.Mail;  
  8. using System.IO;  
  9. using System.Text;  
  10. using System.Net.Mime;  
  11. namespace Demo2  
  12. {  
  13. public partial class Email : System.Web.UI.Page  
  14. {  
  15. protected void Page_Load(object sender, EventArgs e)  
  16. {  
  17. }  
  18. protected void btn_send_Click(object sender, EventArgs e)  
  19. {  
  20. try  
  21. {  
  22. MailMessage message = new MailMessage();  
  23. message.To.Add(txtEmail.Text);// Email-ID of Receiver  
  24. message.Subject = txtSubject.Text;// Subject of Email  
  25. message.From = new  
  26. System.Net.Mail.MailAddress("****");// Email-ID of Sender  
  27. message.IsBodyHtml = true;  
  28. message.AlternateViews.Add(Mail_Body());  
  29. SmtpClient SmtpMail = new SmtpClient();  
  30. SmtpMail.Host = "smtp.gmail.com";//name or IP-Address of Host used for SMTP transactions  
  31. SmtpMail.Port = 587;//Port for sending the mail  
  32. SmtpMail.Credentials = new  
  33. System.Net.NetworkCredential("****","****");//username/password of network, if apply  
  34. SmtpMail.DeliveryMethod = SmtpDeliveryMethod.Network;  
  35. SmtpMail.EnableSsl = true;  
  36. SmtpMail.ServicePoint.MaxIdleTime = 0;  
  37. SmtpMail.ServicePoint.SetTcpKeepAlive(true, 2000, 2000);  
  38. message.BodyEncoding = Encoding.Default;  
  39. message.Priority = MailPriority.High;  
  40. SmtpMail.Send(message); //Smtpclient to send the mail message  
  41. //message = null;  
  42. message.Dispose();  
  43. String FileName = fileUpload1.PostedFile.FileName;  
  44. string path1 = Server.MapPath("~/Images/");  
  45. string path = path1 + FileName;  
  46. //FileInfo TheFile = new FileInfo(path);  
  47. //if (TheFile.Exists)  
  48. //{  
  49. File.Delete(path); // It not works if file is used in another proces  
  50. //}  
  51. Response.Write("Email has been sent");  
  52. }  
  53. catch (Exception ex)  
  54. { Response.Write("Failed"); }  
  55. }  
  56. private AlternateView Mail_Body()  
  57. {  
  58. String FileName = fileUpload1.PostedFile.FileName;  
  59. string path1 = Server.MapPath("~/Images/");  
  60. string path = path1 + FileName;  
  61. LinkedResource Img = new LinkedResource(path, MediaTypeNames.Image.Jpeg);  
  62. Img.ContentId = "MyImage";  
  63. string str = @"  
  64. <table>  
  65. <tr>  
  66. <td> '" + txtmessagebody.Text + @"'  
  67. </td>  
  68. </tr>  
  69. <tr>  
  70. <td>  
  71. <img src=cid:MyImage id='img' alt='' width='100px' height='100px'/>  
  72. </td>  
  73. </tr></table>  
  74. ";  
  75. AlternateView AV =  
  76. AlternateView.CreateAlternateViewFromString(str, null, MediaTypeNames.Text.Html);  
  77. AV.LinkedResources.Add(Img);  
  78. return AV;  
  79. }  
  80. }  
  81. }  

Answers (2)