返回列表 发帖

Python3使用新浪邮箱的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 = 'j2270168881@sina.com' #发件人邮箱账号
my_pass = '9157d00e9886890e' #发件人邮箱的授权码;新浪邮箱要使用授权码
my_name = 'zhuohua' #发件人邮箱昵称

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


def func_mail():

        try:
                Key_1 = '邮件正文内容,Welcome to zhuohua.' #邮件的正文内容
                Message_1 = MIMEText(Key_1, 'plain', 'utf-8')

                Message_1['From'] = formataddr([my_name,my_sender])
                Message_1['To'] = formataddr([receiver_name,receiver])
                Message_1['Subject'] = "新浪邮箱的25端口测试邮件" #邮件的主题
                server = smtplib.SMTP()
               
                server.connect("smtp.sina.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
发送邮件失败,原因: (535, b'5.7.8 authentication failed')
[root@centos6 ~]#


脚本运行的结果:(一切都正常时)
[root@centos6 ~]# python3 xx.py
邮件发送成功
[root@centos6 ~]#

QQ邮箱收到的邮件:
图片2.png

备注:新浪邮箱发送这种邮件后,在“已发送”里是没有记录的。





########

例子二:(发送带附件的邮件)
[root@centos6 ~]# cat yy.py
#coding=utf-8

import smtplib #以下4个模块都是内置的,不用额外安装
from email.mime.text import MIMEText
from email.utils import formataddr
from email.mime.multipart import MIMEMultipart #发送附件的模块

my_sender = 'j2270168881@sina.com' #发件人邮箱账号
my_pass = '9157d00e9886890e' #发件人邮箱的授权码
my_name = 'zhuohua_1' #发件人邮箱昵称

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


def func_mail():

        try:
       
                #创建一个带附件的实例
                Message_1 = MIMEMultipart()

                #构造附件1 (对应文件/test1.txt
                att1 = MIMEText(open('/test1.txt', 'rb').read(), 'base64', 'utf-8')
                att1["Content-Type"] = 'application/octet-stream'
                att1["Content-Disposition"] = 'attachment; filename="test1.txt"' #附件名称不包含中文时的写法;
                Message_1.attach(att1)

                #构造附件2 (对应文件/test2.log
                att2 = MIMEText(open('/test2.log', 'rb').read(), 'base64', 'utf-8')
                att2["Content-Type"] = 'application/octet-stream'
                att2.add_header("Content-Disposition","attachment",filename=("utf-8","","日志文件_test2.log")) #附件名称包含中文时的写法;附件的名称可以跟原文件的不一样
                Message_1.attach(att2)

               
                Key_1 = '邮件正文内容,Welcome to zhuohua.' #邮件的正文内容
                Message_1.attach(MIMEText(Key_1, 'plain', 'utf-8'))
               
                Message_1['From'] = formataddr([my_name,my_sender])
                Message_1['To'] = formataddr([receiver_name,receiver])
                Message_1['Subject'] = "新浪邮箱的25端口测试邮件+附件" #邮件的主题


                server = smtplib.SMTP()
                server.connect("smtp.sina.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 yy.py


脚本运行的结果:(其中一个附件不存在时,邮件将无法发送)
[root@centos6 ~]# python3 yy.py
发送邮件失败,原因: [Errno 2] No such file or directory: '/test1.txt'
[root@centos6 ~]#


脚本运行的结果:(一切都正常时)
[root@centos6 ~]# python3 yy.py
邮件发送成功
[root@centos6 ~]#

QQ邮箱收到的邮件:
图片3.png

备注:附件可以正常下载、打开;附件的内容可以包含中文。





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

Win10安装Python3
Windows2012R2安装Python3

CentOS6使用mailx(使用新浪邮箱的25端口)
使用新浪邮箱的465端口发送邮件报错(535 authentication failed)

返回列表