You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nodemailer无法发送邮件提示Message sent:Undefined故障求助

故障根因
  • 配置冲突:你在创建transporter时同时设置了sendmail: true和SMTP服务参数。sendmail参数开启后,Nodemailer会优先调用系统原生sendmail工具发信,完全忽略后续填写的SMTP地址、账号密码等配置。当前运行环境未安装sendmail工具,因此抛出spawn sendmail ENOENT错误。
  • 异步逻辑未处理:transporter.sendMail()是异步方法,返回Promise对象,你未使用await或者.then()处理异步返回结果,直接同步读取info.messageId就会得到undefined,同时异步抛出的错误没有被捕获,因此出现UnhandledPromiseRejectionWarning警告。
  • 发件人配置不符合服务商要求:Outlook SMTP强制要求发件人地址必须和认证使用的邮箱完全一致,你当前填写的发件人foo@example.com和认证邮箱memail@outlook.com不符,即使前两个问题修复也会被SMTP服务器拒收。
  • TLS配置过时:你设置的ciphers: "SSLv3"使用的SSLv3协议早已被废弃,Outlook SMTP目前仅支持TLS1.2及以上版本,该配置会导致握手失败。
修复方案
  1. 调整transporter配置,删除sendmail: true和ciphers: "SSLv3"两行配置
  2. 将接口处理函数改为async函数,为sendMail方法添加await,同时添加try/catch捕获异步错误
  3. 将发件人地址修改为你认证使用的Outlook邮箱地址

修复后的核心代码如下:

// 仅展示修改后的/sendEmail接口逻辑,其余代码保持不变
app.post("/sendEmail", async function (req, res) {
  console.log(req.body);
  const emailInfo = {
    id: uuidv4(),
    email: req.body.email,
    cc: req.body.cc,
    message:req.body.message,
  };
  const dataArray = listUserData();
  dataArray.push(emailInfo);
  fs.writeFileSync(readData, JSON.stringify(dataArray));
  console.log("SentBody", emailInfo);

  let transporter = nodemailer.createTransport({
    host: "smtp.outlook.com",
    port: 587,
    secure: false,
    auth: {
      user: "memail@outlook.com",
      pass: "mypw",
    },
  });

  try {
    // send mail with defined transport object
    let info = await transporter.sendMail({
      from: '"Fred Foo 👻" <memail@outlook.com>', // 修改为实际认证的邮箱
      to: "garbageacc7878@outlook.com",
      subject: "Hello ✔",
      text: "Hello world?",
      html: "<b>Hello world?</b>",
    });
    console.log("Message sent: %s", info.messageId);
    res.send(emailInfo); // 发信成功后再返回响应
  } catch (err) {
    console.error("发信失败:", err);
    res.status(500).send({error: "邮件发送失败", detail: err.message});
  }
  return emailInfo;
});

内容的提问来源于stack exchange,提问作者Brute

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 02:09:03