每当我运行代码时,我都会收到错误"错误:在SMTPConnection._formatError ...."
中从未收到过问候语function automatedMailSender(req,res){
var mailOptions = {
from: "abc <abc@test.com>", // sender address
to: 'nit@test.com', // list of receivers
subject: 'Hello ', // Subject line
text: 'Hello world ?', // plain text body
html: '<b>Hello world ?</b>' // html body
};
var mailer=nodemailer.createTransport({
host: 'mail.test.com',
port: 465,
secure: false, // secure:true for port 465, secure:false for port 587
auth: {
user: 'mymail@test.com',
pass: '1234'
}
});
mailer.sendMail(mailOptions, (error, response)=>{
if(error){
console.log('mail not sent \n',error);
}
else{
console.log("Message sent: " ,response.message);
}
});
}
我在代码中做错了。
答案 0 :(得分:1)
代码中的答案
port: 465,
secure: false, // secure:true for port 465, secure:false for port 587
&#34; secure&#34;的价值对于端口465应该是真的
答案 1 :(得分:1)
这是由于您的未经授权用户的SMTP安全性所致,因此,为了解决此问题,您需要设置u+fffd
希望这会对您有所帮助。
答案 2 :(得分:0)
尝试类似的东西,它对我来说很完美:
//Create reusable transporter object using the default SMTP transport
var transporter = nodemailer.createTransport(
'smtps://USERNAME%40gmail.com:PASSWORD@smtp.gmail.com');
// setup e-mail data
var mailOptions = {
from: '"Michel " <recipe@example.com>', // sender address
to: 'bernard@example.com', // list of receivers
subject: 'Hello', // Subject line
text: 'Bonjour!' // plaintext body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
如果仍然没有工作,你会有一点tuto here
答案 3 :(得分:0)
如果您使用Gmail发送邮件,请尝试此操作。
答案 4 :(得分:0)
ChildActivity.this.getParent().moveTaskToBack(true)
使用邮件变量中的上述代码修复它。 谢谢你们
答案 5 :(得分:0)
const express = require('express');
const router = express.Router();
const { Email } = require('../models/email.model');
const { User } = require('../models/user.model');
const nodemailer = require('nodemailer');
//post email
router.post('/send', async (req, res) => {
if (req.query) {
var query = {}
if (req.query.lastname) { query.lastname = req.query.lastname }
if (req.query.firstname) { query.firstname = req.query.firstname }
if (req.query.email) { query.email = req.query.email }
if (req.query.isactive) { query.isActive = req.query.isactive }
if (req.query.isgain) { query.isGain = req.query.isgain }
}
const email = new Email({
title: req.body.title,
subject: req.body.subject,
text: req.body.context,
description: req.body.description
});
if (req.body.title && req.body.subject && req.body.context) {
const user_db = await User.find(query)
const user_db_email = user_db.map(x => x.email)
var smtpTransport = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: { user: 'ghiles@gmail.com', pass: 'g' },
tls: { rejectUnauthorized: false }, // do not fail on invalid certs
});
var mailOptions = {
to: user_db_email,
from: 'fatb@gmail.com',
subject: req.body.subject,
text: req.body.context
};
email.save().then(() => {
smtpTransport.sendMail(mailOptions)
.then(() => res.status(200).json(email))
.catch(() => res.status(500).json({ success: false, message: `erreur dans l'envoi de mail` }))
}).catch(() => res.status(200).json({ success: false, message: `erreur dans le serveur` }))
} else {
res.status(400).json({ success: false, message: `Veuillez renseigner le titre et l'objet et le contexte du mail` })
}
module.exports = router;
答案 6 :(得分:0)
host: 'smtp.host.com',
port: 587, // 587 or 25 or 2525
secure: false, // secure:true for port 465, secure:false for port 587
auth: {
user: 'mymail@test.com',
pass: '1234'
}
尝试一下。给smtp加上前缀并更改端口。