Danny Rock
Danny Rock

Reputation: 89

Cant use gmail smtp to send email via c# form app

I wrote this code in c# .net form app to send emails. Code is working with yahoo,hotmail,gmx by replacing the smtp servers name but not working with gmail,

            try
            {

                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                var mail = new MailMessage();
                mail.From = new MailAddress(youremail.Text);
                mail.To.Add(txtreceiver.Text);
                mail.Subject = txtsubject.Text;
                mail.IsBodyHtml = true;
                mail.Body = txtbody.Text;
                SmtpServer.Port = 465;
                SmtpServer.UseDefaultCredentials = false;
                SmtpServer.Credentials = new System.Net.NetworkCredential(youremail.Text, yourpass.Text);
                SmtpServer.EnableSsl = true;
                SmtpServer.Send(mail);
                MessageBox.Show("Sent sucessfully..!  \n If Email is not found in inbox check junk ");
            }
            catch (Exception s)
            {
                MessageBox.Show("Failled To Send Mail..!");
            }

Upvotes: 0

Views: 929

Answers (1)

easuter
easuter

Reputation: 1197

Firstly, you must use port 587 as @user1666620 suggested in the comments.

Then you will also need to allow "less secure" devices to access that GMail account. Click on your account avatar, then "My Account" -> "Sign-in & Security" -> "Connected Apps & Sites". At the bottom of that page, toggle the "Allow less secure apps" option.

Upvotes: 3

Related Questions