当“允许不太安全的应用程序:关闭”时,如何将邮件以java发送到Gmail [英] How to send a mail in java to Gmail, when "Allow less secure apps: OFF"

查看:1214
本文介绍了当“允许不太安全的应用程序:关闭”时,如何将邮件以java发送到Gmail的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以发送邮件到java,当允许不安全的应用程序:ON (你可以找到代码这里)。但是当允许少时会出现错误安全应用程式:OFF。请给我正确的步骤来解决这个问题。

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.

错误:

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 

我如何获得令牌:

步骤1:我在Google开发者控制台中创建了服务帐户。

step 1: I created service account in google developer console.

步骤2:接下来,通过使用这个服务帐号,我得到了令牌。下面你可以找到代码。

step 2: Next, by using this service account I got token. Below, you can find the code.

步骤3:如何使用这个令牌发送邮件给Gmail?请指导我。adv我是新来的。指导我,如果我错了

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(); 


推荐答案

如果您没有打开2步验证,那么遵循教程将允许您发送邮件通过GMail使用javax.mail几乎没有大惊小怪。它在运输过程中使用SSL,但限制在较少的端口(587而不是465),您必须记住确保添加(mail.smtp.starttls.enable,true)属性,否则您将不会使用SSL。它也值得记住,这种方法将尝试使用ssl,但如果服务器没有ssl功能,它将以纯文本传输。

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.

这篇关于当“允许不太安全的应用程序:关闭”时,如何将邮件以java发送到Gmail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆