Wednesday, March 21, 2012

Please help me figure this out!

I hope someone answers this thread, I am loosing my mind googling tofind a solution to this and nothing seems to work. I am trying tobuild an application that will let me send an e-mail message with a maxof 5 attachments, the problem that I have faced is that the filesupload fine to the server and then theiy are mailed but for some reason, when I try to add a file.delete() statement after sending the messageI get an error saying: "The file cannot be deleted because it is beingused by another process"... I close explorer, go to the foldercontaining the uploaded files and I try to delete them manually and Ikeep getting the same error "the file is being used by anotherapplicaton" ....
I don't know what I should add to my code tomake this work , here's what I have. I commented out the lines that arethrowing me this error:

Imports System.IO

PartialClass cadil

Inherits System.Web.UI.Page

Dim emaildestinoAsString

Dim strDondeAsString

ProtectedSub btnSend_Click(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles btnSend.Click

Try

If txtSubject.Text =""Then

lblMessage.Visible =True

lblMessage.ForeColor = Drawing.Color.Red

lblMessage.Text ="* you must type your name below *"

ExitSub

EndIf

IfNot ddEscuela.SelectedItem.Value ="NADA"Then

If ddEscuela.SelectedItem.Value ="CADIL"Then

emaildestino = "email0@dotnet.itags.org.mymail.com"

EndIf

If ddEscuela.SelectedItem.Value ="ARCOIRIS"Then

emaildestino = "email1@dotnet.itags.org.mymail.com"

EndIf

If ddEscuela.SelectedItem.Value ="LABORAL"Then

emaildestino = "email2@dotnet.itags.org.mymail.com"

EndIf

Dim emailAsNew System.Net.Mail.MailMessage(txtSubject.Text.ToUpper &" <no_responder@dotnet.itags.org.ultramega.cl>", emaildestino)

email.Subject ="Documentos desde la Web"

email.Body = txtBody.Text

Dim mailClientAsNew System.Net.Mail.SmtpClient

Dim basicAuthenticationInfoAsNew System.Net.NetworkCredential("no_responder@dotnet.itags.org.ultramega.cl","**secret**")

'Put your own, or your ISPs, mail server name on this next line

mailClient.Host ="mail.ultramega.cl"

mailClient.UseDefaultCredentials =False

mailClient.Credentials = basicAuthenticationInfo

Dim attach1AsString =Nothing

Dim attach2AsString =Nothing

Dim attach3AsString =Nothing

Dim attach4AsString =Nothing

Dim attach5AsString =Nothing

Dim strFileNameAsString =Nothing

IfNot Attachment1.PostedFileIsNothingThen

Dim UploadFileAs HttpPostedFile = Attachment1.PostedFile

Dim UploadFileLenAsInteger = UploadFile.ContentLength

If UploadFileLen > 0Then

strFileName = Path.GetFileName(Attachment1.PostedFile.FileName)

strFileName ="attachments/" + strFileName

Attachment1.PostedFile.SaveAs(Server.MapPath(strFileName))

Dim attachAs System.Net.Mail.Attachment =New System.Net.Mail.Attachment(Server.MapPath(strFileName))

email.Attachments.Add(attach)

attach1 = strFileName

EndIf

EndIf

IfNot Attachment2.PostedFileIsNothingThen

Dim UploadFileAs HttpPostedFile = Attachment2.PostedFile

Dim UploadFileLenAsInteger = UploadFile.ContentLength

If UploadFileLen > 0Then

strFileName = Path.GetFileName(Attachment2.PostedFile.FileName)

strFileName ="attachments/" + strFileName

Attachment2.PostedFile.SaveAs(Server.MapPath(strFileName))

Dim attachAs System.Net.Mail.Attachment =New System.Net.Mail.Attachment(Server.MapPath(strFileName))

email.Attachments.Add(attach)

attach2 = strFileName

EndIf

EndIf

IfNot Attachment3.PostedFileIsNothingThen

Dim UploadFileAs HttpPostedFile = Attachment3.PostedFile

Dim UploadFileLenAsInteger = UploadFile.ContentLength

If UploadFileLen > 0Then

strFileName = Path.GetFileName(Attachment3.PostedFile.FileName)

strFileName ="attachments/" + strFileName

Attachment3.PostedFile.SaveAs(Server.MapPath(strFileName))

Dim attachAs System.Net.Mail.Attachment =New System.Net.Mail.Attachment(Server.MapPath(strFileName))

email.Attachments.Add(attach)

attach3 = strFileName

EndIf

EndIf

IfNot Attachment4.PostedFileIsNothingThen

Dim UploadFileAs HttpPostedFile = Attachment4.PostedFile

Dim UploadFileLenAsInteger = UploadFile.ContentLength

If UploadFileLen > 0Then

strFileName = Path.GetFileName(Attachment4.PostedFile.FileName)

strFileName ="attachments/" + strFileName

Attachment4.PostedFile.SaveAs(Server.MapPath(strFileName))

Dim attachAs System.Net.Mail.Attachment =New System.Net.Mail.Attachment(Server.MapPath(strFileName))

email.Attachments.Add(attach)

attach4 = strFileName

EndIf

EndIf

IfNot Attachment5.PostedFileIsNothingThen

Dim UploadFileAs HttpPostedFile = Attachment5.PostedFile

Dim UploadFileLenAsInteger = UploadFile.ContentLength

If UploadFileLen > 0Then

strFileName = Path.GetFileName(Attachment5.PostedFile.FileName)

strFileName ="attachments/" + strFileName

Attachment5.PostedFile.SaveAs(Server.MapPath(strFileName))

Dim attachAs System.Net.Mail.Attachment =New System.Net.Mail.Attachment(Server.MapPath(strFileName))

email.Attachments.Add(attach)

attach5 = strFileName

EndIf

EndIf

mailClient.Send(email)

' If Not attach1 Is Nothing Then

' File.Delete(attach1)

' End If

lblMessage.Visible =True

lblMessage.Text ="your files have been sent."

lblMessage2.Visible =True

Else

lblMessage.Visible =True

lblMessage.ForeColor = Drawing.Color.Red

lblMessage.Text ="* you must select one from the list*"

EndIf

Catch exAs Exception

lblMessage.Text = ex.ToString()

EndTry

EndSub

EndClass


Please someone, any ideas willbe very appreciate......... I'm stuck with this :(
hello?
Perhaps the object is still being held, perhaps by the save operation? I've never used file uploading, so I'm just taking a shot here...

it sounds like the attachment object is still being used (open). try dispose() for each attachment after you send.

http://msdn2.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx


Thank you , I tried adding an attach1.Dispose() after sending the e-mail but I got an error saying Dispose is not a member of String so I believe I am not using the correct object?

I will take a closer look over the weekend

Any additional inputs will be appreciated!


Thank you for your help. I fixed this by adding an

email.Dispose()

statement right after emailclient.send(email)

thanks!!

0 comments:

Post a Comment