Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Am i correctly disposing this?

Basicly, my program needs to call this method alot, and i was wondering if everytime it calls the method, the old instances(var client = new SmtpClient("smtp.gmail.com", 587) as an example) gets disposed, cashed in memory or overwritten. What im trying to say is that, is this the right way of doing it?

Code
// Email sender method
        public static void SendEmail(string To, string Title, string Body)
        {
            var client = new SmtpClient("smtp.gmail.com", 587)
            {
                Credentials = new NetworkCredential(Username, Password),
                EnableSsl = true
            };

            client.Send(To, To, Title, Body);
        }
Well, if the SmtpClient instance needs to be only defined once then do so instead of creating multiple instances with the same credentials. Define the stuff that stays the same as the class level. Also, to effectively dispose of an object you'd need to use the using block.

Code
using (var client = new SmtpClient(string, port)
{


}
It should be fine so since SmtpClient doesn't have a dispose method

Edit: Nvm, it actually implements IDisposable in .NET 4.0
Create a class that implements IDisposable to dispose of the SmtpClient internally once your class is done using it. You only need a single instance of the client, and since you'll be calling it many times constructing one with the same parameters it looks like, it would make more sense to only create it once, send a bunch of emails using the single instance, then dispose of it once you're done using it.

Btw - you're not at all properly disposing anything. You're just creating a bunch of instances it looks like, and the only thing that would cause the disposing would be the GC.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(Aug 17, 2017 02:54 PM)+Haze Wrote:  Well, if the SmtpClient instance needs to be only defined once then do so instead of creating multiple instances with the same credentials. Define the stuff that stays the same as the class level. Also, to effectively dispose of an object you'd need to use the using block.
Code
using (var client = new SmtpClient(string, port) { }

(Aug 17, 2017 03:13 PM)Electron Wrote:  It should be fine so since SmtpClient doesn't have a dispose method Edit: Nvm, it actually implements IDisposable in .NET 4.0

(Aug 17, 2017 03:41 PM)0xDEAD10CC Wrote:  Create a class that implements IDisposable to dispose of the SmtpClient internally once your class is done using it. You only need a single instance of the client, and since you'll be calling it many times constructing one with the same parameters it looks like, it would make more sense to only create it once, send a bunch of emails using the single instance, then dispose of it once you're done using it. Btw - you're not at all properly disposing anything. You're just creating a bunch of instances it looks like, and the only thing that would cause the disposing would be the GC.

Thank you all for the help, i solved the issue 🙂


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by LynxJan 3, 2022 04:47 PM 1 184 Jan 3, 2022 04:47 PM
Last Post: yuumi
[C] by lo0srApr 12, 2021 10:32 PM 1 135 Apr 12, 2021 10:32 PM
Last Post: Sajuuk
[C] by ❖Onyx❖Oct 18, 2019 04:48 PM 11 353 Oct 18, 2019 04:48 PM
Last Post: SKIDROOTKIT