我查阅了API文档和语言指南,但没有看到任何关于在Dart中发送邮箱的内容.我也判断了这google groups post个,但按照Dart的标准,它已经很旧了.
这有可能做到吗?我知道我总是可以使用Process类来调用外部程序,但如果有的话,我更喜欢真正的DART解决方案.
我查阅了API文档和语言指南,但没有看到任何关于在Dart中发送邮箱的内容.我也判断了这google groups post个,但按照Dart的标准,它已经很旧了.
这有可能做到吗?我知道我总是可以使用Process类来调用外部程序,但如果有的话,我更喜欢真正的DART解决方案.
有一个名为mailer
的图书馆,它的功能正是你想要的:发送邮箱.
将其设置为pubspec.yaml
中的依赖项,然后运行pub install
:
dependencies:
mailer: any
我将给出一个在本地Windows计算机上使用Gmail的简单示例:
import 'package:mailer/mailer.dart';
main() {
var options = new GmailSmtpOptions()
..username = 'kaisellgren@gmail.com'
..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 = 'support@yourcompany.com'
..fromName = 'Your company'
..recipients = ['someone@somewhere.com', 'another@example.com']
..subject = 'Your subject'
..text = 'Here goes your body message';
// Finally, send it!
transport.send(envelope)
.then((_) => print('email sent!'))
.catchError((e) => print('Error: $e'));
}
GmailSmtpOptions
只是一个帮手类.如果要使用本地SMTP服务器:
var options = new SmtpOptions()
..hostName = 'localhost'
..port = 25;
你可以在SmtpOptions
班考check here for all possible fields分.
下面是一个使用流行的Rackspace Mailgun的例子:
var options = new SmtpOptions()
..hostName = 'smtp.mailgun.org'
..port = 465
..username = 'postmaster@yourdomain.com'
..password = 'from mailgun';
该库还支持HTML邮箱和附件.查看the example了解如何做到这一点.
我个人使用mailer
与邮枪在生产中使用.