NodeMailer不从Ubuntu服务器发送邮件

mw3dktmi  于 2023-03-22  发布在  其他
关注(0)|答案(1)|浏览(85)

你好,我有一个简单的Nodejs程序,它使用了Nodemailer:

var syncSql = require('sync-mysql');             
var nodemailer = require('nodemailer');   

var syncCon = new syncSql(
{ 
    host:'localhost', 
    user:'_nodeuser', 
    password:'_password', 
    database:'mydatabase'
});

var transporter = nodemailer.createTransport(
{
    service: 'gmail',
    auth: 
    {
        user: '_email@gmail.com',
        pass: '_gmailAppPass'
    },
    logger: true
});

...
resp2 = syncCon.query(sql);

var mailOptions =
            {
                from:   '_email@gmail.com',
                to:      resp2.email,                       
                subject:'_email subject',     
                html:   '_email body'
            };

            transporter.sendMail(mailOptions, function(error, info) {
                if(error) {
                    console.log('remind_old_action(): '+ error);
                } else {
                    console.log('Email sent: ' + info.response);   
                }
            });

在我的本地Win 11机器上,它可以工作:

[2023-03-20 11:33:08] DEBUG Creating transport: nodemailer (6.9.0; +https://nodemailer.com/; SMTP/6.9.0[client:6.9.0])
[2023-03-20 11:33:09] DEBUG Sending mail using SMTP/6.9.0[client:6.9.0]
[2023-03-20 11:33:10] DEBUG [0FD44uxJ0] Resolved smtp.gmail.com as 173.194.76.108 [cache miss]
[2023-03-20 11:33:10] INFO  [0FD44uxJ0] Secure connection established to 173.194.76.108:465
[2023-03-20 11:33:11] DEBUG [0FD44uxJ0] SMTP handshake finished
[2023-03-20 11:33:11] INFO  [0FD44uxJ0] User "_email@gmail.com" authenticated
[2023-03-20 11:33:11] INFO  Sending message <c25b179b-d610-4a55-8cdd-d5605ac0ae00@gmail.com> to <_target_email>
[2023-03-20 11:33:12] INFO  [0FD44uxJ0] <501 bytes encoded mime message (source size 498 bytes)>
[2023-03-20 11:33:13] DEBUG [0FD44uxJ0] Closing connection to the server using "end"
Email sent: 250 2.0.0 OK  1679311993 v15-20020a05600c444f00b003edddce5b00sm4292772wmn.12 - gsmtp
[2023-03-20 11:33:13] INFO  [0FD44uxJ0] Connection closed

但是当我在Ubuntu 22.04服务器上运行这个程序时,它在那里不工作:

[2023-03-20 11:40:27] DEBUG Creating transport: nodemailer (6.9.1; +https://nodemailer.com/; SMTP/6.9.1[client:6.9.1])
[2023-03-20 11:40:27] DEBUG Sending mail using SMTP/6.9.1[client:6.9.1]
[2023-03-20 11:40:27] DEBUG [jZWnAXUE78U] Resolved smtp.gmail.com as 172.253.63.108 [cache miss]
[2023-03-20 11:42:27] ERROR [jZWnAXUE78U] Connection timeout
[2023-03-20 11:42:27] DEBUG [jZWnAXUE78U] Closing connection to the server using "destroy"
[2023-03-20 11:42:27] ERROR Send Error: Connection timeout
remind_old_action(): Error: Connection timeout

似乎问题不是Nodemailer本身。我也试过从快递服务器(app)-同样的情况。express app和发送邮件都是从本地机器工作的。Express app本身从Ubuntu服务器工作,但发送邮件不是。我也使用Nginx作为express app的反向代理,用config玩,打开/关闭-在这两种情况下都无法发送电子邮件。Nodemailer无法连接到smtp.gmail.com,等待120秒后出现错误。我还可以检查什么?
UPD:我已经在Google帐户上启用了2FA,并创建了一个“不太安全”的16字符应用程序密码。它在本地Win机器上正常工作,身份验证没有问题,但在Ubuntu服务器上超时。

lp0sw83n

lp0sw83n1#

您正在使用Gmail服务发送邮件,因此您可以创建Gmail的16位密码,此密码是使用

var transporter = nodemailer.createTransport(
{
    service: 'gmail',
    auth:
    {
        user: '_email@gmail.com',
        pass: '16 digit _gmailAppPass'
    },
    logger: true
});

相关问题