问题
I am getting this message since Yahoo have made changes in user authentication. My code still works if I use option suggested by Yahoo, which is:
While you switch apps or update your app, you can give the old app temporary access. Just remember, this is not intended for permanent access and you'll want to turn it off afterwards. Turn on Allow apps that use less secure sign in. Once the feature is turned on, you'll get regular alerts reminding you to turn it off...
My question is how to update my code to work without turning on
Allow apps that use less secure sign in.
On Yahoo side?
Code:
private void Test_Click(object sender, EventArgs e)
{
MailMessage mail = new MailMessage("userid@yahoo.com", "user@example.com", "Test", "Hello World");
SmtpClient smtp = new SmtpClient();
smtp.UseDefaultCredentials = false;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.EnableSsl = true;
smtp.Host = "smtp.mail.yahoo.com";
smtp.Port = 587;//I tried 465 as well
smtp.Credentials = new NetworkCredential( "userid@yahoo.com", "myPassword");
smtp.Send(mail);
}
回答1:
To avoid using that option you would need to use OAuth2 for authentication instead of a user ID and password.
You would need to register your application with Yahoo. Then go through the OAuth2 workflow:
- Get a request token from Yahoo
- Launch a Web browser and send the user to Yahoo to authorize your application and get an authorization key
- Trade the authorization key for an access token
- Store the access token somewhere and use it when logging in with SMTP
You're going to want third-party libraries for this. I don't think SmtpClient supports OAuth, and you'll want a library to handle the other OAuth details for you.
Here is an example of how to the OAuth this with MailBee.Net (a commercial e-mail library).
来源:https://stackoverflow.com/questions/52527061/system-net-mail-smtpexception-mailbox-unavailable-the-server-response-was-re