linux使用mail发送外部smtp邮件

转载自: http://coolnull.com/2614.html
参考:http://www.4wei.cn/archives/1001468
参考:http://blog.csdn.net/wave_1102/article/details/46463849


------------------ 我是分割线 ------------------------

说明:
linux自带的mail可以实现外部smtp发邮件。不需要本地配置postfix,sendmail邮件服务器,配置整套下来确实需要点时间。

实现:
1.关闭本机的sendmail服务或者postfix服务

点击(此处)折叠或打开

  1. sendmail
  2. # service sendmail stop
  3. # chkconfig sendmail off

  4. postfix
  5. # service postfix stop
  6. # chkconfig postfix off

2.修改/etc/mail.rc,增加一下类似N行,指定外部的smtp服务器地址、帐号密码等。

点击(此处)折叠或打开

  1. # cp /etc/mail.rc /etc/mailrc.bak.20131113
  2. # cat /etc/mail.rc //内容如下
  3. set from=123456@qq.com
  4. set smtp=smtp.qq.com
  5. set smtp-auth-user=123456
  6. set smtp-auth-password=sbsbsbsb
  7. set smtp-auth=login

3.然后通过命令来发送邮件

点击(此处)折叠或打开

  1. # echo hello word | mail -s " title" 123456@qq.com
  2. # mail -v -s "title" 123456@qq.com < 附件.zip            添加附件的方式发送邮件

RHEL6下发送邮件没有问题。但RHEL5下,一直没办法发送。这是为什么呢?可以先看下RHEL6和RHEL5下mail和mailx的不同

RHEL6下

点击(此处)折叠或打开

  1. # rpm -qa|grep mailx
  2. mailx-12.4-6.el6.x86_64
  3. # ll /bin/mail* //mail链接到mailx,mailx版本为12.4
  4. lrwxrwxrwx. 1 root root 5 7月 22 2011 /bin/mail -> mailx
  5. -rwxr-xr-x. 1 root root 390424 2月 16 2010 /bin/mailx
  6. # mail -V
  7. 12.4 7/29/08

RHEL5下,则发现了一些不同

点击(此处)折叠或打开

  1. # rpm -qa|grep mailx
  2. mailx-8.1.1-44.2.2
  3. # ll /bin/mail* //mailx链接到mail命令,mailx版本为8.1
  4. -rwxr-xr-x 1 root mail 84856 Jun 6 2007 /bin/mail
  5. lrwxrwxrwx 1 root root 4 Apr 12 18:03 /bin/mailx -> mail
  6. # mail -V 此处运行就会出错,没有-V这个参数
也就是说,在RHEL65下,运行mail和mailx,实际上都是一个命令,而mail版本不同,导致了RHEL5下mail命令无法调用外部SMTP服务器。因此安装mailx新版本可以解决此问题。

1.RHEL5安装新版本mailx

点击(此处)折叠或打开

  1. # wget ftp://ftp.spline.inf.fu-berlin.de/pub/gentoo/distfiles/mailx-12.4.tar.bz2 //下载mailx-12.4.tar.bz2,更多下载地址http://www.filewatcher.com/m/mailx-12.4.tar.bz2.271482.0.0.html
  2. # tar jxvf mailx-12.4.tar.bz2
  3. # cd mailx-12.4
  4. # make
  5. # make install UCBINSTALL=/usr/bin/install
  6. # whereis mailx //查看mailx程序默认安装位置、及版本
  7.  mailx: /usr/local/bin/mailx /usr/share/man/man1p/mailx.1p.gz
  8. # mailx -V
  9.  12.4 7/29/08

2.修改/etc/nail.rc,安装后的mailx使用的是/etc/nail.rc而不是/etc/mail.rc。这个需要注意下!!!

点击(此处)折叠或打开

  1. # vi /etc/nail.rc //在行尾添加如下行
  2. set from=123456@qq.com
  3. set smtp=smtp.qq.com
  4. set smtp-auth-user=123456
  5. set smtp-auth-password=sbsbsbsb
  6. set smtp-auth=login

3.然后通过命令来发送邮件

点击(此处)折叠或打开

  1. # echo hello word | mailx -v -s " title" 123456@qq.com //成功发送的话,就可以看到类似的提示
  2.  235 2.7.0 Authentication successful
  3.  >>> MAIL FROM:<demo@qq.com>
  4.  250 2.1.0 Ok
  5.  >>> RCPT TO:<xxxxx@qq.com>
  6.  250 2.1.5 Ok
  7.  >>> DATA
  8.  354 End data with <cr><lf>.<cr><lf>
  9.  >>> .
  10.  250 2.0.0 Ok: queued as 9291963494
  11.  >>> QUIT
  12.  221 2.0.0 Bye

亲测163邮箱可以正常使用,qq邮箱需要设置SSL。





请使用浏览器的分享功能分享到微信等