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

使用Flutter发送邮件时出现Mailer插件报错问题

Hey there! Let's tackle this mailer issue step by step. First off, I notice your code snippet is incomplete (the recipients line cuts off), but let's walk through the most common pitfalls with the mailer2 package and how to fix them.

1. First: Fix Gmail SMTP Authentication

Gmail has strict security rules that are the #1 cause of errors here:

  • If you have 2FA enabled on your Gmail account (which you should!), you can't use your regular password. You need to create an App Password specifically for this app. Go to your Google Account > Security > App Passwords (only visible if 2FA is on) to generate one.
  • If you're using a Google Workspace account, make sure your admin hasn't restricted SMTP access for your account.
2. Complete & Correct Your Code

Your current code is missing key parts. Here's the full, corrected version for mailer2:

import 'package:flutter/material.dart';
import 'package:mailer2/mailer.dart';

void main() {
  // Configure Gmail SMTP options
  var options = GmailSmtpOptions()
    ..username = 'mer*******@gmail.com'
    ..password = 'your-app-password-here'; // Use the App Password, not your regular password

  // Initialize transport
  var emailTransport = SmtpTransport(options);

  // Create a complete envelope (email)
  var envelope = Envelope()
    ..from = 'mer*******@gmail.com'
    ..recipients.add('recipient@example.com') // Add your recipient's email
    ..subject = 'Test Email from Flutter'
    ..text = 'Hello! This is a test email sent via the mailer2 package.';

  // Send the email with error handling
  emailTransport.send(envelope)
      .then((success) => print('Email sent successfully!'))
      .catchError((error) => print('Error sending email: $error'));
}
3. Troubleshoot Common Errors

If you still get an error, check these common issues:

  • Incorrect credentials: Double-check your Gmail address and App Password. Typos here are super common.
  • SMTP port issues: GmailSmtpOptions should default to port 465 (SSL), but if that fails, try port 587 (TLS) by adding ..port = 587 to your options.
  • Network restrictions: Make sure your device has an active internet connection, and no firewall/proxy is blocking SMTP traffic to smtp.gmail.com.
  • Outdated package: mailer2 is a fork that hasn't been updated in years. Consider migrating to the official, actively maintained mailer package (see below for how).

Since mailer2 is unmaintained, switching to the official mailer package will give you better stability and support. Here's a quick example:

  1. Add dependencies to pubspec.yaml:
    dependencies:
      mailer: ^6.0.1 # Use the latest version from pub.dev
      mailer_smtp: ^2.0.0
    
  2. Updated code:
    import 'package:mailer/mailer.dart';
    import 'package:mailer/smtp_server/gmail.dart';
    
    void main() async {
      final username = 'mer*******@gmail.com';
      final appPassword = 'your-app-password-here';
    
      final smtpServer = gmail(username, appPassword);
    
      // Build your email message
      final message = Message()
        ..from = Address(username, 'Your App Name')
        ..recipients.add('recipient@example.com')
        ..subject = 'Test Email from Flutter'
        ..text = 'Hello from the official mailer package!';
    
      // Send with proper error handling
      try {
        final sendReport = await send(message, smtpServer);
        print('Email sent: ${sendReport.toString()}');
      } on MailerException catch (e) {
        print('Failed to send email:');
        for (var problem in e.problems) {
          print('Error code: ${problem.code} | Message: ${problem.msg}');
        }
      }
    }
    

If you still run into issues, share the exact error message you're getting—this will help pinpoint the problem even faster!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 03:56:10