Handling Attachment after Smtp.Send Exception?

M

mirror

Hi.
I'm trying to send an email with attachment with System.Web.Mail
When SMTP server I use is down (or I use a non-existing smtp server)
smtp.send() raises a exception
After that I want to delete the attachment I used.
The problem is that (from what I understand) Windows marks the file
as "to be deleted" and it deletes it only when i close my App.

an example of what I am doing is the following:

private void button1_Click(object sender, System.EventArgs e)
{
SmtpMail.SmtpServer = "localhost";
MailMessage m = new MailMessage();
m.From = "(e-mail address removed)";
m.To ="(e-mail address removed)";
m.Attachments.Add(new MailAttachment("c:\\xx.txt"));

try
{
SmtpMail.Send(m);
}
catch(Exception x)
{
MessageBox.Show(x.Message);
}

try
{
System.IO.File.Delete("c:\\xx.txt");
}
catch(Exception x)
{
MessageBox.Show(x.Message);
}
}
I want the file to be deleted immediately after Smtp.Send() without
closing my App.
Is this possible?
 
N

Nicholas Paldino [.NET/C# MVP]

mirror,

To get around this, have you considered copying the file to a temp
directory, and then attaching that? This way, you can delete the file when
done, and the temp file will get deleted when the app is shut down.

Hope this helps.
 
M

mirror

Thanks Nicholas

But my problem is that the Application (It will be a windows service)
will not be closed unless operating system goes down.
So with the temp directory solution im moving the problem from my Apps
directory to the temporary.

Nicholas Paldino said:
mirror,

To get around this, have you considered copying the file to a temp
directory, and then attaching that? This way, you can delete the file when
done, and the temp file will get deleted when the app is shut down.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

mirror said:
Hi.
I'm trying to send an email with attachment with System.Web.Mail
When SMTP server I use is down (or I use a non-existing smtp server)
smtp.send() raises a exception
After that I want to delete the attachment I used.
The problem is that (from what I understand) Windows marks the file
as "to be deleted" and it deletes it only when i close my App.

an example of what I am doing is the following:

private void button1_Click(object sender, System.EventArgs e)
{
SmtpMail.SmtpServer = "localhost";
MailMessage m = new MailMessage();
m.From = "(e-mail address removed)";
m.To ="(e-mail address removed)";
m.Attachments.Add(new MailAttachment("c:\\xx.txt"));

try
{
SmtpMail.Send(m);
}
catch(Exception x)
{
MessageBox.Show(x.Message);
}

try
{
System.IO.File.Delete("c:\\xx.txt");
}
catch(Exception x)
{
MessageBox.Show(x.Message);
}
}
I want the file to be deleted immediately after Smtp.Send() without
closing my App.
Is this possible?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top