You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native(Android):SMTP发送带附件邮件的替代方案咨询

React Native SMTP Email with Attachments: Alternatives to react-native-mailcore

Got it, let's tackle this—since react-native-mailcore isn't cutting it for attachments, here are solid SMTP-based options that support file attachments in React Native:

This is the go-to library for SMTP email with attachments in vanilla React Native. It supports multiple attachment types (PDFs, images, docs, etc.) and works reliably on both iOS and Android.

Setup & Example

First install the package:

npm install react-native-smtp-mailer --save
# Or for yarn users:
yarn add react-native-smtp-mailer

Then integrate it into your component (you'll need valid SMTP credentials from your email provider, like Gmail, Outlook, or a self-hosted server):

import RNSmtpMailer from "react-native-smtp-mailer";

const sendEmailWithAttachment = async () => {
  try {
    await RNSmtpMailer.sendMail({
      mailhost: "smtp.gmail.com",
      port: 465,
      ssl: true, // Use SSL for port 465; set to false for TLS (port 587)
      username: "your-email@gmail.com",
      password: "your-app-specific-password", // For Gmail, use an app password if 2FA is enabled
      fromName: "Your Name",
      recipients: "recipient@example.com",
      subject: "Test Email with Attachment",
      body: "Check out the attached file below!",
      attachmentPaths: [
        // Local file paths (e.g., from Camera Roll, device storage)
        "/storage/emulated/0/Download/sample.pdf",
        "file:///var/mobile/Containers/Data/Application/.../vacation-photo.jpg"
      ],
      attachmentNames: ["project-docs.pdf", "summer-vacation.jpg"], // Optional: rename attachments
    });
    console.log("Email sent successfully!");
  } catch (error) {
    console.error("Failed to send email:", error);
  }
};

Key Notes

  • For Gmail, enable app-specific passwords (instead of less secure apps) if you have 2FA enabled
  • On Android, add file read permissions to your AndroidManifest.xml
  • Supports both SSL and TLS encryption for secure connections

2. expo-smtp (For Expo Managed Projects)

If you're building with Expo, this official Expo package is the simplest way to send SMTP emails with attachments. It integrates seamlessly with Expo's file system and media picker APIs.

Setup & Example

Install the package (no native linking required for Expo managed projects):

npx expo install expo-smtp

Example using an attachment picked via Expo's Document Picker:

import * as SMTP from 'expo-smtp';
import * as DocumentPicker from 'expo-document-picker';

const sendExpoEmail = async () => {
  // Pick a file from the device
  const result = await DocumentPicker.getDocumentAsync({ type: '*/*' });
  if (result.canceled) return;

  try {
    await SMTP.sendMailAsync({
      host: 'smtp.outlook.com',
      port: 587,
      secure: false, // Use TLS for port 587
      username: 'your-email@outlook.com',
      password: 'your-password',
      from: 'your-email@outlook.com',
      to: 'recipient@example.com',
      subject: 'Expo Email with Attachment',
      body: 'Here\'s the document you requested.',
      attachments: [
        {
          uri: result.assets[0].uri,
          name: result.assets[0].name,
          mimeType: result.assets[0].mimeType,
        },
      ],
    });
    alert('Email sent successfully!');
  } catch (error) {
    console.error('Error sending email:', error);
  }
};

Key Notes

  • Only works with Expo managed or bare projects using Expo modules
  • Pair with expo-image-picker or expo-document-picker to easily get attachment URIs
  • Supports all major email providers (Gmail, Outlook, Yahoo, etc.)

3. Custom Native Module (Advanced, Full Control)

If you need complete flexibility (or the above libraries don't fit your use case), build a custom React Native module using native SMTP libraries:

  • iOS: Use MailCore2 (the same foundation as react-native-mailcore, but you can implement attachment support directly in Swift/Objective-C)
  • Android: Use the JavaMail library (add dependencies to your Android project and wrap SMTP logic with attachment handling)

Basic Workflow

  1. Create native modules for iOS and Android that handle SMTP connections and email sending with attachments
  2. Expose a JavaScript API from the modules that your React Native code can call
  3. Pass file paths/URIs from React Native to the native module, which reads the file and attaches it to the email

This approach requires native development skills, but it lets you customize every part of the SMTP process (e.g., custom encryption, advanced attachment validation).


内容的提问来源于stack exchange,提问作者Andrew6299E

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:29:58