更新时间:2023-09-28 17:47:04
有一个名为 mailer
的图书馆
在 pubspec.yaml
中设置为依赖关系,运行 pub install
:
依赖关系:
mailer:any
我将在本地Windows计算机上使用Gmail的一个简单示例:
import'package:mailer / mailer.dart';
main(){
var options = new GmailSmtpOptions()
..username ='kaisellgren@gmail.com'
..password ='my gmail password '; //如果您使用Google应用程式专用密码,请使用其中之一。
// Justin在评论中指出,要小心你存储在源代码中。
//要特别注意你在公共存储库中检查什么。
//我只是给出最简单的例子。
//现在只支持SMTP传输方法。
var transport = new SmtpTransport(options);
//创建要发送的信封。
var envelope = new Envelope()
..from ='support@yourcompany.com'
..fromName ='您的公司'
..recipients = ['someone @ somehere.com','another@example.com']
..subject ='您的主题'
..text ='这里是你的身体信息';
//最后,发送吧!
transport.send(envelope)
.then((_)=> print('email sent!'))
.catchError((e)=& $ e'));
}
GmailSmtpOptions
只是一个帮助类。如果您要使用本地SMTP服务器:
var options = new SmtpOptions $ b ..hostName ='localhost'
..port = 25;
您可以 SmtpOptions 类中的主/ lib / src / smtp / smtp_options.dartrel =noreferrer>检查所有可能的字段。
以下是使用流行的 Rackspace Mailgun 的示例:
var options = new SmtpOptions()
..hostName ='smtp.mailgun.org'
..port = 465
..username ='postmaster@yourdomain.com'
..password ='from mailgun';
库还支持HTML电子邮件和附件。请查看示例,了解如何执行此操作。 p>
我在生产使用中亲自使用 mailer
和Mailgun。
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.
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 = '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'));
}
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 = 'postmaster@yourdomain.com'
..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.