我不想撰写电子邮件,我只想在react-native应用程序中能够启动用户设备(iOS和Android)上的主要电子邮件应用程序。
场景:我将在注册时向用户发送验证电子邮件。
我不想撰写电子邮件,我只想在react-native应用程序中能够启动用户设备(iOS和Android)上的主要电子邮件应用程序。
场景:我将在注册时向用户发送验证电子邮件。
<Button onPress={() => Linking.openURL('mailto:support@example.com') }
title="support@example.com" />
<Button onPress={() => Linking.openURL('mailto:support@example.com?subject=SendMail&body=Description') }
title="support@example.com" />
<Button onPress={() => Linking.openURL('https://www.google.co.in/') }
title="www.google.co.in" />
##别忘了导入
import { Linking } from 'react-native'
注意:在iOS模拟器中不受支持,因此您必须在设备上进行测试。
openURL('mailto:...')
打开的是一封新邮件的编辑页面,而不是已有的邮件客户端。Jasan明确表示不希望这样做。请注意保留原意并使文本易于理解。 - miahconst subject = encodeURI(strings.feedbackSubject);
- Simon K. Gergesmailto:somethingemail@gmail.com?subject=${encodeURIComponent("This is a subject")}
https://github.com/facebook/react-native/issues/36680#issuecomment-1488093034 - undefined很不幸,之后的回答都不正确。
我不想写一封电子邮件。我只想能够启动主要的电子邮件应用程序
我希望有相同的行为:
打开电子邮件应用
的按钮跟Slack Onboarding中的魔法链接差不多
我发现了一个使用库react-native-email-link的解决方案。 您可以从React Native中打开电子邮件客户端(用于“魔术链接”功能)。
mail.app
。Linking.openURL('message://')
,它会打开 Mail.app。上述提到的库可以处理跨移动操作系统的多个邮件应用程序。 - filipeLinking.openURL('mailto:example@gmail.com?subject=example&body=example')
Expo.io 纯 JS/TypeScript 解决方案:
import * as IntentLauncher from 'expo-intent-launcher';
// ...
public openMailClientIOS() {
Linking.canOpenURL('message:0')
.then(supported => {
if (!supported) {
console.log('Cant handle url')
} else {
return Linking.openURL('message:0')
.catch(this.handleOpenMailClientErrors)
}
})
.catch(this.handleOpenMailClientErrors)
}
public openMailClientAndroid() {
const activityAction = 'android.intent.action.MAIN'; // Intent.ACTION_MAIN
const intentParams: IntentLauncher.IntentLauncherParams = {
flags: 268435456, // Intent.FLAG_ACTIVITY_NEW_TASK
category: 'android.intent.category.APP_EMAIL' // Intent.CATEGORY_APP_EMAIL
};
IntentLauncher.startActivityAsync(activityAction, intentParams)
.catch(this.handleOpenMailClientErrors);
}
iOS邮件中可以正常工作,Android中也可以。
Android Intent文档: https://developer.android.com/reference/android/content/Intent#ACTION_MAIN
Expo IntentLauncher文档: https://docs.expo.io/versions/latest/sdk/intent-launcher/
在iOS上打开电子邮件应用程序:
Linking.canOpenURL('message:')
.then(supported => {
if (!supported) {
console.log('Cant handle url')
} else {
return Linking.openURL('message:')
}
})
.catch(err => {
console.error('An error occurred', err)
})
您可以使用此方法打开任何电子邮件客户端,并发送带有某些数据的电子邮件。
export const sendEmailViaEmailApp = (toMailId, subject, body) => {
if (!isUndefined(toMailId)) {
let link = `mailto:${toMailId}`;
if (!isUndefined(subject)) {
link = `${link}?subject=${subject}`;
}
if (isUndefined(subject)) {
link = `${link}?body=${body}`;
} else {
link = `${link}&body=${body}`;
}
Linking.canOpenURL(link)
.then(supported => {
if (supported) {
// 'mailto:support@example.com?subject=Billing Query&body=Description'
Linking.openURL(link);
}
})
.catch(err => console.error('An error occurred', err));
} else {
console.log('sendEmailViaEmailApp -----> ', 'mail link is undefined');
}
};
将此方法放入工具类中,并在任何您想要使用的地方使用此方法。
import { View,Linking,Text, Image,TouchableOpacity } from 'react-native';
const emailId= 'care@flipkart.com'
const onPressEmailClick = (email) => {
Linking.openURL('mailto:'+email)
// Linking.openURL('mailto:Care@amazon.com')
}
<View style={{ flexDirection: "row", alignItems: "center", justifyContent: "center" }} >
<Text style={{ textAlign: "center", marginTop: 15, color: "black" }} >
{"For any query mail us "}
</Text>
<TouchableOpacity
onPress={() => onPressEmailClick(emailId)} >
<Text style={{ textAlign: "center", marginTop: 15, color: "black", textDecorationLine: 'underline' }} >
{emailId}
</Text>
</TouchableOpacity>
googlegmail://
打开Gmail应用程序,这是Gmail的方案。第一次打开Gmail时,它会弹出一个警告,显示“想要打开Gmail”,用户可以点击确定或取消。这种行为只会发生一次。我找到了一种方法来打开邮件账户,而不是撰写邮件。由于需要物理iOS设备,我只在安卓(三星S9 +)上进行了测试,并且它有效。
import { openInbox } from "react-native-email-link";
import * as IntentLauncher from 'expo-intent-launcher';
const openMail = async () => {
if (Platform.OS === "ios") {
try {
await openInbox({ title: "Open mail app" });
} catch (error) {
console.error(`OpenEmailbox > iOS Error > ${error}`);
}
}
if (Platform.OS === "android") {
const activityAction = "android.intent.action.MAIN";
const intentParams = {
category: "android.intent.category.APP_EMAIL",
};
IntentLauncher.startActivityAsync(activityAction, intentParams);
}
}
import { Linking } from 'react-native'
React Native Open Mail
<TouchableOpacity onPress={() => Linking.openURL('mailto:support@example.com')}>
<Text>support@example.com</Text>
</TouchableOpacity>
React Native Open Mail With Subject & Body
<TouchableOpacity onPress={() => Linking.openURL('mailto:support@example.com?subject=sendmail&body=details')}>
<Text>support@example.com</Text>
</TouchableOpacity>
this will only work in real device. not working in iOS simulator.