Es gibt eine Bibliothek namens mailer
, die genau das tut, wonach Sie gefragt haben: sendet E-Mails.
Set es als eine Abhängigkeit in Ihrem pubspec.yaml
und laufen pub install
:
dependencies:
mailer: any
ich ein einfaches Beispiel Google Mail auf meinem lokalen Windows-Rechner mit:
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'));
}
Die GmailSmtpOptions
ist nur ein Helfer Klasse.
var options = new SmtpOptions()
..hostName = 'localhost'
..port = 25;
Sie können check here for all possible fields in der SmtpOptions
Klasse: Wenn Sie einen lokalen SMTP-Server verwendet werden soll.
Hier ist ein Beispiel mit dem beliebten Rackspace Mailgun:
var options = new SmtpOptions()
..hostName = 'smtp.mailgun.org'
..port = 465
..username = '[email protected]'
..password = 'from mailgun';
Die Bibliothek unterstützt HTML-E-Mails und Anhänge als auch. Schauen Sie sich the example an, um zu lernen, wie man das macht.
Ich benutze persönlich mailer
mit Mailgun in der Produktion verwenden.
FYI, seien Sie vorsichtig mit anwendungsspezifischen Passwörtern. Sie umgehen die 2-Faktor-Authentifizierung und sind nicht auf eine bestimmte Anwendung beschränkt, wie der Name vermuten lässt. Ich würde niemals einen im Quellcode platzieren. _Maybe_ Sie könnten sich sicher fühlen, es aus einem sicheren Schlüsselspeicherdienst zu laden. –
Vielleicht sollte ich klarstellen, dass ich nie jemanden so im Quellcode speichern wollte! Aktualisiertes Beispiel –
Im Falle von Google würde ich niemals sicher sein, solche Passwörter zu speichern. Ich würde höchstens ein E-Mail-Service-spezifisches Passwort speichern, wie bei den meisten E-Mail-Diensten wie Mailgun. Danke für die Nachricht. –