在HarmonyOS鸿蒙Next应用开发中,需要一个跳转系统邮件,发送邮件的demo

发布于 1周前 作者 sinazl 来自 鸿蒙OS

在HarmonyOS鸿蒙Next应用开发中,需要一个跳转系统邮件,发送邮件的demo 在鸿蒙应用开发中,需要一个跳转系统邮件,发送邮件的demo

4 回复

一个简单的demo,望采纳,谢谢!

最简单的邮件跳转实现

import common from '@ohos.app.ability.common';

function sendEmail(context: common.UIAbilityContext) {
  try {
    let want = {
      action: 'ohos.want.action.send',
      uri: 'mailto:recipient@example.com', // 收件人邮箱
      parameters: {
        'subject': '邮件主题', // 邮件主题
        'body': '邮件正文内容', // 邮件正文
        'cc': 'cc@example.com', // 抄送
        'bcc': 'bcc@example.com' // 密送
      }
    };
    
    context.startAbility(want).then(() => {
      console.log('Jump to email app successfully');
    }).catch((err) => {
      console.error('Failed to jump to email app:', err);
    });
  } catch (err) {
    console.error('Error occurred:', err);
  }
}

在页面中调用

@Entry
@Component
struct EmailPage {
  private context = this.getContext(this) as common.UIAbilityContext;

  build() {
    Column() {
      Button('发送邮件')
        .onClick(() => {
          this.sendEmailWithAttachment();
        })
    }
  }

  private sendEmailWithAttachment() {
    let want = {
      action: 'ohos.want.action.send',
      uri: 'mailto:recipient@example.com',
      parameters: {
        'subject': '测试邮件',
        'body': '这是一封来自鸿蒙应用的测试邮件',
        // 可以添加多个收件人,用逗号分隔
        'uri': 'mailto:recipient1@example.com,recipient2@example.com'
      }
    };
    
    this.context.startAbility(want).catch((err) => {
      console.error('启动邮件应用失败:', err);
      // 可以在这里添加备选方案,如提示用户安装邮件应用
    });
  }
}

权限声明:在module.json5中添加必要的权限

{
  "module": {
    "requestPermissions": [
      {
        "name": "ohos.permission.READ_USER_STORAGE",
        "reason": "需要读取附件文件"
      },
      {
        "name": "ohos.permission.WRITE_USER_STORAGE",
        "reason": "需要创建附件文件"
      }
    ]
  }
}

更多关于在HarmonyOS鸿蒙Next应用开发中,需要一个跳转系统邮件,发送邮件的demo的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next应用开发中,实现跳转系统邮件并发送邮件的功能,可以使用Intent来启动系统邮件应用。以下是一个简单的示例代码:

import featureAbility from '@ohos.ability.featureAbility';
import wantConstant from '@ohos.ability.wantConstant';

let want = {
    action: 'ohos.intent.action.SENDTO',
    uri: 'mailto:recipient@example.com',
    parameters: {
        'ohos.intent.extra.SUBJECT': '邮件主题',
        'ohos.intent.extra.TEXT': '邮件内容'
    },
    flags: wantConstant.Flags.FLAG_ABILITY_NEW_MISSION
};

featureAbility.startAbility(want).then((data) => {
    console.log('邮件应用启动成功');
}).catch((error) => {
    console.error('邮件应用启动失败', error);
});

在这个示例中,want对象定义了启动邮件应用的意图。action指定了操作为发送邮件,uri指定了收件人邮箱地址,parameters中包含了邮件主题和内容。flags设置为FLAG_ABILITY_NEW_MISSION,表示在新的任务栈中启动邮件应用。通过featureAbility.startAbility方法启动邮件应用。

在HarmonyOS鸿蒙Next应用开发中,可以通过Intent实现跳转系统邮件并发送邮件的功能。以下是一个简单的示例代码:

Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"recipient@example.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "邮件主题");
intent.putExtra(Intent.EXTRA_TEXT, "邮件内容");
startActivity(intent);

此代码会启动系统默认的邮件应用,并预填收件人、主题和内容。确保设备上已安装邮件应用。

回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!