问题
I am able to send mail in java to gmail, when "Allow less secure apps: ON
"(you can find code here ).But am getting an error when "Allow less secure apps: OFF". please give me proper steps to resolve this.
Error:
534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbtDY
534-5.7.14 01tH8y-udRciS_SO5d08uqHUrrFPAbG1_XdRaoO-x_gAICwxl_UIsENPSoTXVrNqFs5BcR
534-5.7.14 KXMaVoEIwR9uACjnduELTwJg0SxLrXL5CltztHepubHzUzgC6h-iszlhKrUuckV1-k6FWS
534-5.7.14 jpMrpomFM2k_UcOdDm7SyTde8MadhGyxmYDZ5KZAHcqkRGKjR6hU7oY8hUaTFocikPxquP
534-5.7.14 B2iqPFDd4NLZqv6qGfDNK0q_i17s> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/answer/78754 ud10sm34016765pab.27 - gsmtp
How I got token:
step 1: I created service account in google developer console.
step 2: Next, by using this service account I got token. Below, you can find the code.
step 3: How to send mail by using this token to gmail?? please guide me.thks in adv I am new to this. Guide me, if I am wrong
String emailAddress = "XXXX@XXXX.XX.gserviceaccount.com";
JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
HttpTransport httpTransport;
try {
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(emailAddress)
.setServiceAccountPrivateKeyFromP12File(new File("XXXX.p12"))
.setServiceAccountScopes(Collections.singleton(SQLAdminScopes.SQLSERVICE_ADMIN))
.build();
System.out.println("success......");
credential.refreshToken();
token = credential.getAccessToken();
回答1:
Upgrade to a more secure app that uses the most up to date security measures.
How:
- First read this
https://developers.google.com/identity/protocols/OAuth2 it describes why and how it works - Here is the library you need to import into your project
https://developers.google.com/api-client-library/java/google-api-java-client/oauth2
It pretty simple, you just need to go through all the steps, everything is well explained.
回答2:
If you dont have 2 step verification turned on then the following tutorial will allow you to send mail through GMail using javax.mail with little to no fuss. It uses SSL during transport but on slightly less restrictive port (587 as opposed to 465), you will have to remember to make sure to add the ("mail.smtp.starttls.enable", "true") property or you will not be using SSL. Its also worthwhile remembereing that this method will try to use ssl but if the server doesnt have ssl capabilities it WILL transmit in plain-text.
来源:https://stackoverflow.com/questions/35550064/how-to-send-a-mail-in-java-to-gmail-when-allow-less-secure-apps-off