Search code examples
react-nativesmtp-auth

Google blocks smtp in react-native


I am having trouble with google smtp for sending emails from my app.

The app works fine in my mobile and I am able to send emails without any issue.

But When I publish the app and people start using it, I get a security email from google telling me that it has blocked a login try.

I have even enabled less secure login.

Is there any other settings I should enable?

Here is my code for sending emails

import RNSmtpMailer from 'react-native-smtp-mailer'

async sendEmail(email: string, htmlBody: string, subject: string) {
  try {
    var settings = await this.getAppSettings();
    if (!settings)
      throw "Could not find the smtp settings"
    var success = await RNSmtpMailer.sendMail({
      mailhost: settings.smtp,
      port: settings.port,
      ssl: true, // optional. if false, then TLS is enabled. Its true by default in android. In iOS TLS/SSL is determined automatically, and this field doesn't affect anything
      username: settings.email,
      password: settings.password,
      fromName: "NovelManager", // optional
      replyTo: undefined, // optional
      recipients: email,
      bcc: [], // optional
      subject: subject,
      htmlBody: htmlBody,
      attachmentPaths: [], // optional
      attachmentNames: [], // required in android, these are renames of original files. in ios filenames will be same as specified in path. In a ios-only application, no need to define it
    });
    return true;
  } catch (error) {
    return false;
  }
}

and here is the smtp settings

{
  smtp: "smtp.gmail.com",
  port: "465",
  email: "[email protected]", // not the real email
  password: "test"
}

Solution

  • Ok at last I found a solution, And that is using app password instead of simple password.

    I am posting the solution here incase anyone is intressted.