使用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.
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.
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')); }
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:
GmailSmtpOptionsshould default to port 465 (SSL), but if that fails, try port 587 (TLS) by adding..port = 587to 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:
mailer2is a fork that hasn't been updated in years. Consider migrating to the official, actively maintainedmailerpackage (see below for how).
mailer Package Since mailer2 is unmaintained, switching to the official mailer package will give you better stability and support. Here's a quick example:
- Add dependencies to
pubspec.yaml:dependencies: mailer: ^6.0.1 # Use the latest version from pub.dev mailer_smtp: ^2.0.0 - 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

