Nodemailer和“SSL23_GET_SERVER_HELLO:未知协议”错误
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nodemailer和“SSL23_GET_SERVER_HELLO:未知协议”错误相关的知识,希望对你有一定的参考价值。
下面是我的Node.js代码。使用代码导致:
Error: 0:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:opensslssls23_clnt.c:794
这是代码:
var express = require('express')
, fs = require("fs")
, app = express()
, path = require('path')
, request = require('request')
, bodyParser = require('body-parser')
, http = require('http')
, server = http.createServer(app)
, io = require('socket.io').listen(server, {log: true, origins: '*:*'})
;
var smtpTransport = require('nodemailer-smtp-transport');
var options = {
key : fs.readFileSync('server.key'),
cert : fs.readFileSync('server.crt')
};
var nodemailer = require('nodemailer');
var sendmailTransport = require('nodemailer-sendmail-transport');
var emailserver = nodemailer.createTransport(smtpTransport({
service: 'Gmail',
port: 25,
strictSSL: false,
host:'smtp.gmail.com',
SSL Protocol: 'off',
TLS Protocol: ON,
auth: {
user: 'choudhary1707@gmail.com',
pass: 'mypassword'
},
tls: {ciphers: "SSLv3"}
}));
如何解决这个错误?
使用此功能正常
var nodemailer = require('nodemailer');
var smtpTransport = nodemailer.createTransport("SMTP", {
service: "Gmail",
connectionTimeout : "7000",
greetingTimeout : "7000",
auth: {
XOAuth2: {
user: "email id",
clientId: "client id",
clientSecret: "secret",
refreshToken: "refresh token"
}
}
});
使用带有'nodemailer-smtp-transport'节点模块的第三方smtp时出现此错误。
我的问题原来是我使用的是端口587。
当我把它切换到465时,一切都开始工作了。
我的配置如下:
const smtpConfiguration = {
host: '<my smtp host>',
port: 465,
secure: true, // use TLS
auth: {
user: '<user account>',
pass: '<password>'
}
};
我的电子邮件功能(打字稿,蓝鸟承诺):
export const SendEmail = (from:string,
to:string[],
subject:string,
text:string,
html:string) => {
const transportOptions = smtpConfiguration;
const transporter = nodemailer.createTransport(smtpTransport(transportOptions));
const emailOptions = {
from: from,
to: to.join(','),
subject: subject,
text: text,
html: html
};
return new Promise((resolve, reject) => {
transporter.sendMail(emailOptions, (err, data) => {
if (err) {
return reject(err);
} else {
return resolve(data);
}
});
});
};
我遇到过类似的问题
解决了这个thread
// For port 465
var transporter = nodemailer.createTransport({
host: 'smtp.hostname',
port: 465,
secure: true,
auth: {
user: 'email',
pass: 'password'
}
});
// for port 587 or 25 or 2525 etc.
var transporter = nodemailer.createTransport({
host: 'smtp.hostname',
port: 587,
secure: false,
requireTLS: true, // only use if the server really does support TLS
auth: {
user: 'email',
pass: 'password'
}
});
看起来你使它变得比它需要的复杂一点。我成功地使用了Nodemailer的0.7.0版本。
var smtpTransport = nodemailer.createTransport("SMTP", {
service: "Gmail",
auth: {
user: "***@gmail.com",
pass: "****"
}
});
var mailOptions = {
from: "****@gmail.com",
to: to,
subject: 'Subject Line!',
text: 'Alternate Text',
html: '<label>Hello!</label>'
}
smtpTransport.sendMail(mailOptions, function(error, response) {
if (error) {
console.log(error);
} else {
console.log("Message sent: " + response.message);
}
// if you don't want to use this transport object anymore, uncomment following line
smtpTransport.close(); // shut down the connection pool, no more messages
callback();
});
您的问题可能是SSLv3。 Most servers have it disabled。
TLS Protocol: ON,
auth: {
user: 'jdoe@example.com',
pass: 'super-secret-password'
},
tls: {ciphers: "SSLv3"}
您应该使用TLS 1.0及更高版本。
您还应该使用Server Name Indication or SNI,但我不知道如何告诉Node.js使用它。
以上是关于Nodemailer和“SSL23_GET_SERVER_HELLO:未知协议”错误的主要内容,如果未能解决你的问题,请参考以下文章
Node.js 和 Nodemailer:我们可以将 PDF 文档附加到电子邮件中吗?
使用 Nodemailer 和 Webpack 时无法解析 dns 和 child_process
Node.js Nodemailer 和 SES - 错误:未定义传输方法
使用 node.js、node mailer 和 nodemailer-mailgun-transport 发送模板化电子邮件