gpt4 book ai didi

node.js - 使用nodemailer发送邮件,需要的步骤

转载 作者:搜寻专家 更新时间:2023-11-01 00:47:41 27 4
gpt4 key购买 nike

我是 testcafe 和 Node.js 的新手,我想使用 nodemailer 发送邮件。有人可以给我步骤吗?我试了几次我不确定我是否做错了什么。

接下来的步骤是:

  1. 安装 nodemailer:npm install nodemailer
  2. 使用 SMTP 或其他传输机制创建 Nodemailer 传输器
  3. 设置消息选项(谁向谁发送什么)
  4. 使用您之前创建的传输器的 sendMail() 方法传送消息对象:
var mailer = require("nodemailer");

var transporter = mailer.createTransport({
host: "smtp.abc.email",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: "email@abc.com", //
pass: "password" //
}
});

// send mail with defined transport object
var mail = {
from: "email@abc.com", // sender address
to: "email@abc.com", // list of receivers
subject: "hello", // Subject line
text: "Hello world?", // plain text body
html: "<b>Hello world?</b>" // html body
};

transporter.sendMail(mail, function(error, response) {
if (error) {
console.log(error);
} else {
console.log("Message sent: " + response.message);
}

transporter.close();
});

我有以下问题:

  1. 对于 SMTP,我需要从 npm 安装任何东西吗?

  2. net 中的大多数示例显示 Gmail,但我将其替换为我公司的邮件,我在这里考虑为 smtp.abc.com 我还需要做任何其他事情吗?

  3. 我想从公司邮箱发送邮件,需要什么不同的设置?

最佳答案

  1. For SMTP do I need to install anything from npm?

没有。只有 nodemailer

  1. I want to send mail from my company email, what different setting is needed for that?
import * as nodeMailer from "nodemailer";

const MAILER_CONFIG = Object.freeze({
host: "smtp.mycompany.com",
port: 25,
secure: false, // true for 465 SMTP port, otherwise false
auth: {
user: "username",
pass: "password"
}
});

const MAIL_OPTIONS = Object.freeze({
from: mailSender,
to: mailReceiver,
subject: mailSubject,
text: mailBodyText,
html: mailBodyHTML
});

const TRANSPORTER = Object.freeze(nodeMailer.createTransport(MAILER_CONFIG));

const mailSendingResult = await TRANSPORTER.sendMail(MAIL_OPTIONS);

TRANSPORTER.close();

关于node.js - 使用nodemailer发送邮件,需要的步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57184995/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com