'Sending an SMTP email in Dart
I looked through the API documentation and language guide, but I did not see anything about sending emails in Dart. I also checked this google groups post, but it's quite old by Dart standards.
Is this possible to do? I know that I can always use the Process class to invoke external programs, but I'd prefer a real Dart solution if there's any.
Solution 1:[1]
There's a library called mailer
, which does exactly what you asked for: sends out emails.
Set it as a dependency in your pubspec.yaml
and run pub install
:
dependencies:
mailer: any
I will give a simple example using Gmail on my local Windows machine:
import 'package:mailer/mailer.dart';
main() {
var options = new GmailSmtpOptions()
..username = '[email protected]'
..password = 'my gmail password'; // If you use Google app-specific passwords, use one of those.
// As pointed by Justin in the comments, be careful what you store in the source code.
// Be extra careful what you check into a public repository.
// I'm merely giving the simplest example here.
// Right now only SMTP transport method is supported.
var transport = new SmtpTransport(options);
// Create the envelope to send.
var envelope = new Envelope()
..from = '[email protected]'
..fromName = 'Your company'
..recipients = ['[email protected]', '[email protected]']
..subject = 'Your subject'
..text = 'Here goes your body message';
// Finally, send it!
transport.send(envelope)
.then((_) => print('email sent!'))
.catchError((e) => print('Error: $e'));
}
The GmailSmtpOptions
is just a helper class. If you want to use a local SMTP server:
var options = new SmtpOptions()
..hostName = 'localhost'
..port = 25;
You can check here for all possible fields in the SmtpOptions
class.
Here's an example using the popular Rackspace Mailgun:
var options = new SmtpOptions()
..hostName = 'smtp.mailgun.org'
..port = 465
..username = '[email protected]'
..password = 'from mailgun';
The library supports HTML emails and attachments as well. Check out the example to learn how to do that.
I am personally using mailer
with Mailgun in production use.
Solution 2:[2]
== Updated Answer:
You can use official mailer library from pub.dev:
Add mailer library below your dependencies:
at pubspec.yaml
dependencies:
mailer: ^3.2.1
Then make sure to import those two lines:
import 'package:mailer/mailer.dart';
import 'package:mailer/smtp_server.dart';
And you can create this method and use it wherever you want: (This is a example to send SMTP mail for Gmail)
void sendMail() async {
String username = '[email protected]';
String password = 'password';
final smtpServer = gmail(username, password);
final equivalentMessage = Message()
..from = Address(username, 'Your name')
..recipients.add(Address('[email protected]'))
..ccRecipients.addAll([Address('[email protected]'), '[email protected]'])
..bccRecipients.add('[email protected]')
..subject = 'Test Dart Mailer library :: ? :: ${DateTime.now()}'
..text = 'This is the plain text.\nThis is line 2 of the text part.'
..html = "<h1>Test</h1>\n<p>Hey! Here's some HTML content</p>";
await send(equivalentMessage, smtpServer);
}
}
But make sure that you enable (Less secure app access) at your Gmail Account Settings > Security to successfully integrate with your Email and send this mail.
- Documentation & full example: https://pub.dev/packages/mailer
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 |