Home > OS >  how make invisible the password with Mailkit in Authenticate if the SMTP server requires authenticat
how make invisible the password with Mailkit in Authenticate if the SMTP server requires authenticat

Time:11-21

I understand that SmtpClient doesn't support many modern protocols so the recommendation is use MailKit, but with SmtpClient using System.Net.Mail the password was not required. With Mailkit I must Authanticate with password in my code.

using (var client = new SmtpClient ()) {
                client.Connect ("smtp.friends.com", 587, false);

                // Note: only needed if the SMTP server requires authentication
                client.Authenticate ("joey", "password");

                client.Send (message);
                client.Disconnect (true);
            }

Could I don't use the password visible in my code, please ? How ?

Thank you

CodePudding user response:

You could load the password from an XML (or JSON or any other format) config file.

It's also possible that your SMTP server doesn't require authentication, so you should at least check that.

CodePudding user response:

this is my way of doing this:

Environment.GetEnvironmentVariable("Password")

You need to go to Environment variables in Windows settings:

enter image description here

Now:

enter image description here

Nazwa zmiennej = Environment variable (Password) Wartość zmiennej = value (your password)

Remember to reload Visual Studio after adding Environment variable because VS load it at the start.

Your code with my method will look like this:

using (var client = new SmtpClient ()) {
                client.Connect ("smtp.friends.com", 587, false);

                // Note: only needed if the SMTP server requires authentication
                client.Authenticate ("joey", Environment.GetEnvironmentVariable("Password"));

                client.Send (message);
                client.Disconnect (true);
            }
  • Related