Java - Send Email Securely via Gmail API

Asked

Viewed 1,990 times

8

I’m using a API e-mail Mail.jar of this tutorial for sending e-mail via Java, but I realized I have to, activate the permission for less secure apps. There is another way to send email Java, without having to enable this option of Gmail from less secure applications?

Print da seção de Aplicativos menos seguros

  • 1

    Try to perform SMTP connection configuration with active SSL. You can try with this link tutorial: https://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/

  • 3

    There, you have to use Google’s XOAUTH2 stream. Your app directs you to Google, it logs into Google, authorizes your app, and Google redirects you back with a token. With this token, while the user does not deauthorize, your application obtains the login credentials whenever you need to access SMTP.

  • @Carlossilva the code shown in the example is what I use, then have to activate the authorization for less secure applications

  • @Bacco I’m researching on XOAUTH2 and java

  • @Carlos can’t help with the Java part specifically for lack of experience with the language, but the flow is basically the following: your application will direct the user to Google to get an application token on the first access, and then, whenever you use SMTP, you will use this application token to obtain a session token. This session token is used in place of the SMTP password, whenever you need it. When the session token expires, the application token allows you to request a new one. The application never expires, until the user revokes access.

1 answer

1


There are 3 chances to send an email through Gmail:

  • Enable this option for less secure applications, although it cannot be used if the account has active TFA.
  • Use application password, although the user has to create one to send the email.
  • Use the Google API for Java that opens a web page where you can log in to the account, even though it has TFA (recommended).

It is important to know that the Google example comes with premises to see the tags. To change this, just go to the list SCOPES and change GmailScopes.GMAIL_LABELS for GmailScopes.GMAIL_COMPOSE.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.