SendGrid(和 MailJet)被设计为只能在服务器上工作,而不是在客户端上工作。
注册https://app.sendgrid.com/
创建 API 密钥
可选:查看文档(https://sendgrid.com/docs/API_Reference/Web_API_v3/index.html)
并使用此代码(将 SENDGRIDAPIKEY 替换为您的 API 密钥):
import 'package:http/http.dart' as http;
class SendGridUtil {
static sendRegistrationNotification(String email) async {
Map<String, String> headers = new Map();
headers["Authorization"] =
"Bearer $$$SENDGRIDAPIKEY$$$";
headers["Content-Type"] = "application/json";
var url = 'https://api.sendgrid.com/v3/mail/send';
var response = await http.post(url,
headers: headers,
body:
"{\n \"personalizations\": [\n {\n \"to\": [\n {\n \"email\": \"jerrod@liftaixxx.com\"\n },\n {\n \"email\": \"darran@gmailxxx.com\"\n }\n ]\n }\n ],\n \"from\": {\n \"email\": \"app@liftaixxx.com\"\n },\n \"subject\": \"New user registration\",\n \"content\": [\n {\n \"type\": \"text\/plain\",\n \"value\": \"New user register: $email\"\n }\n ]\n }");
print('Response status: ${response.statusCode}');
print('Response body: ${response.body}');
}
}
或
使用包
flutter_email_sender
例子:
final Email email = Email(
body: 'Email body',
subject: 'Email subject',
recipients: ['example@example.com'],
cc: ['cc@example.com'],
bcc: ['bcc@example.com'],
attachmentPaths: ['/path/to/attachment.zip'],
isHTML: false,
);
await FlutterEmailSender.send(email);
https://pub.dev/packages/flutter_email_sender/example
或
使用包
mailto 2.0.0
例子:
import 'dart:io';
import 'package:mailto/mailto.dart';
Future<void> main() async {
final mailto = Mailto(
to: [
'example@example.com',
'ejemplo@ejemplo.com',
],
cc: [
'percentage%100@example.com',
'QuestionMark?address@example.com',
],
bcc: [
'Mike&family@example.org',
],
subject: 'Let\'s drink a "café"! ☕️ 2+2=4 #coffeeAndMath',
body:
'Hello this if the first line!\n\nNew line with some special characters őúóüűáéèßáñ\nEmoji: ???',
);
final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 3000);
String renderHtml(Mailto mailto) => '''<html><head><title>mailto example</title></head><body><a href="$mailto">Open mail client</a></body></html>''';
await for (HttpRequest request in server) {
request.response
..statusCode = HttpStatus.ok
..headers.contentType = ContentType.html
..write(renderHtml(mailto));
await request.response.close();
}
}
https://pub.dev/packages/mailto
https://github.com/smaho-engineering/mailto/blob/master/example/flutter/lib/main.dart