企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
**1. 安装插件 Email Extension Template、Email Extension** **2. 开启邮箱的 POP3/SMTP服务** 在这里我使用 163邮箱作为Jenkins的系统邮箱,即发件邮箱。发件邮箱需要开启 POP3/SMTP服务。下面为开启 163邮箱的 POP3/SMTP服务 的步骤。 (1)登录网易邮箱:https://mail.163.com/ 。 (2)开启 POP3/SMTP服务 服务。 ![](https://img.kancloud.cn/40/7a/407a72be4cb1c0f7561d4827d4469778_1439x392.jpg) ![](https://img.kancloud.cn/d3/69/d3690f8c69afb31f00edcc5c2193fb0f_1393x407.jpg) (3)开启后会有一个授权码,记得复制并保存下来。 **3. Jenkins设置邮箱相关参数** (1)系统管理 -> 系统配置 -> Jenkins Location。 ![](https://img.kancloud.cn/a9/b2/a9b2540319142b4fbe3600a2487ca6ff_1298x254.jpg) (2)配置 Extended E-mail Notification。 ![](https://img.kancloud.cn/27/91/27918ad291ae6da6a0814a7dac6ea50e_1344x425.jpg) ![](https://img.kancloud.cn/5c/dd/5cdd980052e8af650e5a6ae4e83da98e_1400x568.jpg) ![](https://img.kancloud.cn/7e/1f/7e1fb8986f4c4051eb4a5685c8b57b79_1466x360.jpg) ![](https://img.kancloud.cn/8e/a9/8ea94d37b20eb69b2e1181ec69d00623_1451x405.jpg) ``` ${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS} ! ${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS} ! Check console output at $BUILD_URL to view the results. ``` (3)配置邮件 邮件通知。 ![](https://img.kancloud.cn/bc/da/bcda577924a709fea2f8f416ded08e7c_1470x567.jpg) ![](https://img.kancloud.cn/be/81/be811aca04284f34426b6ec2f5492a4c_1450x296.jpg) (4)测试是否配置成功。 ![](https://img.kancloud.cn/b0/4b/b04b2d105ce63e097738daeb09bc15f1_1490x221.jpg) **4. 编写邮件模板** ![](https://img.kancloud.cn/8b/5e/8b5ebf0a4c7af895a04db4904cb7abf0_1218x291.jpg) `src/main/resources/email.html` ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>${ENV, var="JOB_NAME"}-第${BUILD_NUMBER}次构建日志</title> </head> <body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4" offset="0"> <table width="95%" cellpadding="0" cellspacing="0" style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif"> <tr> <td>(本邮件是程序自动下发的,请勿回复!)</td> </tr> <tr> <td> <h2> <font color="#0000FF">构建结果 - ${BUILD_STATUS}</font> </h2> </td> </tr> <tr> <td><br /> <b> <font color="#0B610B">构建信息</font> </b> <hr size="2" width="100%" align="center" /> </td> </tr> <tr> <td> <ul> <li>项目名称&nbsp;:&nbsp;${PROJECT_NAME}</li> <li>构建编号&nbsp;:&nbsp;第${BUILD_NUMBER}次构建</li> <li>触发原因:&nbsp;${CAUSE}</li> <li>构建日志:&nbsp;<a href="${BUILD_URL}console">${BUILD_URL}console</a></li> <li>构建&nbsp;&nbsp;Url&nbsp;:&nbsp;<a href="${BUILD_URL}">${BUILD_URL}</a></li> <li>工作目录&nbsp;:&nbsp;<a href="${PROJECT_URL}ws">${PROJECT_URL}ws</a></li> <li>项目&nbsp;&nbsp;Url&nbsp;:&nbsp;<a href="${PROJECT_URL}">${PROJECT_URL}</a></li> </ul> </td> </tr> <tr> <td><b> <font color="#0B610B">Changes Since Last Successful Build:</font> </b> <hr size="2" width="100%" align="center" /> </td> </tr> <tr> <td> <ul> <li>历史变更记录 : <a href="${PROJECT_URL}changes">${PROJECT_URL}changes</a></li> </ul> ${CHANGES_SINCE_LAST_SUCCESS,reverse=true, format="Changes for Build #%n:<br />%c<br />",showPaths=true,changesFormat=" <pre>[%a]<br/>%m</pre> ",pathFormat="&nbsp;&nbsp;&nbsp;&nbsp;%p"} </td> </tr> <tr> <td><b>Failed Test Results</b> <hr size="2" width="100%" align="center" /> </td> </tr> <tr> <td> <pre style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">$FAILED_TESTS</pre> <br /></td> </tr> <tr> <td><b> <font color="#0B610B">构建日志 (最后 100行):</font> </b> <hr size="2" width="100%" align="center" /> </td> </tr> <tr> <td><textarea cols="80" rows="30" readonly="readonly" style="font-family: Courier New">${BUILD_LOG, maxLines=100}</textarea> </td> </tr> </table> </body> </html> ``` **5. 编写脚本发送邮件** ![](https://img.kancloud.cn/66/9b/669bf59639cc449a965975be795e2f47_1173x306.jpg) `src/main/resources/jenkinsfile` ```groovy pipeline { agent any stages { ... } post { always { //1. 邮件内容指定 email.html 模板 emailext body: '${FILE,path="src/main/resources/email.html"}', //2. 这个是邮件的标题 subject: '构建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS} !', //3. 这个是邮件的接收者,多个邮件用 , 分割 to: 'xxb@163.com,xx010@qq.com' } } } ``` 修改后记得上传到 gitlab 上。 **6. 构建后两个邮件收到的信息如下** ![](https://img.kancloud.cn/5e/a2/5ea2ce2f47c21d8f50eeaba62acaa1bb_1388x333.jpg)