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 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 ...
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
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 ...
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
"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
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.
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.
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.
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 *** This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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...
|
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...
|
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...
|
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,...
| |
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.
...
|
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...
|
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...
|
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. ...
|
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,...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |