Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 42312

VS 2010 [RESOLVED] Closing smtpClient

$
0
0
hi everyone again ,

im making a program that sending a mail..
here i wants
1.save richtextbox as any txt file anywhere in my computer
2.add Attachment (that txt file) to my mail
3.send mail (without mailbody, only attachment file)
4.Delete txt file

Problem is Deleting file. I can not close the smtpClient , it is using txt file so im getting error "IOException was unhandled , file using with other programs.... "
i tried Dispose() isnt working and there is no Close()..

Here my codes;
PHP Code:

Dim filename As String "\" + Today + ".txt"
        Dim foldername As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        Dim path As String = String.Concat(foldername, filename)
        Dim write As IO.StreamWriter = New IO.StreamWriter(path)
        write.Write(RichTextBox1.Text)
        write.Dispose()
        write.Close()

        Dim MyMailMessage As New MailMessage()
        MyMailMessage.From = New MailAddress("
mymail")
        MyMailMessage.To.Add("
anymail")
        MyMailMessage.Subject = ("
mail subject")
        MyMailMessage.Attachments.Add(New Attachment(path))
        MyMailMessage.Body = ("")
        MyMailMessage.SubjectEncoding = System.Text.Encoding.UTF8

        Dim SMTPServer As New SmtpClient("
smtp.gmail.com")
        SMTPServer.Port = 587
        SMTPServer.Credentials = New System.Net.NetworkCredential("
mymail", "mypass")
        SMTPServer.EnableSsl = True
        SMTPServer.Send(MyMailMessage)
        SMTPServer.Dispose()
        IO.File.Delete(path) ' this code doesnt work
        MsgBox("
Sent") 

i assumed problem is streamwriter at first but , i tried this code in other programs and it works ,

PHP Code:

Dim path As String "F:\text.txt"
        
Dim write As IO.StreamWriter = New IO.StreamWriter(path)
        
write.Write(RichTextBox1.Text)
        
write.Dispose()
        
write.Close()
        
IO.File.Delete(path

this means problem is smtpclient. it is using my file so i cant delete it.
how can i close smtpClient or is there other ways to make what i wants?
(sorry about my english)

Viewing all articles
Browse latest Browse all 42312

Trending Articles