应用jenkins发送邮件的邮件服务器 SMTP server 可以是本地的,也可以是远程,远程的就不多说了,在此说一下如何在jenkins本地服务器上搭建邮件服务器,并用jenkins发送邮件,详细过程如下:

一、linux服务器搭建邮件服务器:

  这里选择用linux的sendmail 来搭建邮件服务器,步骤如下:

   1、工具准备:

  安装sendmail和配置工具sendmail-cf

  检查是否已安装:rpm -qa grep sendmail*

  jenkins调用本地搭建sendmail邮件服务器发送邮件-LMLPHP

 如果有结果,则说明已经安装了,没结果则安装一下:

  sudo yum install mailx -y

  sudo yum -y install sendmail

  sudo yum install -y sendmail-cf

  继续安装SMTP认证服务(如果需要用外部SMTP服务的话,这里我要使用的是本地邮件服务器,所以没有用到这个)
  sudo yum install -y saslauthd

  2、配置sendmail:

  sudo vim /etc/mail/sendmail.mc

  找到DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA')dnl 这一行,把它修改为:

  DAEMON_OPTIONS(`Port=smtp,Addr=0.0.0.0, Name=MTA')dnl

  3、生成Sendmail的配置文件:

  m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf

  如果报错:sendmail.mc:10: m4: cannot open `/usr/share/sendmail-cf/m4/cf.m4′: No such file or directory

  则说明 sendmail-cf没安装成功,重新安装一下:

  sudo yum install -y sendmail-cf

  4、重启sendmail服务:

  sudo service sendmail restart

  如果报错:sendmail:451 4.0.0 /etc/mail/sendmail.cf: line 91: fileclass: cannot open '/etc/mail/local-host-names': Group writable directory 
            451 4.0.0 /etc/mail/sendmail.cf: line 588: fileclass: cannot open '/etc/mail/trusted-users': Group writable directory

  或者Starting sendmail: 451 4.0.0 /etc/mail/sendmail.cf: line 91: fileclass: cannot open '/etc/mail/local-host-names': World writable directory
             451 4.0.0 /etc/mail/sendmail.cf: line 588: fileclass: cannot open '/etc/mail/trusted-users': World writable derectory [FAILED]

这样的,那么就是文件权限问题导致的,处理办法如下:

  检查一下文件权限:

  ls -ld / /etc /etc/mail /usr /var /var/spool /var/spool/mqueue

  dr-xr-xr-x. 23 root root 4096 Aug 31 02:55 /
  drwxr-xr-x. 93 root root 4096 Oct 11 18:31 /etc
  drwxr-xr-x 2 root root 4096 Oct 11 19:49 /etc/mail
  drwxr-xr-x. 15 root root 4096 Aug 31 16:03 /usr
  drwxr-xr-x. 18 root root 4096 Aug 31 02:55 /var
  drwxr-xr-x. 13 root root 4096 Oct 11 16:28 /var/spool
  drwx------ 2 root mail 4096 Oct 12 14:04 /var/spool/mqueue

  修改/etc/mail/sendmail.cf 文件中以下内容:

  sudo  vim /etc/mail/sendmail.cf

  Fw/etc/mail/local-host-names  --->改成:Fw-o /etc/mail/local-host-names

  Ft/etc/mail/trusted-users   --->改成:Ft-o /etc/mail/trusted-users

  修改/etc/mail/submit.cf文件内容:

  sudo vim /etc/mail/submit.cf

  Ft/etc/mail/trusted-users --->改成:Ft-o /etc/mail/trusted-users

  修改完成后再次重启:

  sudo /etc/init.d/sendmail restart

  启动成功后查看一下运行状态:

  sudo /etc/rc.d/init.d/sendmail status

  sendmail (pid 21921) is running...

  sm-client (pid 21930) is running...

  成功运行,接下来可以先发送一个测试邮件

  5.发送测试邮件:

  mail -s "test" [email protected] < /home/opt/welcom.txt

  这样可以把/home/opt/welcom.txt文件里的内容作为邮件正文,test 作为邮件标题发送到邮箱:[email protected]

  OK,至此,邮件服务器搭建成功,接下来在jenkins里调用

二、jenkins调用本地sendmail邮件服务器发送邮件

  上诉搭建的sendmail邮件服务器跟jenkins是一台机子,jenkins里设置如下:

  这就简单了,只需要配置两个地方即可

  系统配置里:

  jenkins调用本地搭建sendmail邮件服务器发送邮件-LMLPHP

jenkins调用本地搭建sendmail邮件服务器发送邮件-LMLPHP

这两个地方设置一下,然后测试发送邮件,成功

【本地邮件服务器不需要再配SMTP认证】

04-16 07:47