当我在构造函数中调用sendMail函数时,它运行良好,但我调用了 单击按钮时该功能不起作用。我不明白为什么? 请帮帮我!
这是错误消息
request.js:150 OPTIONS https://api.nodemailer.com/user 405
Access to fetch at 'https://api.nodemailer.com/user' from origin 'http://localhost:3301' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.`enter code here`
index.js:33 Uncaught TypeError: net.isIP is not a function
at Object.push../node_modules/nodemailer/lib/shared/index.js.module.exports.resolveHostname (index.js:33)
at SMTPConnection.connect (index.js:333)
at index.js:263
at index.js:70
at run (setImmediate.js:40)
at runIfPresent (setImmediate.js:69)
at onGlobalMessage (setImmediate.js:109)
这是我的javascript文件:
const SmtpPage = Form.create({ name: 'SmtpPage' })(
class extends React.Component {
constructor(props) {
super(props);
this.state = {
data: [],
};
this.sendMail(); // this is working well
}
sendMail = () => {
// eslint-disable-next-line global-require
const nodemailer = require('nodemailer');
nodemailer.createTestAccount(() => {
const message = {
from: 'elonmusk@tesla.com', // Sender address
to: 'to@email.com', // List of recipients
subject: 'Design Your Model S | Tesla', // Subject line
text: 'Have the most fun you can in a car. Get your Tesla today!', // Plain text body
};
const transport = nodemailer.createTransport({
host: 'smtp.mailtrap.io',
port: 2525,
auth: {
user: '0130a10cc25c83',
pass: '750ca28f3b6d1c',
},
});
transport.sendMail(message, (err, info) => {
if (err) {
console.log(err);
} else {
console.log(info);
}
});
});
}
render() {
return (
<button onClick={() => this.sendMail()}>
Test Connection
</button>
);
}
},
);
export default SmtpPage;
单击按钮时不起作用