MichMat
asked on
Releasing file after sending it as attachment VB.net
Hi
I have a problem, using the code bellow I send a file as attachment, but I have trouble as the program dose not release it after the file is sent.
Please help me to release the file so that other processes can use it
Michal
I have a problem, using the code bellow I send a file as attachment, but I have trouble as the program dose not release it after the file is sent.
Please help me to release the file so that other processes can use it
Michal
'Sending module
sender = Form1.TextBox42.Text
recipient = Form1.TextBox50.Text
Subject = "Todays File " & Fdate & ".csv"
attachmentString = ArchiveLocation & "\Archives\" & AnyDate.Year.ToString("0000") & "\" & AnyDate.Month.ToString("00") & "\" & Fdate & ".csv"
Dim fromAddress As New Net.Mail.MailAddress(sender)
Dim toAddress As New Net.Mail.MailAddress(recipient)
Dim message As New Net.Mail.MailMessage(fromAddress, toAddress)
Dim mailSender As Net.Mail.SmtpClient
' Change the Smtp server name on the next line
mailSender = New Net.Mail.SmtpClient(Form1.TextBox41.Text, Form1.TextBox55.Text)
' message.Bcc.Add(fromAddress)
message.Subject = Subject
message.IsBodyHtml = True
message.Body = "Todays CSV File "
If Not attachmentString = "" Then
Dim msgAttach As New Net.Mail.Attachment(attachmentString)
message.Attachments.Add(msgAttach)
End If
Try
mailSender.Send(message)
MsgBox("File Sent")
Catch ex As Exception
MessageBox.Show(ex.Message, "Error Mail Not Sent")
End Try
101:
End Sub
Do you dispose of your message object? I have never had that issue and I have an app that exports data to Excel, attches it and sends it out.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I know that it doesn't retain access, because I delete the file after it has been sent...
Load the attachment as an IO stream:
Dim fileName As String = "C:\file.txt"
Dim attFile As New FileStream(fileName, FileMode.Open, FileAccess.Read)
Dim att As New Net.Mail.Attachment(attFile, "attachment-file-name.txt")
' ... send code goes here ...
Try
attFile.Close()
Catch ex As Exception
End Try
ASKER
Thank you that fixed the issue