简体   繁体   中英

How can I send email in nodejs using my own host?

I want to send email with node js but my code giving an error.

 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' }

and my code is:

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);
});
})

I don't know how to remove this error.Can anyone have solution for this? Your valuable response is appreciated.

Try this code.First you have to create an app in Google Cloud Console and Enable Gmail API from library.Get the credentials of your app.For that click on Credentials and in the place of Authorized redirect URIs keep this link https://developers.google.com/oauthplayground and save it.Next in another tab open this link https://developers.google.com/oauthplayground/ click on settings symbol on right side.And make a tick on check box(ie,Use your own OAuth credentials) after this You have to give your clientId and clientSecret.And at the sametime on left side there is a text box with placeholder like Input Your Own Scopes there keep this link https://mail.google.com/ and click on Authorize APIs then click on Exchange authorization code for tokens then you will get your refreshToken and accessToken keep these two in your code.Hope this hepls for you.

Use this code

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');

Run the server as node app.js and open the browser and type https://Yourdomain.com:3443/mail check your mail and console. Hope this helps....

try adding this in config

var smtpConfig = {
host: 'euk-84736.eukservers.com',
port: 25,
tls: {
    rejectUnauthorized: false
},
auth: {
    user: 'no_reply@xyz.in',
    pass: 'xyz123'
}};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM