How to send email by using MailKit?

前端 未结 2 1455
长情又很酷
长情又很酷 2020-12-04 16:48

According to the new google politics https://googleonlinesecurity.blogspot.de/2014/04/new-security-measures-will-affect-older.html I can\'t sent an email. \"Less secure apps

2条回答
  •  离开以前
    2020-12-04 17:32

    Tested following code and works for me:

            // STEP 1: Navigate to this page https://www.google.com/settings/security/lesssecureapps & set to "Turn On"
    
            var message = new MimeMessage();
            message.From.Add(new MailboxAddress("Joey Tribbiani", "YOU_FROM_ADDRESS@gmail.com"));
            message.To.Add(new MailboxAddress("Mrs. Chanandler Bong", "YOU_TO_ADDRESS@gmail.com"));
            message.Subject = "How you doin'?";
    
            message.Body = new TextPart("plain")
            {
                Text = @"Hey Chandler,I just wanted to let you know that Monica and I were going to go play some paintball, you in?-- Joey"
            };
    
            using (var client = new SmtpClient())
            {
                client.Connect("smtp.gmail.com", 587);
    
    
                // Note: since we don't have an OAuth2 token, disable
                // the XOAUTH2 authentication mechanism.
                client.AuthenticationMechanisms.Remove("XOAUTH2");
    
                // Note: only needed if the SMTP server requires authentication
                client.Authenticate("YOUR_GMAIL_NAME", "YOUR_PASSWORD");
    
                client.Send(message);
                client.Disconnect(true);
            }
    

提交回复
热议问题