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, ));