简体   繁体   中英

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..!");
            }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM