Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Releasing file after sending it as attachment VB.net

Posted on 2009-05-15
5
Medium Priority
?
315 Views
Last Modified: 2012-05-07
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
'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

Open in new window

0
Comment
Question by:MichMat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 12

Expert Comment

by:rgn2121
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.
0
 
LVL 12

Accepted Solution

by:
rgn2121 earned 2000 total points
ID: 24393737
Something like this...
'Code to send email
        smtpServer.Send(msg)
        msg.Dispose()
        smtpServer = Nothing
        msg = Nothing

Open in new window

0
 
LVL 12

Expert Comment

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

Expert Comment

by:burningmace
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 ...
Try
    attFile.Close()
Catch ex As Exception
End Try

Open in new window

0
 

Author Closing Comment

by:MichMat
ID: 31581851
Thank you that fixed the issue
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

688 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