deleting email attachments

  • Thread starter Thread starter Andrew
  • Start date Start date
A

Andrew

Hi,

I want to delete my email attachment after sending the email. How do I get
around this issue ?

I have set up a method to send email using SMTP.
public bool CreateMessageWithAttachment()
{
MailMessage message = new MailMessage();
....
client = new SmtpClient(var.hostName, var.portNo);
client.SendCompleted += new SendCompletedEventHandler
(SendCompletedCallback);

string userState = "test message1";
client.SendAsync(message, userState);
}

This method is called last after the email is actually sent. I can't put a
Dispose() call in the above as I would get an error
"System.Net.Mail.SmtpException: Failure sending mail. --->
System.ObjectDisposedException: Cannot access a closed file.".

public static void SendCompletedCallback(object sender,
AsyncCompletedEventArgs e)
{
if (e.Error != null)
{
Logger.Write(String.Format("Processed: {0}", e.Error.ToString()));
}
else
{
File.Delete(sAtt.Value);
}
}
Thanks in advance

regards,
Andrew
 
Andrew said:
Hi,

I want to delete my email attachment after sending the email. How do I get
around this issue ?

I have set up a method to send email using SMTP.
public bool CreateMessageWithAttachment()
{
MailMessage message = new MailMessage();
....
client = new SmtpClient(var.hostName, var.portNo);
client.SendCompleted += new SendCompletedEventHandler
(SendCompletedCallback);

string userState = "test message1";
client.SendAsync(message, userState);
}

This method is called last after the email is actually sent. I can't put a
Dispose() call in the above as I would get an error
"System.Net.Mail.SmtpException: Failure sending mail. --->
System.ObjectDisposedException: Cannot access a closed file.".

public static void SendCompletedCallback(object sender,
AsyncCompletedEventArgs e)
{
if (e.Error != null)
{
Logger.Write(String.Format("Processed: {0}", e.Error.ToString()));
}
else
{
File.Delete(sAtt.Value);
}
}
Thanks in advance

regards,
Andrew

you will have to call Dispose mannualy on attachment
and mailMessage objects
after mail has been sent

it looks that attachments doesn't get
disposed and garbage collected properly

let us know if this worked ...
 
Thanks for your reply. However I am still having difficulties in accessing
the MailMessage object to dispose of it.

I've tried and got this error when using this code line in :

public static void SendCompletedCallback(object sender,
AsyncCompletedEventArgs e)
{
((MailMessage)sender).Dispose();
}

{"The process cannot access the file 'C:\Project\Output\testDOC.doc' because
it is being used by another process."}
{"Exception has been thrown by the target of an invocation."}

Any ideas ?

Thanks in advance

regards,
Andrew
 
Hi,

I want to delete my email attachment after sending the email. How do I get
around this issue ?

I have set up a method to send email using SMTP.
public bool CreateMessageWithAttachment()
{
MailMessage message = new MailMessage();
....
client = new SmtpClient(var.hostName, var.portNo);
client.SendCompleted += new SendCompletedEventHandler
(SendCompletedCallback);

string userState = "test message1";
client.SendAsync(message, userState);

}

This method is called last after the email is actually sent. I can't put a
Dispose() call in the above as I would get an error
"System.Net.Mail.SmtpException: Failure sending mail. --->
System.ObjectDisposedException: Cannot access a closed file.".

Dispose of what?

Also in your code below you did not include the part of the
attachment.
You are sending it async. so of course you cannot delete the file
after the method ends. You could do it in the SendCompletedCallback
method though
 
Thanks for your reply. If I am understanding you correctly, I am actually
doing that.

public static void SendCompletedCallback(object sender,
AsyncCompletedEventArgs e)
{
MailMessage MM = (MailMessage)sender; //Error msg: Exception has been
thrown by the target of an invocation.
MM.Dispose();
..
..
File.Delete(sAtt.Value);
}



Thanks in advance

regards,
Andrew
 
I've traced the error to this line of code:

client.SendAsync(mailMessage, userToken); //

However in
public static void SendCompletedCallback(object sender,
AsyncCompletedEventArgs e)
{
SmtpClient s = (SmtpClient)sender;
}

I would have thought that s would be of MailMessage class. Not of
SmtpClient class ? How do I access MailMessage within this event ?

Thanks in advance

regards,
Andrew
 
Thanks for your reply. If I am understanding you correctly, I am actually
doing that.

public static void SendCompletedCallback(object sender,
AsyncCompletedEventArgs e)
{
MailMessage MM = (MailMessage)sender; //Error msg: Exception has been
thrown by the target of an invocation.
MM.Dispose();
..
..
File.Delete(sAtt.Value);
}

Thanks in advance

regards,
Andrew

You cannot dispose the sender, think about it, the code that call your
method is holding a reference to it.
Also, check in the debigger the type of the Sender, you are blindly
casting it to a MailMessage.
 
I've traced the error to this line of code:

client.SendAsync(mailMessage, userToken); //

However in
public static void SendCompletedCallback(object sender,
AsyncCompletedEventArgs e)
{
SmtpClient s = (SmtpClient)sender;
}

I would have thought that s would be of MailMessage class. Not of
SmtpClient class ? How do I access MailMessage within this event ?

You can hold a reference to it.
Or a reference to the file you just attached.
 
Thanks for your reply.

I send tis to :
//client.SendAsync(MailMessage message, object userToken)
client.SendAsync(message, userToken);

public static void SendCompletedCallback(object sender,
AsyncCompletedEventArgs e)
{
///How do I access the MailMessage object within here ??
}



Thanks in advance

regards,
Andrew
 
I am having a similar problem.

I connot delete the file recently attached to an email even though I am
not running async.

Thanks,
Kyr
 
Back
Top