I'am trying to send mail using Oauth and nodemailer on a nodejs app, I did it without Oauth but my password was wrote in the code so I turn myself to Oauth.
I only want to connect myself to send mail in an automatic way. I have settup a project and a service account on google cloud platform. I added the gmail api and wrote some code :
var smtpTransport = nodemailer.createTransport({
host:'smtp.gmail.com',
port:465,
secure:true,
auth:{
type: 'OAuth2',
user: 'thomas.legrand.test@gmail.com',
serviceClient:config.client_id,
privateKey:config.private_key
}
});
var mail = {
from: "thomas.legrand.test@gmail.com",
to: "thomas.legrand26@gmail.com",
subject:"Un sujet abstrait",
html:"<h1> Ceci est un mail de test </h1><h2> et ceci un sous titre </h2> "
};
smtpTransport.on('token', token => {
console.log('A new access token was generated');
console.log('User: %s', token.user);
console.log('Access Token: %s', token.accessToken);
console.log('Expires: %s', new Date(token.expires));
});
smtpTransport.sendMail(mail, function(error, response) {
if(error) {
console.log("Erreur lors de l'envoie du mail ");
console.log(error);
}else {
console.log("succes")
}
smtpTransport.close();
});
But I get an error (unauthorized_client) which I can't solve.
I hope you can help me or give me hints at least !