原因
可能是阿里云 把 你使用的那个端口封了
网上找到的解决办法:
修改连接部分代码
smtplib.SMTP_SSL(mail_host, 465)
# _*_coding:utf-8 _*_
#邮件服务封装
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
# 邮件预警系统相关信息
MAIL_CONFIG = {
'sender_email':'***', #发送预警邮件的邮箱账号
'sender_password':'***', #邮箱授权码
'receive_email':'*******@qq.com', #要接收邮件的地址
'mail_title':'salve端-异常预警', #邮件标题
}
class EmailHandler(object):
def __init__(self,sender_email,sender_password, type = 0):
"""
:param sender_email:str 发送人邮箱地址(用户名)
:param sender_password:str 发送人在QQ或163申请的授权码
:param type:int 1 为QQ邮箱 0 为163邮箱
"""
self.__163 = {'smtp': 'smtp.163.com', 'port': 465}
self.__QQ = {'smtp':'smtp.qq.com','port':465} # 阿里云把 25 端口屏蔽了
self.sender_email = sender_email
self.sender_password = sender_password
if type == 0:
self.server=smtplib.SMTP_SSL(self.__163['smtp'],self.__163['port'])
self.server.login (self.sender_email,self.sender_password)
elif type == 1:
self.server=smtplib.SMTP_SSL(self.__QQ['smtp'],self.__QQ['port'])
self.server.login (self.sender_email,self.sender_password)
def send_mail(self,To,subject,content):
"""
:param To:str 接收人邮箱地址
:param subject:str 邮件标题
:param content:str 邮件内容
:return:bool True 成功 False 失败
"""
try:
msg = MIMEText(content,'plain','utf-8')
msg['From'] = formataddr(['淘宝爬虫异常报警系统',self.sender_email])
msg['To'] = formataddr(['',To])
msg['Subject'] = subject
self.server.sendmail(self.sender_email,To,msg.as_string())
print("【%s】邮件发送成功"%subject)
return True
except Exception as e:
print("【%s】邮件发送失败,请检查信息:%s"%(subject,e))
return False
emailer = EmailHandler(MAIL_CONFIG['sender_email'], MAIL_CONFIG['sender_password'])
if __name__ == '__main__':
emailer.send_mail(MAIL_CONFIG['receive_email'],MAIL_CONFIG['mail_title'], "恭喜你被阿里巴巴录取了,hhh")