.net core C# SMTP gmail

 

 [HttpGet]

        public IActionResult Get()

        {

            string to = "carlosauyeung306@gmail.com";

            string from = "carlosauyeung306@gmail.com";

            string subject = "Using the new SMTP client.";

            string body = @"Using this new feature, you can send an email message from an application very easily.";



            MailAddress fromAddress = new MailAddress(from);

            MailAddress toAddress = new MailAddress(to);


            MailMessage mail = new MailMessage(fromAddress.Address, toAddress.Address);

            mail.Subject = "Testing";

            mail.Body = "contents.";


            SmtpClient client = new SmtpClient();

            client.Host = "smtp.gmail.com";

            client.Port = 587;

            client.EnableSsl = true;

            client.Timeout = 10000;

            client.UseDefaultCredentials = false;

            client.Credentials = new NetworkCredential("carlosauyeung306@gmail.com", "password");




            try

            {

                client.Send(mail);

            }

            catch (Exception ex)

            {

                var a = ex.Message;

            

            }




            return Ok();


        }









Error


寄送 Gmail 出現 The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required. 解決方法

https://blog.no2don.com/2021/01/c-gmail-smtp-server-requires-secure.html

Solution:

到這網址把 低安全性打開 https://myaccount.google.com/lesssecureapps

留言

這個網誌中的熱門文章

香港袐密行動

要老是忘記, 我更記不起