我如何从我的Gmail帐户在我的Node.js脚本发送电子邮件

How do I send email from my Gmail account in my Node.js script?

本文关键字:我的 脚本 Node js 电子邮件 Gmail      更新时间:2023-09-26
app.get('/email', function(req,res){
    var emailjs = require('emailjs');
    var email_server = emailjs.server.connect({
        host: 'smtp.gmail.com',
        ssl: true,
        tls: true,
        port: "465",
        user:'kateholloway@gmail.com',
        password:'mypassword',
    });
    var h={
        text: 'hey how you doing',
        from: 'Kate Holloway <kateholloway@gmail.com>',
        to: 'someonesemail@gmail.com',
        subject: 'where is your phone'
    };
    var message = emailjs.message.create(h);
    email_server.send(message, function(err,message){
        console.log(err);
        console.log(message);
        res.send('ok');
    });
});

这个设置正确吗?

我得到的错误信息是:

{ [Error: connection has ended] code: 9, smtp: undefined }

注意:此代码适用于我自己的域/服务器。但它不适用于Gmail。因此,我认为我的Gmail smtp设置不正确。

我们已经在Gmail中使用了这个库。我们只设置了主机、用户、密码和ssl选项。一切正常。尝试删除其他选项