返回列表 发帖

Python3使用126邮箱的25端口发送邮件

操作系统的版本信息:
[root@centos6 ~]# cat /etc/redhat-release
CentOS release 6.9 (Final)
[root@centos6 ~]#
[root@centos6 ~]# uname -r
2.6.32-696.el6.x86_64

Python的版本信息:
[root@centos6 ~]# python3 --version
Python 3.6.8



发送邮件的脚本内容:
[root@centos6 ~]# cat xx.py
#coding=utf-8

import smtplib #以下3个模块都是内置的,不用额外安装
from email.mime.text import MIMEText
from email.utils import formataddr

my_sender = 'j13680432782@126.com' #发件人邮箱账号
my_pass = 'Jackyxxxx' #发件人邮箱密码
my_name = 'zhuohua' #发件人邮箱昵称

receiver = '2270168881@qq.com' #收件人邮箱账号
receiver_name = 'Python' #收件人昵称


def func_mail():

        try:
                Message_1 = MIMEText('邮件正文内容,Welcome to zhuohua.', 'plain', 'utf-8') #邮件的正文内容
                Message_1['From'] = formataddr([my_name,my_sender])
                Message_1['To'] = formataddr([receiver_name,receiver])
                Message_1['Subject'] = "126邮箱的25端口测试邮件" #邮件的主题,也可以说是标题

                server = smtplib.SMTP()
               
                server.connect("smtp.126.com",25) #发件人邮箱的SMTP服务器,端口是TCP 25       
                server.login(my_sender, my_pass)
                server.sendmail(my_sender,[receiver,],Message_1.as_string())
                server.quit() #关闭连接
               
        except Exception as e:
                print (f"发送邮件失败,原因: {e}")
               
        else:
                print("邮件发送成功")


if __name__ == '__main__':

        func_mail()


设置脚本权限:
[root@centos6 ~]# chmod a+x xx.py


脚本运行的结果:
[root@centos6 ~]# python3 xx.py
邮件发送成功
[root@centos6 ~]#


QQ邮箱收到的邮件:(收件人邮箱的“收件人”使用本身的昵称)
图片1.png
2022-3-19 21:27



126邮箱发送这种邮件后,在“已发送”里是有记录的:(发件人邮箱的“收件人”使用自定义的昵称)
图片2.png
2022-3-19 21:27






相关文章:
Python3使用新浪邮箱的25端口发送邮件
Python3使用126邮箱的465端口发送邮件

Win7/Windows2008R2安装Python3

CentOS8使用mailx(使用126邮箱的25端口)
CentOS8_通过脚本发送Zabbix告警邮件(使用126邮箱的25端口)





#################################
#################################
亲,学习研究也要劳逸结合哦,来我微店逛逛,买点东西好好犒劳犒劳自己和家人吧^_^^_^

FaSoLa免磨陶瓷刀具 抗氧化厨房切菜刀水果刀切肉刀 寿命长
dao.png
2019-10-2 09:53


【宝家洁】喷雾喷水拖把平板拖把大号 高档木地板除尘 不用手洗的创意拖把 懒人必备
tuoba.png
2019-10-2 09:53


薇语馨 (促销价)6833#夏季新款收腰显瘦蝙蝠袖露肩复古雪纺连衣裙波浪裙子邻家女孩 好质量 支持无理由退货
duoyi.png
2019-10-2 09:53


薇语馨1852真丝连衣裙套装裙两件套系带宽松大码不规则裙子女
danyi.png
2019-10-2 09:53

返回列表