Click here to Skip to main content
15,916,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I am using vb.net 2013. I have a program to send an email to my clients. This email has 2 attachments. One word document and one PDF file. My program collecting both these file paths with a "OpenFileDialog" in vb.net. After collecting these paths, i store them in a list of string. Then i send these files using MailMessage class. After sending emails, i need to move these file into a specific folder. But i got an error message which shows one process is using these files. How do i move these files ?
Now, i am using a work around. I just save the file paths in a text file after sending mails. And later, in form load event, my program will read these paths and move all the files.

Here is my function to send an email.

What I have tried:

Public Function SendMail()
        Dim Result As Boolean = False
        Try
            Dim mail As New MailMessage
            mail.Subject = Me.m_Subject
            mail.To.Add(Me.m_MailAddress)
            If Me.m_ccStatus = True Then
                mail.CC.Add(Me.m_CC)
            End If
            mail.From = New MailAddress(Me.m_Credents.UserName)
            mail.Body = Me.m_BodyText

            Dim attachF As Net.Mail.Attachment
            For Each i As String In Me.m_ListOfAttachments
                attachF = New Net.Mail.Attachment(i)
                mail.Attachments.Add(attachF)
            Next
            Dim smtp As New SmtpClient(Me.m_SmtpClient)
            smtp.EnableSsl = True
            smtp.Credentials = New System.Net.NetworkCredential(Me.m_Credents.UserName, Me.m_Credents.PassWord)
            smtp.Port = Me.m_PortNumber
            smtp.Send(mail)
            Result = True
        Catch ex As Exception
            Throw New Exception("Can't send e-mail")
        End Try
Posted
Updated 4-Apr-19 23:51pm
Comments
Vinod Kc 5-Apr-19 5:39am    
Never mind, I think i found the answer myself.
Instead of this line---
Dim mail As New MailMessage

I wrote this ----
Using mail As New MailMessage

I didn't properly disposed the mailmessage object. That was the reason. Now, problem solved.

1 solution

Answered only to remove from unanswered queue - solved by OP.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900