You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
slms/docs/JENKINS_EMAIL_SETUP.md

7.5 KiB

Jenkins 邮件通知配置指南

前提条件

当前 Jenkinsfile 使用 emailext 插件发送邮件,需要完成以下配置才能正常工作。

一、安装必要插件

1. 进入插件管理

访问:http://localhost:8084/pluginManager/

2. 安装以下插件

  • Email Extension Plugin (必需)
  • Mailer Plugin (通常已安装)

安装步骤:

  1. 点击 "Available plugins"(可选插件)
  2. 搜索 "Email Extension"
  3. 勾选插件
  4. 点击 "Install without restart"(安装但不重启)
  5. 等待安装完成

二、配置 SMTP 服务器

1. 进入系统配置

访问:http://localhost:8084/configure

2. 配置 Jenkins Location

找到 Jenkins Location 部分:

  • Jenkins URL: http://localhost:8084/
  • System Admin e-mail address: jenkins@slms.local 或你的邮箱

3. 配置 Extended E-mail Notification

找到 Extended E-mail Notification 部分:

使用 QQ 邮箱示例

SMTP server: smtp.qq.com
SMTP port: 465

高级设置(点击 Advanced

  • Use SMTP Authentication: 勾选
    • User Name: 你的QQ邮箱123456789@qq.com
    • Password: QQ邮箱授权码不是QQ密码
  • Use SSL: 勾选
  • SMTP port: 465
  • Charset: UTF-8
  • Default Content Type: HTML (text/html)

使用 163 邮箱示例

SMTP server: smtp.163.com
SMTP port: 465

高级设置:

  • Use SMTP Authentication: 勾选
    • User Name: 你的163邮箱
    • Password: 163邮箱授权码
  • Use SSL: 勾选
  • SMTP port: 465

使用 Gmail 示例

SMTP server: smtp.gmail.com
SMTP port: 587

高级设置:

  • Use SMTP Authentication: 勾选
    • User Name: 你的Gmail地址
    • Password: Gmail应用专用密码
  • Use TLS: 勾选
  • SMTP port: 587

4. 配置默认收件人

Extended E-mail Notification 部分:

  • Default Recipients: 填写收件人邮箱,多个用逗号分隔
    • 例如:user1@example.com, user2@example.com

5. 配置邮件触发器

Default Triggers 中勾选:

  • Failure - Any(构建失败)
  • Success(构建成功)
  • Unstable - Any(构建不稳定)

6. 测试邮件配置

点击 Test configuration by sending test e-mail

  • 输入测试邮箱地址
  • 点击 "Test configuration"
  • 检查是否收到测试邮件

7. 保存配置

点击页面底部的 Save 按钮

三、获取邮箱授权码

QQ 邮箱授权码获取步骤

  1. 登录 QQ 邮箱:https://mail.qq.com
  2. 点击 设置账户
  3. 找到 POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务
  4. 开启 POP3/SMTP服务IMAP/SMTP服务
  5. 点击 生成授权码
  6. 按提示发送短信验证
  7. 获得16位授权码Cr7Bd5wKFjuFb$Z@
  8. 这个授权码就是 Jenkins 中的密码

163 邮箱授权码获取步骤

  1. 登录 163 邮箱:https://mail.163.com
  2. 点击 设置POP3/SMTP/IMAP
  3. 开启 IMAP/SMTP服务
  4. 点击 客户端授权密码
  5. 按提示设置授权码
  6. 获得授权码用于 Jenkins

Gmail 应用专用密码获取步骤

  1. 访问:https://myaccount.google.com/security
  2. 开启 两步验证
  3. 进入 应用专用密码
  4. 选择应用:邮件
  5. 选择设备:Windows 计算机
  6. 生成密码
  7. 使用生成的16位密码

四、修改 Jenkinsfile可选

当前配置

emailext (
    subject: "✅ SLMS 构建成功 - Build #${BUILD_NUMBER}",
    body: """...""",
    to: '${DEFAULT_RECIPIENTS}',  // 使用系统默认收件人
    mimeType: 'text/html'
)

指定收件人(推荐)

如果想在 Jenkinsfile 中指定收件人,修改为:

emailext (
    subject: "✅ SLMS 构建成功 - Build #${BUILD_NUMBER}",
    body: """...""",
    to: 'your-email@example.com',  // 指定收件人
    mimeType: 'text/html'
)

多个收件人

to: 'user1@example.com, user2@example.com, user3@example.com'

添加抄送和密送

emailext (
    subject: "✅ SLMS 构建成功 - Build #${BUILD_NUMBER}",
    body: """...""",
    to: 'user1@example.com',
    cc: 'user2@example.com',      // 抄送
    bcc: 'user3@example.com',     // 密送
    mimeType: 'text/html'
)

五、验证邮件功能

1. 触发构建

推送代码到 Gitea 触发 Jenkins 构建

2. 查看构建日志

在 Jenkins 构建日志中查找:

========== 发送成功通知邮件 ==========
✓ 成功通知邮件已发送

3. 检查邮箱

查看是否收到构建通知邮件

六、常见问题排查

问题 1: 邮件发送失败 - 认证失败

原因: 密码错误或未使用授权码

解决:

  • 确认使用的是授权码,不是邮箱登录密码
  • 重新生成授权码
  • 检查用户名是否正确(完整邮箱地址)

问题 2: 连接超时

原因: 端口或 SSL/TLS 配置错误

解决:

  • QQ/163 邮箱使用 SSL端口 465
  • Gmail 使用 TLS端口 587
  • 检查防火墙是否阻止 SMTP 端口

问题 3: 收不到邮件

原因: 邮件被拦截或进入垃圾箱

解决:

  • 检查垃圾邮件文件夹
  • 将 Jenkins 邮箱地址加入白名单
  • 检查邮箱是否有拦截规则

问题 4: 邮件内容乱码

原因: 字符集配置错误

解决:

  • 在 Extended E-mail Notification 中设置 Charset: UTF-8
  • 确认 Default Content Type: HTML (text/html)

问题 5: 测试邮件成功但构建邮件失败

原因: Jenkinsfile 中收件人配置错误

解决:

  • 检查 to: '${DEFAULT_RECIPIENTS}' 是否正确
  • 或直接指定收件人邮箱地址
  • 查看 Jenkins 系统日志:http://localhost:8084/log/all

七、推荐配置

最简单的配置QQ 邮箱)

  1. SMTP 服务器: smtp.qq.com
  2. SMTP 端口: 465
  3. 用户名: 你的QQ邮箱
  4. 密码: QQ邮箱授权码
  5. Use SSL:
  6. Default Recipients: 收件人邮箱

Jenkinsfile 修改

to: 'your-qq-email@qq.com'  // 替换为你的邮箱

八、测试命令

在 Jenkins Script Console 中测试

访问:http://localhost:8084/script

运行以下脚本测试邮件:

import javax.mail.*
import javax.mail.internet.*

def props = new Properties()
props.put("mail.smtp.host", "smtp.qq.com")
props.put("mail.smtp.port", "465")
props.put("mail.smtp.auth", "true")
props.put("mail.smtp.ssl.enable", "true")

def session = Session.getInstance(props, new Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("your-email@qq.com", "your-auth-code")
    }
})

def message = new MimeMessage(session)
message.setFrom(new InternetAddress("your-email@qq.com"))
message.addRecipient(Message.RecipientType.TO, new InternetAddress("recipient@example.com"))
message.setSubject("Jenkins 邮件测试")
message.setText("这是一封测试邮件")

Transport.send(message)
println "邮件发送成功!"

九、当前 Jenkinsfile 状态

已配置邮件通知

  • 构建成功时发送
  • 构建失败时发送
  • 构建不稳定时发送

⚠️ 需要完成上述配置才能正常工作

十、快速配置检查清单

  • 安装 Email Extension Plugin
  • 配置 SMTP 服务器和端口
  • 配置 SMTP 认证(用户名和授权码)
  • 配置 SSL/TLS
  • 设置默认收件人
  • 测试邮件配置
  • 保存配置
  • 触发构建验证

完成以上步骤后Jenkins 就能正常发送邮件通知了!