Releasing file after sending it as attachment

Posted on 2009-05-15
Last Modified: 2012-05-07

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

'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)


        End If




            MsgBox("File Sent")

        Catch ex As Exception

            MessageBox.Show(ex.Message, "Error Mail Not Sent")

        End Try


    End Sub

Open in new window

Question by:MichMat
  • 3
LVL 12

Expert Comment

ID: 24393730
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.
LVL 12

Accepted Solution

rgn2121 earned 500 total points
ID: 24393737
Something like this...
'Code to send email



        smtpServer = Nothing

        msg = Nothing

Open in new window

LVL 12

Expert Comment

ID: 24393745
I know that it doesn't retain access, because I delete the file after it has been sent...

Expert Comment

ID: 24393748
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 ...



Catch ex As Exception

End Try

Open in new window


Author Closing Comment

ID: 31581851
Thank you that fixed the issue

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now