flutter 邮件发送

1、安装
/// pubspec.yaml文件添加依赖,并在执行flutter pub get命令
dependencies
flutter_email_sender: ^5.2.0

2、使用

email_send_util.dart

import 'package:flutter_mailer/flutter_mailer.dart';
import 'package:smartphone/util/logger_util.dart';

/// メール送信管理クラス
class EmailSendUtil {

  /// @description メールで送る
  /// @param: mailOptions メールの内容
  /// @return Future<void>
  Future<void> sendEmail(MailOptions mailOptions) async {
    final MailerResponse response = await FlutterMailer.send(mailOptions);
    String platformResponse = "";
    switch (response) {
      case MailerResponse.saved:

        /// ios only
        platformResponse = 'mail was saved to draft';
        break;
      case MailerResponse.sent:

        /// ios only
        platformResponse = 'mail was sent';
        break;
      case MailerResponse.cancelled:

        /// ios only
        platformResponse = 'mail was cancelled';
        break;
      case MailerResponse.android:
        platformResponse = 'intent was successful';
        break;
      default:
        platformResponse = 'unknown';
        break;
    }
    logWTF(platformResponse);
  }
}

mailerOptions.dart

part of flutter_mailer;

class MailOptions {
  static const String GMAIL = 'com.google.android.gm';

  /// Email Subject field
  final String subject;

  /// List of primary Recipients for the email
  final List<String> recipients;

  /// List of Carbon copy Recipents
  final List<String> ccRecipients;

  /// List of Blind carbon copy Recipents
  final List<String> bccRecipients;

  /// Email body field
  final String body;

  final bool isHTML;

  /// List of attachment file path
  final List<String> attachments;

  /// define a specific Email App to open
  ///
  /// this can be used to step over App choser sheet when sending an email with attachments.
  ///
  /// for Gmail on android `com.google.android.gm` or by `MailOptions.GMAIL`
  ///
  /// _android only_
  final String? appSchema;
  // ignore: sort_constructors_first
  MailOptions({
    this.subject = '',
    this.recipients = const <String>[],
    this.ccRecipients = const <String>[],
    this.bccRecipients = const <String>[],
    this.body = '',
    this.attachments = const <String>[],
    this.isHTML = false,
    this.appSchema,
  });

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> map = <String, dynamic>{
      'subject': subject,
      'body': body,
      'recipients': recipients,
      'ccRecipients': ccRecipients,
      'bccRecipients': bccRecipients,
      'isHTML': isHTML,
      'appSchema': appSchema,
    };
    if (appSchema != null && appSchema!.isNotEmpty) {
      map['appSchema'] = appSchema;
    }

    if (attachments.isNotEmpty) {
      final List<String> paths = <String>[];
      for (String path in attachments) {
        if (path.isNotEmpty) {
          paths.add(path);
        }
      }

      map['attachments'] = paths;
    }

    return map;
  }
}

3.调用

emailSendUtil.sendEmail(MailOptions(
  body: '',
  subject: '',
  recipients: [],
  isHTML: true,
  bccRecipients: [],
  ccRecipients: [],
  attachments: attachmentsTemp,
));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值