您好,我想从Flutter Web发送邮件到注册电子邮件。我希望在新注册用户第一次登录时发送随机安全码。收件人的电子邮件取自TextFormField。我参考了邮件pub.dev和示例代码。下面是我添加的代码和我得到的结果。我没有为这个实现做任何其他配置。
final smtpServer = gmail('email address', 'password');
final message = Message()
..from = Address('email address', 'Your Name')
..recipients.add(_emailText.text)
..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}');
}
}
var connection = PersistentConnection(smtpServer);
// Send the first message
await connection.send(message);
// close the connection
await connection.close();
这是我得到的错误:
Error: Unsupported operation: Socket constructor
at Object.throw_ [as throw] (http://localhost: )
at Function._connect (http://localhost: )
at Function.connect (http://localhost: )
at connection.Connection.new.connect (http://localhost: )
at connect.next (<anonymous>)
at runBody (http://localhost: )
at Object._async [as async] (http://localhost: )
at connection.Connection.new.connect (http://localhost: )
at connect (http://localhost: )
at connect.next (<anonymous>)
at runBody (http://localhost: )
at Object._async [as async] (http://localhost: )
at Object.connect (http://localhost: )
at send (http://localhost: )
at send.next (<anonymous>)
at runBody (http://localhost: )
at Object._async [as async] (http://localhost: )
at Object.send (http://localhost: )
at signUpForm._SignUpFormState.new.<anonymous> (http://localhost: )
at Generator.next (<anonymous>)
at http://localhost:
at _RootZone.runUnary (http://localhost: )
at _FutureListener.thenAwait.handleValue (http://localhost: )
at handleValueCallback (http://localhost: )
at Function._propagateToListeners (http://localhost: )
at _Future.new.[_completeWithValue] (http://localhost: )
at async._AsyncCallbackEntry.new.callback (http://localhost: )
at Object._microtaskLoop (http://localhost: )
at _startMicrotaskLoop (http://localhost: )
at http://localhost:
我找不到任何相关的解决方案。我错过了什么,还是有更简单的方法来实现这一点?希望有人能帮忙。非常感谢!
当你使用Flutter生成Web应用程序时,所有移动事物的事情都发生在JavaScript中。我强烈建议不要将任何电子邮件凭证放入JavaScript代码中!!
为了避免这种情况,只需创建一个可调用的Firebase函数,你可以从本地应用程序和Web应用程序调用它来发送电子邮件。你可以有一个电子邮件内容的有效载荷,这个函数也在context
中获得认证数据。如果发送失败或成功,您还会得到一个响应。在可调用的云函数中,你可以使用正常的JS代码和包来发送电子邮件,如nodemailer
。