【python】smtp发送邮件(账号密码+账号授权码说明) | 您所在的位置:网站首页 › qq邮箱密码和qq密码是一样的吗 › 【python】smtp发送邮件(账号密码+账号授权码说明) |
0 安装库+引用库
安装代码 pip install PyEmail引用库 import smtplib from email.mime.text import MIMEText from email.header import Header 1 单接收者 # 第三方 SMTP 服务 mail_host="smtp服务器地址" #设置服务器 mail_user="[email protected]" #用户名 mail_pass="xxx" #密码、授权码 sender = '[email protected]' #发送邮箱 receivers = ['[email protected]'] #接收邮箱 #邮件主题,邮件内容 def send_mail(title,message): # 三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码 message = MIMEText(message, 'plain', 'utf-8') message['From'] = Header('[email protected]', 'utf-8') # 发送者 message['To'] = Header('[email protected]', 'utf-8') # 接收者 message['Subject'] = Header(title, 'utf-8') smtpObj = smtplib.SMTP_SSL(mail_host, 465) smtpObj.login(mail_user,mail_pass) smtpObj.sendmail(sender, receivers, message.as_string()) send_mail('【测试】邮件发送测试','邮件发送测试') 2 多接收者 # 第三方 SMTP 服务 mail_host="smtp服务器地址" #设置服务器 mail_user="[email protected]" #用户名 mail_pass="xxx" #密码、授权码 sender = '[email protected]' #发送邮箱 receivers = ['[email protected]','[email protected]'] #接收邮箱 #邮件主题,邮件内容 def send_mail(title,message): # 三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码 message = MIMEText(message, 'plain', 'utf-8') message['From'] = Header('[email protected]', 'utf-8') # 发送者 message['To'] = Header('[email protected], [email protected]', 'utf-8') # 接收者 message['Subject'] = Header(title, 'utf-8') smtpObj = smtplib.SMTP_SSL(mail_host, 465) smtpObj.login(mail_user,mail_pass) smtpObj.sendmail(sender, receivers, message.as_string()) send_mail('【测试】邮件发送测试','邮件发送测试') 3 说明 3.1 密码?授权码?根据smtp服务器的要求,login中的mail_pass可能是账号密码,也可能是smtp授权码。 smtp授权码需要在邮件客户端,用户绑定手机号码自行开启。 例如: smtp.163.com 要求 授权码 smtp.qiye.163.com 要求 账号对应密码 3.2 smtp端口以163邮箱为例:163免费邮客户端设置的POP3、SMTP、IMAP地址 465、994端口号使用SSL协议,需要调用 smtplib.SMTP_SSL(mail_host, 465) 25端口号使用非SSL协议,需要调用 smtpObj = smtplib.SMTP() smtpObj.connect(mail_host, 25) SSL加密,保密性高 |
CopyRight 2018-2019 实验室设备网 版权所有 |