473,486 Members | 2,429 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

deleting email attachments

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

Sep 24 '08 #1
9 4911

"Andrew" <An****@discussions.microsoft.comwrote in message
news:86**********************************@microsof t.com...
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 ...
Sep 24 '08 #2
On Sep 24, 7:54 am, Andrew <And...@discussions.microsoft.comwrote:
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
Sep 24 '08 #3
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
"Arial" wrote:
>
"Andrew" <An****@discussions.microsoft.comwrote in message
news:86**********************************@microsof t.com...
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 ...
Sep 24 '08 #4
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
Sep 24 '08 #5
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
"Andrew" wrote:
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

Sep 24 '08 #6
On Sep 24, 10:47 am, Andrew <And...@discussions.microsoft.comwrote:
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.
Sep 24 '08 #7
On Sep 24, 12:54 pm, Andrew <And...@discussions.microsoft.comwrote:
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.
Sep 24 '08 #8
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
"Ignacio Machin ( .NET/ C# MVP )" wrote:
On Sep 24, 10:47 am, Andrew <And...@discussions.microsoft.comwrote:
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.
Oct 2 '08 #9
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

*** Sent via Developersdex http://www.developersdex.com ***
Oct 21 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2385
by: georgios zakitraxis | last post by:
Hi all, I have a folder with many pictures in it. All have the name like: HHMMDDMMYYYY.jpg H-hour M-minute D-day M-month Y-year
4
3718
by: Paul Schmidt | last post by:
Dear list: I am new to python, and I am trying to figure out the short answer on something. I want to open a POP3 mailbox, read the enclosed mail using the POP3 module, , and then process it...
3
3128
by: LutherRevisited | last post by:
Is there a way I can put a message together without having to download any attachments there may be at the same time. I'm not having any problems dealing with attachments, but the way I'm doing...
6
2624
by: scrimp | last post by:
How would I go about retriving an email message and stripping out the attachment into a file. I have seen examples where they would read in the file and delete the attachment and replace it with...
5
12607
by: morphex | last post by:
Hi, I have an email that's in the utf-8 encoding, and I'm getting this error message when I try to send it using smtplib: * Module smtplib, line 688, in sendmail * Module smtplib, line 485,...
5
15902
by: paii, Ron | last post by:
How do I setup a email with attachment for preview but require the user to push the SEND button in Outlook. I have the following function but it sends the email without the sender ever seeing it. ...
10
4947
by: OdAwG | last post by:
Hello All, Is it possible to send an email from Access? I found a Microsoft article on how to do this but I keep getting an error "RUNTIME ERROR 438" -- Object doesn't support this property or...
2
2629
by: oyster | last post by:
I find that the existing email moudle is some hard for me to understand, especially the part of how to set the CC, BCC and attach the files. Is there any more easy one like this p-code? import...
1
3849
by: budyerr | last post by:
All, I am trying to build a email submission form using asp.net. I currently have a web form page that will upload to my webhosting server, attach to email then delete the file after sending. ...
0
7105
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7132
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7341
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4870
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4564
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
600
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
266
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.