gpt4 book ai didi

javascript - 通过用 Electron/React/TS 编写的应用程序发送电子邮件

转载 作者:行者123 更新时间:2023-12-02 16:21:38 24 4
gpt4 key购买 nike

我正在尝试在我的应用程序中实现电子邮件/gmail 发送机制。这是一个桌面应用程序,其托管内容是在 React 和 Electron 的帮助下编写的。我一直在尝试以网络上描述的各种方式实现它,使用各种模块、googleapi 等,但总是以一些奇怪的错误和更神秘的问题描述结束——代码通常基于在文章的“Node.js”部分。我不知道我是否完全理解这个问题的架构方面,是否有可能触发发送机制,或者它是否必须以服务器端服务的形式实现。如果有人能阐明这个问题,我将不胜感激,因为我已经在这个问题上花费了太多的时间。提前致谢!

最佳答案

想通了(终于!)。我把它写在这里,希望它能对将来的人有所帮助。

所有尝试和方法对我失败的主要原因是我在渲染/浏览器进程而不是主电子/Node 进程(来自 Electron 的 documentation)上实现它们。当我移动时详细描述的实现 this文章(利用 nodemailer 的方法)到主进程,在我的例子中,它几乎只由 electron.js 组成,并通过 ipc 触发它>(进程间通信)从渲染进程开始它奇迹般地开始工作了!

感谢罗伯特指点this问题出来,因为以下声明使我走上了正确的道路。

I was facing the same issue when trying to implement the nodemailer code client side. The reason was because nodemailer doesn't seem to work in the browser (only in node). Moving it serverside (into an express app) solved the issue.

// electron.js
var nodemailer = require("nodemailer");

function SendIt() {
var transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: "your_email@gmail.com",
pass: "your_password",
},
});

const mailOptions = {
from: "your_email@gmail.com",
to: "recepient@gmail.com",
subject: "Subject of your email",
html: "<p>Your html here</p>",
};

transporter.sendMail(mailOptions, function (err, info) {
if (err) console.log(err);
else console.log(info);
});
}

electron.ipcMain.on("SendIt", (event, args) => {
console.log("ipcMain: Executing SendIt");
SendIt();
});

关于javascript - 通过用 Electron/React/TS 编写的应用程序发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65359469/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com