Sending email without actually signing in to gmail account from flutter app

Viewed 1412

I want to send user an email on order confirmation. For this I'm using mailer 3.2.1 package. https://pub.dev/packages/mailer

At below line in below code snippet, I'm getting prompt that saying 'gmail' is deprecated and shouldn't be used. Favor gmailUserXoauth2 as username/password is considered a Less-Secure-Apps.

       final smtpServer = gmail(username, password);

After replacing gmail with gmailUserXoauth2 in above line, I get The method 'gmailUserXoauth2' isn't defined

Below is the code snippet

String username = 'xxx@gmail.com';
String password = 'xxx';

       final smtpServer = gmail(username, password);
                      // Create our message.
                      final message = Message()
                        ..from = Address(username, 'Your name')
                        ..recipients.add('destination@example.com')
                        ..ccRecipients.addAll(
                            ['destCc1@example.com', 'destCc2@example.com'])
                        ..bccRecipients.add(Address('bccAddress@example.com'))
                        ..subject =
                            'Test Dart Mailer library ::  :: ${DateTime.now()}'
                        ..text =
                            'This is the plain text.\nThis is line 2 of the text part.'
                        ..html =
                            "<h1>Test</h1>\n<p>Hey! Here's some HTML content</p>";

                      try {
                        final sendReport = await send(message, smtpServer);
                        print('Message sent: ' + sendReport.toString());
                      } on MailerException catch (e) {
                        print('Message not sent.');
                        for (var p in e.problems) {
                          print('Problem: ${p.code}: ${p.msg}');
                        }
                      }

Upon googling this issue I have come across this solution. https://stackoverflow.com/a/58732280/7290043 This solution gives suggest following

  1. Enable "Access for less secure apps" to use Gmail without OAuth 2.0
  2. Sign in with Google account using the google_sign_in package and then send email

But I don't want to do either of these. I dont want to send email after signing in to google with google_sign_in package

What do I do now?

1 Answers

You can follow the guide here. User login is needed for authentication for emails to be sent from the Flutter app. XOAUTH2 is used in the sample code provided in the guide, it redirects users to a login page for authentication.

Related