首页 > 解决方案 > 发送直接电子邮件到电子邮件地址 Flutter

问题描述

我希望我的应用程序直接向特定的电子邮件地址发送消息我在 onPress sendEmail('example1@gmail.com', 'example1password') 中使用了此功能

void sendEmail(username, password) async {

      SmtpServer smtpServer = mailgun(username, password);
      final message = new Message()
        ..from = new Address(username, 'My App')
        ..recipients.add('example2@gmail.com')
        ..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
      }
  }

当我按下按钮时,函数调用并显示谷歌对话框 - (我现在不知道为什么这个对话框出现可能是为了注册?) - 在选择帐户后使用我的帐户显示这个

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): 

请帮助我非常渴望解决这个问题。

标签: fluttersmtpgoogle-signinsendmail

解决方案


推荐阅读