'Send Direct Email to Email Address Flutter

I want my app to send message to specific email address direct I used this function inside onPress sendEmail('[email protected]', 'example1password')

void sendEmail(username, password) async {

      SmtpServer smtpServer = mailgun(username, password);
      final message = new Message()
        ..from = new Address(username, 'My App')
        ..recipients.add('[email protected]')
        ..subject = 'New User Register'
        ..text = 'New user want activate code';

      try {
        GoogleSignIn _googleSignIn = GoogleSignIn(
          scopes: <String>[
            'email',
            'https://www.googleapis.com/auth/gmail.send',
          ],
        );

        // Signing in
        final account = await _googleSignIn.signIn();

        if (account == null) {
          print("User didn't authorize");
        }

        final auth = await account!.authentication;

        // Creating SMTP server from the access token
        smtpServer = gmailSaslXoauth2(username ,auth.accessToken!);
      } on PlatformException catch (e) {
        print(e);
      }

      try {
        final sendReport = await send(message, smtpServer);
        print('Message sent: ' + sendReport.toString()); //print if the email is sent
      } on MailerException catch (e) {
        print('Message not sent. \n'+ e.toString()); //print if the email is not sent
        btnState = ButtonState.fail;
        // e.toString() will show why the email is not sending
      }
  }

When I press on button the function call and show google dialog -(I don't now why this dialog appear may be for registring?)- with my accounts after select account it's show this

V/PhoneWindow(27336): DecorView setVisiblity: visibility = 0, Parent = android.view.ViewRootImpl@d289c7, this = DecorView@6897bf4[MainActivity]
I/flutter (27336): PlatformException(network_error, com.google.android.gms.common.api.ApiException: 7: , null, null)
D/Surface (27336): Surface::disconnect(this=0x7251f0d000,api=1)
D/Surface (27336): Surface::connect(this=0x7251f0d000,api=1)
D/mali_winsys(27336): EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, EGLBoolean) returns 0x3000
D/Surface (27336): Surface::setBufferCount(this=0x7251f0d000,bufferCount=3)
V/PhoneWindow(27336): DecorView setVisiblity: visibility = 4, Parent = android.view.ViewRootImpl@672a92b, this = DecorView@3b6579c[SignInHubActivity]
D/Surface (27336): Surface::disconnect(this=0x72521d0000,api=1)
D/View    (27336): [Warning] assignParent to null: this = DecorView@3b6579c[SignInHubActivity]
E/flutter (27336): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: SocketException: Failed host lookup: 'smtp.mailgun.org' (OS Error: No address associated with hostname, errno = 7)
E/flutter (27336): #0      _NativeSocket.startConnect (dart:io-patch/socket_patch.dart:677:35)
E/flutter (27336): #1      _NativeSocket.connect (dart:io-patch/socket_patch.dart:914:12)
E/flutter (27336): #2      _RawSocket.connect (dart:io-patch/socket_patch.dart:1668:26)
E/flutter (27336): #3      RawSocket.connect (dart:io-patch/socket_patch.dart:21:23)
E/flutter (27336): #4      Socket._connect (dart:io-patch/socket_patch.dart:1883:22)
E/flutter (27336): #5      Socket.connect (dart:io/socket.dart:724:21)
E/flutter (27336): #6      Connection.connect (package:mailer/src/smtp/connection.dart:135:24)
E/flutter (27336): #7      connect (package:mailer/src/smtp/smtp_client.dart:116:13)
E/flutter (27336): #8      send (package:mailer/src/smtp/mail_sender.dart:94:26)
E/flutter (27336): #9      Manage.sendEmail (package:accounting/Custom/Manage/manage.dart:147:34)
E/flutter (27336): <asynchronous suspension>
E/flutter (27336): 

PLEASE HELP I am so DESPERATE for solving this.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source