我查阅了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与邮枪在生产中使用.

Dart相关问答推荐

在Dart中进行系统调用?

Dart / Flutter 错误:没有为类Logger定义toStringDeep

如何在 Flutter 的小部件树中将新的 MaterialPageRoute 作为子项打开

如何签署 Flutter 的应用程序

如何组织混合HTTP服务器+web客户端Dart元素文件?

如何在 Dart 中创建我们自己的metadata元数据?

如何从列表中的元素创建逗号分隔的字符串

将符号转换为字符串

Dart 工厂构造函数 - 它与const构造函数有何不同

Dart 中的抽象基类

如何在 Dart 中获取字符串的字节数?

Flutter/Dart:按第一次出现拆分字符串

可以在 Dart 中的抽象类中声明静态方法吗?

Dart 中 == 和 === 有什么区别?

如何使用 Dart 列出目录的内容?

Dart 脚本会在浏览器中本地运行吗?

GWT 与 Dart - 主要区别是什么? Dart 是 GWT 的潜在替代品吗?

如何通过 Dart 中的值获取 Map 键?

将`_`(即下划线)作为唯一参数传递给 Dart 语言函数是什么意思?

Dart 是否支持枚举?