Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to send e-mails with GMail API using user name and password only?

I was told by a colleague that using the Gmail API library, you can send e-mails from corporate Gmail account with user name and password.

So far I could not find out how exactly I can build an instance of the Gmail with user name and password (no access token, no secret).

Is it at all possible to use GMail API for sending e-mails with just user name and password? If so, where can I find an example?

Update 1: When I use the following code

@Test
fun sendMail() {
    val credentials = Properties()
    credentials.load(FileInputStream("src/test/resources/credentials.properties"))

    val username = credentials.getProperty("username")
    val password = credentials.getProperty("password")

    val prop = Properties()
    prop.put("mail.smtp.host", "smtp.gmail.com")
    prop.put("mail.smtp.port", "587")
    prop.put("mail.smtp.auth", "true")
    prop.put("mail.smtp.starttls.enable", "true") //TLS

    val session = Session.getInstance(prop,
            object : javax.mail.Authenticator() {
                override fun getPasswordAuthentication(): PasswordAuthentication {
                    return PasswordAuthentication(username, password)
                }
            })

    try {

        val message = MimeMessage(session)
        message.setFrom(InternetAddress("..."))
        message.setRecipients(
                Message.RecipientType.TO,
                InternetAddress.parse("...")
        )
        message.setSubject("Testing Gmail")
        message.setText("Hello!!")

        Transport.send(message)

        println("Done")

    } catch (e: MessagingException) {
        e.printStackTrace()
    }
}

I get this exception:

javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/?p=BadCredentials n6sm4681584wmn.48 - gsmtp

like image 633
Dmitrii Pisarenko Avatar asked Apr 29 '19 13:04

Dmitrii Pisarenko


People also ask

Does Gmail API use SMTP?

Gmail API uses open authentication (Oauth2), which only lets you request the scope of access you need. SMTP provides full access to the account using client login and password SMTP authentication.

Is Gmail API free?

Gmail API is available for free, but it has certain daily usage limits for API calls. Daily usage: 1 billion API calls per day. Per User Rate Limit: 250 API calls per user per second.


1 Answers

Actually, it is not going with gmail api. It's basic mail server configuration for JMS. Here is what i did at spring;

Configuration file:

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=[GMAIL_USERNAME]
spring.mail.password=[GMAIL_PASSWORD]
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.protocol=smtp

Maven dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

Java code with using JavaMailSender:

@Component
public class EmailService
{
    @Autowired
    protected JavaMailSender emailSender;

    public void sendRegistrationMail(RegistrationForm form) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(form.getUserMail());
        message.setSubject("-- COOL SUBJECT --");
        message.setText("- COOL TEXT -");
        emailSender.send(message);
    }

}

Source : https://www.baeldung.com/spring-email

Note: make sure if you are planing just try this application, email account allowing less secure apps (Less secure app access). Here is the link you can change that : https://myaccount.google.com/lesssecureapps

Edit: I am adding public version of basic mailsender project - https://github.com/ercinakcay/mailsender-public

like image 78
Erçin Akçay Avatar answered Sep 28 '22 05:09

Erçin Akçay