欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python使用网易邮箱服务发邮件报554

程序员文章站 2022-06-09 22:49:51
错误信息(554, b’DT:SPM 163 smtp14,EsCowACnnOp_kdBflbfuIg–.5619S2 1607504256,please see http://mail.163.com/help/help_spam_16.htm?ip=61.140.181.56&hostid=smtp14&time=1607504256’)出错代码:import osimport smtplibimport base64from email.mime.text import...

错误信息

(554, b’DT:SPM 163 smtp14,EsCowACnnOp_kdBflbfuIg–.5619S2 1607504256,please see http://mail.163.com/help/help_spam_16.htm?ip=61.140.181.56&hostid=smtp14&time=1607504256’)

出错代码:

import os
import smtplib
import base64
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart


class SendEmail(object):
    def __init__(self, username, passwd, sender, recv, title, content,
                 file=None, ssl=False, email_host='smtp.163.com',
                 port=25, ssl_port=465):
        """
        :param username: 用户名
        :param passwd: 密码
        :param recv: 收件人,多个收件人传list  ['a@qq.com','b@qq.com]
        :param title: 邮件标题
        :param content: 邮件正文
        :param file:  附件路径,如果不在当前目录下,要写绝对路径
        :param ssl: 是否安全链接
        :param email_host: smtp服务器地址
        :param port: 普通端口
        :param ssl_port: 安全链接端口
        """
        self.username = username
        self.passwd = passwd
        self.sender = sender
        self.recv = recv
        self.title = title
        self.content = content
        self.file = file
        self.email_host = email_host
        self.port = port
        self.ssl = ssl
        self.ssl_port = ssl_port

    def send_email(self):
        msg = MIMEMultipart()
        # 发送邮件对象
        if self.file:
            file_name = os.path.split(self.file)[-1]   # 只取文件名,不取路径
            try:
                f = open(self.file, 'rb').read()
            except Exception as e:
                raise Exception('附件打不开!!!')
            else:
                att = MIMEText(f, "base64", "utf-8")
                att["Content-Type"] = 'application/octet-stream'
                new_file_name = '=?utf-8?b?' + base64.b64encode(file_name.encode()).decode() + '?='
                att["Content-Type"] = 'attachment; filename="%s"' % new_file_name
                msg.attach(att)

        msg.attach(MIMEText(self.content))    # 邮件正文的内容
        msg["Subject"] = self.title       # 邮件主题
        msg['From'] = self.sender    # 发送者账号
        msg['To'] = ','.join(self.recv)   # 接收者账号列表

        if self.ssl:
            self.smtp = smtplib.SMTP_SSL(self.email_host, port=self.ssl_port)
        else:
            self.smtp = smtplib.SMTP(self.email_host, port=self.port)

        try:
            self.smtp.login(self.username, self.passwd)
            self.smtp.sendmail(self.username, self.recv, msg.as_string())
            print('成功了。。')
        except Exception as e:
            print('出错了..', e)


if __name__ == '__main__':
    m = SendEmail(
        username='17746506075@163.com',
        passwd='FJRMTAWBWHWVWURK',
        sender='17746506075@163.com',
        recv=['1401230275@qq.com'],
        title='123',
        content='测试邮件发送',
        file=r'F:\图片\abc.jpg',
        ssl=True
    )
    m.send_email()
运行结果
	D:\自学python\python基础\API_Test\venv\Scripts\python.exe D:/自学python/python基础/API_Test/common/send_email.py
	出错了.. (554, b'DT:SPM 163 smtp14,EsCowACnnOp_kdBflbfuIg--.5619S2 1607504256,please see http://mail.163.com/help/help_spam_16.htm?ip=61.140.181.56&hostid=smtp14&time=1607504256')
	
	Process finished with exit code 0

原 因是:

原来这时因为网易将我发的邮件当成了垃圾邮件!

解决办法是:这时候你只要在发邮件的时候抄送上自己,就再也不会报这个错误了!

import os
import smtplib
import base64
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart


class SendEmail(object):
    def __init__(self, username, passwd, sender, recv, title, content,
                 file=None, ssl=False, email_host='smtp.163.com',
                 port=25, ssl_port=465):
        """
        :param username: 用户名
        :param passwd: 密码
        :param recv: 收件人,多个收件人传list  ['a@qq.com','b@qq.com]
        :param title: 邮件标题
        :param content: 邮件正文
        :param file:  附件路径,如果不在当前目录下,要写绝对路径
        :param ssl: 是否安全链接
        :param email_host: smtp服务器地址
        :param port: 普通端口
        :param ssl_port: 安全链接端口
        """
        self.username = username
        self.passwd = passwd
        self.sender = sender
        self.recv = recv
        self.title = title
        self.content = content
        self.file = file
        self.email_host = email_host
        self.port = port
        self.ssl = ssl
        self.ssl_port = ssl_port

    def send_email(self):
        msg = MIMEMultipart()
        # 发送邮件对象
        if self.file:
            file_name = os.path.split(self.file)[-1]   # 只取文件名,不取路径
            try:
                f = open(self.file, 'rb').read()
            except Exception as e:
                raise Exception('附件打不开!!!')
            else:
                att = MIMEText(f, "base64", "utf-8")
                att["Content-Type"] = 'application/octet-stream'
                new_file_name = '=?utf-8?b?' + base64.b64encode(file_name.encode()).decode() + '?='
                att["Content-Type"] = 'attachment; filename="%s"' % new_file_name
                msg.attach(att)

        msg.attach(MIMEText(self.content))    # 邮件正文的内容
        msg["Subject"] = self.title       # 邮件主题
        msg['From'] = self.sender    # 发送者账号
        msg['To'] = ','.join(self.recv)   # 接收者账号列表

        if self.ssl:
            self.smtp = smtplib.SMTP_SSL(self.email_host, port=self.ssl_port)
        else:
            self.smtp = smtplib.SMTP(self.email_host, port=self.port)

        try:
            self.smtp.login(self.username, self.passwd)
            self.smtp.sendmail(self.username, self.recv, msg.as_string())
            print('成功了。。')
        except Exception as e:
            print('出错了..', e)


if __name__ == '__main__':
    m = SendEmail(
        username='17746506075@163.com',
        passwd='FJRMTAWBWHWVWURK',
        sender='17746506075@163.com',
        recv=['1401230275@qq.com', '17746506075@163.com'],
        title='123',
        content='测试邮件发送',
        file=r'F:\图片\abc.jpg',
        ssl=True
    )
    m.send_email()
运行结果为:
D:\自学python\python基础\API_Test\venv\Scripts\python.exe D:/自学python/python基础/API_Test/common/send_email.py
成功了。。

Process finished with exit code 0

本文地址:https://blog.csdn.net/xiaolipanpan/article/details/110930222