Question send mail attachment locks file

itapi

New member
Joined
Apr 22, 2014
Messages
3
Programming Experience
Beginner
hey all!

i would like in my program to send an attachment file in the mail. so i managed to to this but after this i would like to acess the file again ,change somthing there and send it again.

this is the code:

Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load





Dim SmtpServer As New SmtpClient()
SmtpServer.Credentials = New Net.NetworkCredential("user.com ", "dsasd2321d")
SmtpServer.Port = 25
SmtpServer.Host = "smtp.gmail.com"
SmtpServer.EnableSsl = True
Dim omail As New MailMessage()


omail.From = New MailAddress("user.com", "aaa", System.Text.Encoding.UTF8)

omail.Subject = "test subject"
omail.To.Add("iaasd@gmail.com")






Dim attachFile As New Attachment("c:\bit32\bit.text")
omail.Attachments.Add(attachFile)
SmtpServer.SendAsync(omail, Nothing)
MsgBox("emails")





IO.File.AppendAllText("c:\bit32\bit.text", "blbalbablba")




End Sub

End Class

i know how to send a mail with attachment,thats not the point.. the point is i think i need to close the file because when its getting to this line: IO.File.AppendAllText("c:\bit32\bit.text", "blbalbablba")

there is an error which telling me i cant acess the file because its aleardy open by another proccesss... ther is any file.close command or somthing?

can any1 help me plz? its very important .. thank u very much
biggrin.gif
 
You must dispose the Attachment object to release the file.


I thought that I'd already answered this but maybe it was at another forum. The Attachment needs to open the file in order to read the data and send it with the email. You need to dispose the Attachment object in order to release the file.


bro first of all thank u for the help.
so i tryed this line




attacfile.dispose()




in the code:

VB.NET:
VB.NET:
   Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load


    
 




        Dim SmtpServer As New SmtpClient()
        SmtpServer.Credentials = New Net.NetworkCredential("user.com    ", "dsasd2321d")
        SmtpServer.Port = 25
        SmtpServer.Host = "smtp.gmail.com"
        SmtpServer.EnableSsl = True
        Dim omail As New MailMessage()




        omail.From = New MailAddress("user.com", "aaa", System.Text.Encoding.UTF8)


        omail.Subject = "test subject"
        omail.To.Add("iaasd@gmail.com")












        Dim attachFile As New Attachment("c:\bit32\bit.text")
        omail.Attachments.Add(attachFile)
        SmtpServer.SendAsync(omail, Nothing)
        MsgBox("emails")


attachfile.dispose()








        IO.File.AppendAllText("c:\bit32\bit.text", "blbalbablba")









    End Sub


End Class


now theres no error but now its not even sending me the mail... weird no?
 
SendAsync is an asynchronous method, it returns immediately without having completed sending (it hasn't even started). You can't dispose the attachment until the message is sent, which means you either have to use the synchronous Send method or continue your operations when the asynchronous action has completed as notified by the SendCompleted event.
SmtpClient Class (System.Net.Mail)
 
SendAsync is an asynchronous method, it returns immediately without having completed sending (it hasn't even started). You can't dispose the attachment until the message is sent, which means you either have to use the synchronous Send method or continue your operations when the asynchronous action has completed as notified by the SendCompleted event.
SmtpClient Class (System.Net.Mail)

thank u very much for the nice help.

so which command i should use instead of sendasync?
can u give me the currect word/method?
 
thank u very much for the nice help.

so which command i should use instead of sendasync?
can u give me the currect word/method?

It's not necessarily a case of using something else instead of SendAsync. The first thing to do is to determine whether you want to send the message asynchronously or not. If you do then you must use SendAsync. If you don't then you shouldn't have been using SendAsync in the first place. To send the message synchronously, you call Send.

If you do want to send the message asynchronously then you can't dispose the Attachment until the asynchronous operation completes. That means that the file is going to be locked until the message has been sent. In that case, you have two choices:

1. Don't attach the original file. Make a copy of the file and attach that, then delete that after disposing the Attachment.
2. Attach the original file and then wait until the Attachment has been disposed to make your changes.
 

Latest posts

Back
Top