从 Flutter Web 发送邮件
Posted
技术标签:
【中文标题】从 Flutter Web 发送邮件【英文标题】:Send mail from Flutter Web 【发布时间】:2021-10-01 19:32:24 【问题描述】:您好,我想从 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:
我找不到任何相关的解决方案。我错过了什么还是有更简单的方法来实现这个?希望有人可以提供帮助。非常感谢!
【问题讨论】:
【参考方案1】:即使您在使用 Flutter 生成 Web 应用程序时使用 Flutter,所有移动事物的事情都会发生在 javascript 中。我强烈建议不要将任何电子邮件凭据放入 JavaScript 代码!!!
为了避免只创建一个可调用的 Firebase 函数,您可以从本机应用程序和 Web 应用程序调用该函数来发送电子邮件。您可以有一个带有电子邮件内容的有效负载,并且该函数还可以在context
中获取身份验证数据。如果发送失败或成功,您也会收到响应。在可调用的云函数中,您可以使用普通的JS代码和包发送电子邮件,如nodemailer
。
【讨论】:
以上是关于从 Flutter Web 发送邮件的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Flutter Web 通过联系表单发送电子邮件?