0

Does SMTP needs to be disposed in C# after sending email?

I am getting SMTP authentication error, second time when the app starts. I wonder if the SMTP needs to be disposed. Never happens first time on new PC but if same window is open twice then authentication error comes up. here is the code below.

 try{
                            MailMessage mail = new MailMessage();
                            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

                            mail.From = new MailAddress("patrick42@gmail.com");
                            mail.To.Add("feedback@patrick.com");
                            mail.Subject = "FeedBack";
                            mail.Body = "Text ";

                            SmtpServer.Port = 587;
                            SmtpServer.EnableSsl = true;
                            SmtpServer.UseDefaultCredentials = false;
                            SmtpServer.Credentials = new System.Net.NetworkCredential("patrick42@gmail.com", "mypassword");

                            SmtpServer.Send(mail);
                            SmtpServer.Dispose();
                        }
                        catch (Exception)
                        {
                            Application.Exit();
                            this.Close();
                        }
                    }
Bridge
  • 29,818
  • 9
  • 60
  • 82
Patrick
  • 217
  • 1
  • 5
  • 19
  • 2
    A good general rule of thumb seems to be if a class provides a dispose method, it has something that needs disposing and you should probably use it! I don't see how you would get an authentication error though, where you have hardcoded credentials that work _sometimes_... Can you provide the actual error message please? – Bridge Jul 31 '17 at 14:27
  • 1
    If you don't want to dispose manually use the `using( SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com")){/*Your code here*/}` statement. – dcg Jul 31 '17 at 14:29
  • @Bridge Actual Error Message `The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required` – Patrick Jul 31 '17 at 14:48
  • @dcg M I disposing correctly or incorrectly? do I really need to dispose in my situation? there are no file attachments. first installation no error message, second time the form starts up i get this error.. not sure why it comes up. – Patrick Jul 31 '17 at 14:49

1 Answers1

0

According to your error message, I can suggest this post. or you should allow less secure apps that use your mail account on google. I copied your code and modified for my gmail account. It worked without error. You can follow these steps for allowing your form app to access your gmail account.

Murat Seker
  • 890
  • 2
  • 14
  • 26
  • I already have followed these steps and this had fixed my problem originally, now it's not that. it's when after sometime the feedback form is opened again, it gives me the same authentication error, despite all the steps listed on these posts are done already. Whenever a new PC installs the software on first run, it works fine, but on second run it throws this message. – Patrick Aug 01 '17 at 18:25