send email

Andy75

Member
Joined
May 29, 2020
Messages
18
Programming Experience
1-3
Hi all,
I need to send emails from a Windows Forms program in c#.
I have to use gmail's smtp, because the account is gmail.
I saw that you can create passwords in google settings for use in apps.

I would like to create a safe and reliable method.
I have already found the code in c#, I wanted to know if it is correct to create a password for the apps and use them in my program, or if there are other more modern methods.
I can't ask the customer for the account password, that's why I was thinking of this solution.

Thank you
 
Using gmail's SMTP requires to remove oAUTH token and security from Gmail account, or use the oAUTH Token for authentication, the problem using the oAUTH token is that such token has an expiration, meaning that you have to maintain the Gmail token when expires.
I recommend using SendGrid or Mailtrap FREE SMTP.

Change the SMTP_HOST, SMTP_Username, and SMTP_Password with real.

Send email:
var smtpClient = new SmtpClient(SMTP_HOST)
            {
                Port = SMTP_PORT,
                Credentials = new NetworkCredential(SMTP_Username, SMTP_Password),               
                EnableSsl = false,

            };
            try
            {
                //-- emailFrom, emailTo, Subject, Body
                smtpClient.Send(SMTP_Username, RecipientEmailAddress, "Testing", "Testing SMTP Messages");
            }
            catch (Exception e)
            {
               Console.WriteLine(e.Message.ToString());
            }
 
The safe and modern way as the OP is asking for is to use OAuth with the server side flow. By using the traditional user name and password, you are now encumbered with protecting that password, and hope that the user also remembers to update that password in your app whenever they change your password. That GMail password is their Google account password which will be access for everything Google related. With using OAuth, you never know what the user's password is. The user consents to let you act on their behalf only for sending email.
 

Latest posts

Back
Top Bottom