繁体   English   中英

使用nodemailer发送邮件,需要步骤

[英]Sending mail using nodemailer, need the steps

我是testcafe和 Node.js 的testcafe ,我想使用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. 对于 SMTP,我是否需要从 npm 安装任何东西?

不,只有nodemailer

  1. 我想从我的公司电子邮件发送邮件,需要什么不同的设置?
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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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