Flutter OHOS flutter_mailer(邮件工具)

173 阅读1分钟

HarmonyOS next之flutter_mailer

通过设备电子邮件客户端共享电子邮件内容 - 支持多个附件 用于移动开发中电子邮件字段跨应用程序数据共享的简单快捷的插件。

开始使用

添加到您的 pubspec 依赖项,如下所示:

dependencies:
  flutter:
sdk: flutter
  flutter_mailer: ^2.0.0

###实例化邮件选项如下:

发送电子邮件

import 'package:flutter_mailer/flutter_mailer.dart';
​
...
...
​
final MailOptions mailOptions = MailOptions(
  body: 'a long body for the email <br> with a subset of HTML',
  subject: 'the Email Subject',
  recipients: ['example@example.com'],
  isHTML: true,
  bccRecipients: ['other@example.com'],
  ccRecipients: ['third@example.com'],
  attachments: [ 'path/to/image.png', ],
);
​
final MailerResponse response = await FlutterMailer.send(mailOptions);
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;
}
[Android] 检查应用程序是否已安装。

如果要将意图发送到特定应用程序,请使用 full。 在 [IOS] 上返回 false

const GMAIL_SCHEMA = 'com.google.android.gm';
​
final bool gmailinstalled =  await FlutterMailer.isAppInstalled(GMAIL_SCHEMA);
​
if(gmailinstalled) {
  final MailOptions mailOptions = MailOptions(
body: 'a long body for the email <br> with a subset of HTML',
subject: 'the Email Subject',
recipients: ['example@example.com'],
isHTML: true,
bccRecipients: ['other@example.com'],
ccRecipients: ['third@example.com'],
attachments: [ 'path/to/image.png', ],
appSchema: GMAIL_SCHEMA,
  );
  await FlutterMailer.send(mailOptions);
}
[IOS] 检查设备是否有发送电子邮件的功能
  final bool canSend = await FlutterMailer.canSendMail();
​
  if(!canSend && Platform.isIos) {
final url = 'mailto:$recipient?body=$body&subject=$subject';
if (await canLaunch(url)) {
  await launch(url);
} else {
  throw 'Could not launch $url';
}
  }
}
鸿蒙OS代码

校验应用是否已经安装

  /**
   * 校验应用是否已经安装
   *
   * @param bundleName 包名
   * @return 应用是否已经安装
   */
  private async hasInstalled(bundleName: string): Promise<boolean> {
if (!bundleName) {
  Log.w(TAG, 'query package fail, bundleName empty');
  return false
}
let hasInstalled: boolean = false
packageBundle.hasInstalled({
  bundleName: bundleName,
  success: (data) => {
Log.i(TAG, 'package install result: ' + data?.result);
hasInstalled = data?.result || false
  },
  fail: (data: string, code) => {
Log.w(TAG, 'package install get result fail: ' + code + ', data: ' + data);
hasInstalled = false
  },
});
return hasInstalled
  }

发送邮件

function sendEmail() {
let emailIntent = new ability.Intent();
​
// 设置动作
emailIntent.setAction('ohos.action.SEND');
​
// 设置数据类型
emailIntent.setData('mailto:');
​
// 设置邮件接收者
emailIntent.setParameter('android.intent.extra.EMAIL', ['recipient@example.com']);
​
// 设置邮件主题
emailIntent.setParameter('android.intent.extra.SUBJECT', 'Test Email Subject');
​
// 设置邮件正文
emailIntent.setParameter('android.intent.extra.TEXT', 'This is a test email body.');
​
// 启动邮件客户端
ability.startAbility({
want: emailIntent
}).then(() => {
console.log('Email client started successfully.');
}).catch((error) => {
console.error('Failed to start email client:', error);
});
}