Memory overload when sending emails with attachments

PROKA

Junior Contributor
Joined
Sep 3, 2003
Messages
249
Location
Bucharest
Hello. I have the following code:

Code:
 Dim mail As New MailMessage(email_from, email_address, Subject, body)

        If EmailBCC <> "" Then mail.Bcc.Add(EmailBCC)

        Dim client As New SmtpClient(email_smtp)
        client.UseDefaultCredentials = False
        client.Credentials = New Net.NetworkCredential(email_user, email_pass)
        client.DeliveryMethod = SmtpDeliveryMethod.Network
        mail.IsBodyHtml = True

        If CuAtasament = True Then
            mail.Attachments.Add(New Attachment(pdf_stream, "Factura " & NrFactura & " - " & Utilizator & ".pdf"))
        End If


        Dim dac As New DataAccess


        Try
            client.Send(mail) 

            Call pdf_stream.Dispose()
            Call mail.Dispose()


            Return True 'Succes

        Catch ex As Exception

Problem is RAM is loading up, and if i have 3000 emails to send, eventually it will crash. Any ideeas on how to clear data from memory?
 
Are you getting any exceptions raised in your code? If so there is a chance the two .Dispose calls at the end of your post are not being called. You might want to move them into a Finally block

e.g.
Visual Basic:
Try
    client.Send(mail) 
    Return True 'Succes
Catch ex As Exception
    'rest of error handler
Finally
    pdf_stream.Dispose()
    mail.Dispose()
End Try
and see if that makes a difference.

You may also find that creating a single instance of the SmtpClient and using for all the mail rather than creating a new one per e-mail might keep the overheads down.
 
Back
Top