提问人:Ankere 提问时间:4/6/2023 更新时间:4/6/2023 访问量:39
如何在不嵌入凭据的情况下使用 Mailer?
How to use Mailer without embedding credentials?
问:
您好,我正在尝试制作一项服务,用户可以将电子邮件发送到预设位置。例如,用户可以发送有关任何错误的电子邮件,但它不使用自己的电子邮件,而是使用应用程序中预设的电子邮件。
我正在尝试使用邮件应用程序,并且在 api 文档中,出于安全原因,它说不要直接在代码中输入您的凭据。它提到 Johanness Milke 有一个关于如何做到这一点的指南,但我无法通过搜索他的 repo 甚至通过在线谷歌搜索在任何地方找到它。有谁知道如何在不嵌入凭据的情况下使用邮件程序。
邮件程序示例取自软件包网站
我不想嵌入-----行中的部分(用户名/密码)
import 'package:mailer/mailer.dart';
import 'package:mailer/smtp_server.dart';
main() async {
// Note that using a username and password for gmail only works if
// you have two-factor authentication enabled and created an App password.
// Search for "gmail app password 2fa"
// The alternative is to use oauth.
-------------------------------------------------------------------------------
String username = '[email protected]';
String password = 'password';
-------------------------------------------------------------------------------
final smtpServer = gmail(username, password);
// Use the SmtpServer class to configure an SMTP server:
// final smtpServer = SmtpServer('smtp.domain.com');
// See the named arguments of SmtpServer for further configuration
// options.
// Create our message.
final message = Message()
..from = Address(username, 'Your name')
..recipients.add('[email protected]')
..ccRecipients.addAll(['[email protected]', '[email protected]'])
..bccRecipients.add(Address('[email protected]'))
..subject = 'Test Dart Mailer library :: 😀 :: ${DateTime.now()}'
..text = 'This is the plain text.\nThis is line 2 of the text part.'
..html = "<h1>Test</h1>\n<p>Hey! Here's some HTML content</p>";
try {
final sendReport = await send(message, smtpServer);
print('Message sent: ' + sendReport.toString());
} on MailerException catch (e) {
print('Message not sent.');
for (var p in e.problems) {
print('Problem: ${p.code}: ${p.msg}');
}
}
// DONE
// Let's send another message using a slightly different syntax:
//
// Addresses without a name part can be set directly.
// For instance `..recipients.add('[email protected]')`
// If you want to display a name part you have to create an
// Address object: `new Address('[email protected]', 'Display name part')`
// Creating and adding an Address object without a name part
// `new Address('[email protected]')` is equivalent to
// adding the mail address as `String`.
final equivalentMessage = Message()
..from = Address(username, 'Your name 😀')
..recipients.add(Address('[email protected]'))
..ccRecipients.addAll([Address('[email protected]'), '[email protected]'])
..bccRecipients.add('[email protected]')
..subject = 'Test Dart Mailer library :: 😀 :: ${DateTime.now()}'
..text = 'This is the plain text.\nThis is line 2 of the text part.'
..html = '<h1>Test</h1>\n<p>Hey! Here is some HTML content</p><img src="cid:[email protected]"/>'
..attachments = [
FileAttachment(File('exploits_of_a_mom.png'))
..location = Location.inline
..cid = '<[email protected]>'
];
final sendReport2 = await send(equivalentMessage, smtpServer);
// Sending multiple messages with the same connection
//
// Create a smtp client that will persist the connection
var connection = PersistentConnection(smtpServer);
// Send the first message
await connection.send(message);
// send the equivalent message
await connection.send(equivalentMessage);
// close the connection
await connection.close();
}
答: 暂无答案
评论