我想发送带有节点js的电子邮件,但我的代码发出错误。
Error: 140616526522240:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:../deps/openssl/openssl/ssl/s23_clnt.c:794:
at Error (native) code: 'ECONNECTION', command: 'CONN' }
我的代码是:
router.post('/api/emailsending', function(req, res){
var nodemailer = require('nodemailer');
var smtpConfig = {
host: 'euk-84736.eukservers.com',
port: 25,
secure: true, // use SSL
auth: {
user: 'no_reply@xyz.in',
pass: 'xyz123'
}
};
var transporter = nodemailer.createTransport(smtpConfig);
// setup e-mail data with unicode symbols
var mailOptions = {
from: 'no_reply@xyz.in', // sender address
to: 'amandhlbharat@gmail.com', // list of receivers
subject: 'Hello from Nodemailer <img draggable="false" class="emoji" alt="✔" src="https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/svg/2714.svg">', // Subject line
text: 'Hello Node', // plaintext body
html: '<b><img draggable="false" class="emoji" alt="✌" src="https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/svg/270c.svg"></b>' // html body
};
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
})
我不知道如何删除此错误。任何人都可以解决这个问题吗? 您的宝贵回应表示赞赏。
答案 0 :(得分:1)
尝试此代码。首先,您必须在库中Google Cloud Console
和Enable Gmail API
创建应用。获取应用的凭据。点击凭据并代替{{1}保留此链接https://developers.google.com/oauthplayground并保存。在另一个标签中打开此链接打开此链接https://developers.google.com/oauthplayground/点击右侧的设置符号。勾选复选框(即使用您自己的OAuth凭据)这个你必须给你的clientId和clientSecret.And在左侧的同一时间有一个文本框,其中包含Authorized redirect URIs
这样的占位符,保留此链接https://mail.google.com/并单击授权API然后单击{{1然后你会得到你的Input Your Own Scopes
和Exchange authorization code for tokens
将这两个保留在你的代码中。请为你提供这些内容。
使用此代码
refreshToken
以accessToken
运行服务器并打开浏览器并输入const nodemailer = require('nodemailer');
const xoauth2 = require('xoauth2');
var fs = require('fs');
var express = require('express');
var http = require('http');
var https = require('https');
var transporter = nodemailer.createTransport({
service:'gmail',
auth:{
type: 'OAuth2',
user:'sender's mail@gmail.com',
clientId:'Your-clientId of your app',
clientSecret:'Your clientSecret of your app ',
refreshToken:'Your refreshToken from oauth2 playground',
accessToken:'Your accessToken from oauth2 playground'
},
});
var app=express();
var options = {
key: fs.readFileSync('privkey.pem'),
cert: fs.readFileSync('cert.pem')
}
app.get('/mail', function(req,res){
fs.readFile("E:/syed/node/mail/mailwithdb/sheet.xlsx",function(err,data){
var mailOptions={
from:'Name <Sender's mail@gmail.com>',
to:'Receivers's mail@gmail.com',
subject:'Sample mail',
text:'Hello !!!!!!!!!!!!!',
attachments:[
{
'filename':'sheet.xlsx',
'content': data,
'contentType':'application/xlsx'
}]
}
transporter.sendMail(mailOptions,function(err,res){
if(err){
console.log('Error');
}
else{
console.log('Email Sent');
}
})
});
});
var server = http.createServer(app).listen(3000);
var server1 = https.createServer(options,app).listen(3443);
console.log('server running on 3443');
检查您的邮件和控制台。希望这会有所帮助......
答案 1 :(得分:1)
尝试在配置中添加此内容
var smtpConfig = {
host: 'euk-84736.eukservers.com',
port: 25,
tls: {
rejectUnauthorized: false
},
auth: {
user: 'no_reply@xyz.in',
pass: 'xyz123'
}};