是 React Native 生态中用于调用设备原生邮件客户端的轻量级库,核心功能是快速唤起手机自带的邮件应用(如 iOS 的 Mail、Android 的 Gmail / 系统邮件),并自动填充收件人、主题、正文、附件等信息,简化应用内发邮件的流程
Document Template: v0.4.2
react-native-mail
This project is based on react-native-mail.
This third-party library has been migrated to GitCode and is now available for direct download from npm. The new package name is: @react-native-ohos/react-native-mail. The version correspondence details are as follows:
| Name | Version(Npm Address) | Release Information | Supported RN Version | Supported Autolink | Compile API Version | Community Baseline Version | Source code address |
|---|---|---|---|---|---|---|---|
| @react-native-ohos/react-native-mail | ~6.3.0 (In development) | GitCode Releases | 0.82.* | Yes | API12+ | 6.1.1 | master |
| @react-native-ohos/react-native-mail | ~6.2.0 | GitCode Releases | 0.77.* | No | API12+ | 6.1.1 | br_rnoh0.77 |
| @react-native-ohos/react-native-mail | ~6.1.2 | GitCode Releases | 0.72.* | Yes | API12+ | 6.1.1 | br_rnoh0.72 |
| @react-native-oh-tpl/react-native-mail | <= 6.1.1-0.0.1@deprecated | GitHub Releases(deprecated) | 0.72.* | No | API12+ | 6.1.1 | sig |
Introduction
react-native-mail is a utility library used to open the email app and compose a new email.
Installation
Go to the project directory and execute the following instruction:
npm
npm install @react-native-ohos/react-native-mail
yarn
yarn add @react-native-ohos/react-native-mail
Link
| Version | Supported Autolink | Supported RN Version |
|---|---|---|
| ~ 6.3.0 | Yes | 0.82 |
Projects using AutoLink need to be configured according to this document. AutoLink framework guide: https://gitcode.com/CPF-RN/ohos_react_native/blob/main/docs/en/02-development/02-development-guide/autolinking.md
If the version you are using supports Autolink and the project has integrated Autolink, you can skip the ManualLink configuration.
ManualLink: This step provides guidance for manually configuring native dependencies.
Open the harmony directory of the HarmonyOS project in DevEco Studio.
1. Overrides RN SDK
To ensure the project relies on the same version of the RN SDK, add an overrides field in the project's root oh-package.json5 file, specifying the RN SDK version to be used. The replacement version can be a specific version number, a semver range, or a locally available HAR package or source directory.
For more information about this field, see the official documentation.
{
"overrides": {
"@rnoh/react-native-openharmony": "~0.82.1" // ohpm version
// "@rnoh/react-native-openharmony" : "./react_native_openharmony.har" // a locally available HAR package
// "@rnoh/react-native-openharmony" : "./react_native_openharmony" // source code directory
}
}
2. Introducing Native Code
Currently, two methods are available:
- Use the HAR file;
- Directly link to the source code.
Method 1 (recommended): Use the HAR file.
The HAR file is stored in the `harmony` directory in the installation path of the third-party library.
Open entry/oh-package.json5 and add the following dependency:
"dependencies": {
"@react-native-ohos/react-native-mail": "file:../../node_modules/@react-native-ohos/react-native-mail/harmony/mail.har"
}
Click the sync button in the upper right corner.
Alternatively, run the following command on the terminal:
cd entry
ohpm install
Method 2: Directly link to the source code.
For details, see [Directly Linking Source Code](https://gitcode.com/CPF-RN/usage-docs/blob/master/en/link-source-code.md).
3. Configuring CMakeLists and Introducing RNMailPackage
Open entry/src/main/cpp/CMakeLists.txt and add:
project(rnapp)
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_SKIP_BUILD_RPATH TRUE)
set(RNOH_APP_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set(NODE_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../node_modules")
+ set(OH_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../oh_modules")
set(RNOH_CPP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../react-native-harmony/harmony/cpp")
set(LOG_VERBOSITY_LEVEL 1)
set(CMAKE_ASM_FLAGS "-Wno-error=unused-command-line-argument -Qunused-arguments")
set(CMAKE_CXX_FLAGS "-fstack-protector-strong -Wl,-z,relro,-z,now,-z,noexecstack -s -fPIE -pie")
set(WITH_HITRACE_SYSTRACE 1) # for other CMakeLists.txt files to use
add_compile_definitions(WITH_HITRACE_SYSTRACE)
add_subdirectory("${RNOH_CPP_DIR}" ./rn)
# RNOH_BEGIN: manual_package_linking_1
add_subdirectory("../../../../sample_package/src/main/cpp" ./sample-package)
+ add_subdirectory("${OH_MODULES}/@react-native-ohos/react-native-mail/src/main/cpp" ./mail)
# RNOH_END: manual_package_linking_1
file(GLOB GENERATED_CPP_FILES "./generated/*.cpp")
add_library(rnoh_app SHARED
${GENERATED_CPP_FILES}
"./PackageProvider.cpp"
"${RNOH_CPP_DIR}/RNOHAppNapiBridge.cpp"
)
target_link_libraries(rnoh_app PUBLIC rnoh)
# RNOH_BEGIN: manual_package_linking_2
target_link_libraries(rnoh_app PUBLIC rnoh_sample_package)
+ target_link_libraries(rnoh_app PUBLIC rnoh_mail)
# RNOH_END: manual_package_linking_2
Open entry/src/main/cpp/PackageProvider.cpp and add:
#include "RNOH/PackageProvider.h"
#include "SamplePackage.h"
+ #include "MailPackage.h"
using namespace rnoh;
std::vector<std::shared_ptr<Package>> PackageProvider::getPackages(Package::Context ctx) {
return {
std::make_shared<SamplePackage>(ctx),
+ std::make_shared<MailPackage>(ctx)
};
}
4. Introducing RNMailPackage to ArkTS
Open entry/src/main/ets/RNPackagesFactory.ts and add:
...
+ import {RNMailPackage} from '@react-native-ohos/react-native-mail/ts';
export function createRNPackages(ctx: RNPackageContext): RNPackage[] {
return [
+ new RNMailPackage(ctx)
];
}
Running
Click the sync button in the upper right corner.
Alternatively, run the following command on the terminal:
cd entry
ohpm install
Then build and run the project.
Constraints
Compatibility
The content of this document has been verified based on the following versions:
- RNOH: 0.82.7; SDK: HarmonyOS 6.0.1 Release SDK; IDE: DevEco Studio 6.0.1.260; ROM: 6.0.0.130 SP15;
Usage Example
The following code shows a basic usage scenario:
The imported library name remains unchanged.
import React from 'react';
import { View, Alert, Button, StyleSheet } from 'react-native';
import Mailer from 'react-native-mail';
export default function App() {
const handleEmail = () => {
Mailer.mail(
{
subject: 'need help',
recipients: ['support@example.com'],
ccRecipients: ['supportCC@example.com'],
bccRecipients: ['supportBCC@example.com'],
body: '<b>A Bold Body</b>',
isHTML: true,
attachments: [
{
// Specify either `path` or `uri` to indicate where to find the file data.
path: '', // /data/storage/el2/base/haps/entry/files/11.png
uri: '', // file://<packageName>/data/storage/el2/base/haps/entry/files/11.png
// Specify either `type` or `mimeType` to indicate the type of data.
type: '', // Mime Type: jpg, png, doc, ppt, html, pdf, csv
mimeType: '', // Use only if you want a custom type
name: '', // Optional: custom filename for the attachment
},
],
},
(error, event) => {
if (error) {
console.error('Send mail failed:', error);
} else {
console.error('Send mail success:', event);
}
}
);
};
return (
<View style={styles.container}>
<Button onPress={handleEmail} title="Email Me" color="#841584" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F1F3F5',
},
});
How to Use
Send an email
import Mailer from 'react-native-mail';
Mailer.mail({
subject: 'need help', // Email subject
recipients: ['support@example.com'], // Recipients
ccRecipients: ['supportCC@example.com'], // CC recipients (optional)
bccRecipients: ['supportBCC@example.com'], // BCC recipients (optional)
body: '<b>A Bold Body</b>', // Email body
}, (error, event) => {
// The result is returned via the callback: `error` is the error message and `event` is the event name
if (error) {
console.error('Send mail failed:', error);
}
});
Handle the callback result
Mailer.mail({ subject: 'need help', recipients: ['support@example.com'] }, (error, event) => {
if (error) {
console.error('Send mail failed:', error);
} else {
console.error('Send mail success:', event);
}
});
Send an HTML email with attachments
Mailer.mail({
subject: 'Mail with attachment',
recipients: ['support@example.com'],
body: '<b>A Bold Body</b>',
isHTML: true, // Not supported on HarmonyOS yet
attachments: [
{
path: '', // Either `path` or `uri`: absolute path of the file
uri: '', // Either `path` or `uri`: URI of the file
type: '', // Either `type` or `mimeType`: jpg, png, doc, ppt, html, pdf, csv
mimeType: '', // Either `type` or `mimeType`: custom MIME type
name: '', // Optional: custom filename for the attachment
},
],
}, (error, event) => {
console.log(error, event);
});
`isHTML`, `attachments`, and `customChooserTitle` are not supported on HarmonyOS yet. See [Known Issues](#known-issues).
Available APIs
The **Platform** column indicates the platforms supported by the original third-party library.
If **OpenHarmony Platform Support** is **yes**, the OpenHarmony platform supports this property; **no** means not supported; **partially** means partially supported. Usage is consistent across platforms, and the effect aligns with iOS or Android.
API
| Name | Description | Type | Required | Platform | HarmonyOS Support |
|---|---|---|---|---|---|
| Launches the email app and passes information such as recipients, sender, and CC/BCC recipients to it, and returns error information through the callback function. | Function(options,callback) | yes | all | yes |
Parameters
Options
| Name | Description | Type | Required | Platform | HarmonyOS Support |
|---|---|---|---|---|---|
| subject | Email subject | string | no | all | yes |
| recipients | Recipients | string[] | no | all | yes |
| ccRecipients | CC recipients | string[] | no | all | yes |
| bccRecipients | BCC recipients | string[] | no | all | yes |
| body | Email body | string | no | all | yes |
| customChooserTitle | Custom chooser title | string | no | android | no |
| isHTML | Whether the email body is HTML | boolean | no | all | no |
| attachments | Attachment information | object[] | no | all | no |
callback
The result callback function has the signature (error: string, event?: string) => void, with parameters described below:
| Name | Description | Type | Required | Platform | HarmonyOS Support |
|---|---|---|---|---|---|
| error | Error message | string | yes | all | yes |
| event | Event name | string | no | all | yes |
Known Issues
Other
Currently, it jumps directly to the device's built-in email app through startAbility, so customChooserTitle is not supported.
Directory Structure
/rntpc_react-native-mail # Project root directory
├── harmony # HarmonyOS adaptation code
│ ├── mail.har # HAR package
│ └── mail # Core HarmonyOS adaptation code
│ ├── index.ets # Adaptation code entry
│ ├── ts.ets # TurboModule ArkTS declaration entry
│ └── src/main
│ ├── cpp # C++ adaptation code (MailPackage and codegen-generated code)
│ ├── ets # ArkTS adaptation code (RNMailPackage, RNMailTurboModule, etc.)
│ └── module.json5 # Module configuration
├── src # React Native code
│ └── NativeRNMail.harmonys.ts # TurboModule Spec definition
├── example # Example project
├── index.js # Entry file
├── index.d.ts # Type definitions
├── README.md # Chinese installation and usage guide
├── README_en.md # English installation and usage guide
└── package.json # Package configuration
How to Contribute
If you find any problem when using react-native-share, submit an issue or a PR.
License
This project is licensed under The MIT License (MIT). Feel free to enjoy and contribute to open source.
Introduction
是 React Native 生态中用于调用设备原生邮件客户端的轻量级库,核心功能是快速唤起手机自带的邮件应用(如 iOS 的 Mail、Android 的 Gmail / 系统邮件),并自动填充收件人、主题、正文、附件等信息,简化应用内发邮件的流程
Customize your domain