【问题标题】:How to Sending sms and email using Dart (Flutter)?如何使用 Dart (Flutter) 发送短信和电子邮件?
【发布时间】:2021-10-21 00:19:15
【问题描述】:

我正在尝试在发送电子邮件和短信的颤振中构建和应用程序。目的是重置密码(忘记密码)。但我找不到合适的包。一旦用户点击重设密码,电子邮件或短信应该在没有用户交互的情况下发送。短信和电子邮件将从他们的手机发送到相同的号码或电子邮件。

还有其他建议吗?

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    对于电子邮件,您可以使用此邮件包 => https://pub.dev/packages/mailer

    更新: 如果此功能不起作用,请降低您的邮件程序包版本。

    例子:

    Future<void> sendMail(String name, String otp, String recipientEmail) async {
      // ignore: deprecated_member_use
      SmtpServer smtpServer = gmail('yourEmail', 'Password');
      Message message = Message()
        ..from = address('yourEmail', 'UserName')
        ..recipients.add(recipientEmail)
        ..subject = 'Any Text.'
        ..html = "Any Text.";
    
      try {
        final sendReport = await send(message, smtpServer);
        PrintLog.printMessage('Message sent: ' + sendReport.toString());
      } on MailerException catch (e) {
        PrintLog.printMessage('Message not sent. ' + e.toString());
        for (var p in e.problems) {
          PrintLog.printMessage('Problem: ${p.code}: ${p.msg}');
        }
      }
      var connection = PersistentConnection(smtpServer);
      await connection.send(message);
      await connection.close();
    }
    

    对于 SMS,您可以使用 Url Launcher Package => https://pub.dev/packages/url_launcher

    例子:

      _textMe() async {
    if (Platform.isAndroid) {
      const uri = 'sms:YourNumber?body=hello%20there';
      await launch(uri);
      } else if (Platform.isIOS) {
      // iOS
      const uri = 'sms:YourNumber&body=hello%20there';
      await launch(uri);
      }
    }
    

    【讨论】:

    • 网址启动器也可以用来发送电子邮件吗?
    • 是的,但如果您想以编程方式发送邮件,我会打开 Gmail 应用程序发送邮件,然后使用邮件程序包。如果这个答案可能对您有所帮助,请勾选正确答案。快乐飘飘
    猜你喜欢
    • 2019-02-05
    • 1970-01-01
    • 2011-02-01
    • 1970-01-01
    • 2017-02-03
    • 2014-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多