1.利用163邮箱发送邮件
# _*_ coding:UTF-8 import time import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart def sentemail(): caodate = str(time.strftime('%Y-%m-%d', time.localtime())) # 设置发件服务器地址 host = 'smtp.163.com' # 设置发件服务器端口号。注意,这里有SSL和非SSL两种形式 port = 465 # 设置发件邮箱,一定要自己注册的邮箱 sender = '17682303516@163.com' # 设置发件邮箱的密码,163邮箱的授权码,等会登陆会用到 pwd = 'yu17682303516' # 设置邮件接收人,可以是扣扣邮箱 receiver0 = '1064145110@qq.com' # receiver1 = '54400407@qq.com' # 设置邮件正文,这里是支持HTML的 body = '<h1>' + caodate + '</h1>' # 设置正文为符合邮件格式的HTML内容 msg = MIMEText(body, 'html') message = MIMEMultipart() # 设置邮件标题 message['subject'] = 'kaixinliao通知' # 设置发送人 message['from'] = sender # 设置接收人 message['to'] = receiver0 message.attach(msg) # # 构造附件1,传送当前目录下的 filename 文件 # filename = 'xfurlwett-' + caodate + '.txt' # att1 = MIMEText(open(filename, 'rb').read(), 'base64', 'utf-8') # att1["Content-Type"] = 'application/octet-stream' # # 这里的filename可以任意写,写什么名字,邮件中显示什么名字 # att1["Content-Disposition"] = 'attachment; filename="' + filename + '"' # message.attach(att1) try: # 注意!如果是使用SSL端口,这里就要改为SMTP_SSL s = smtplib.SMTP_SSL(host, port) # 登陆邮箱 s.login(sender, pwd) # 发送邮件! s.sendmail(sender, receiver0, message.as_string()) # 发送第二人邮件 # s.sendmail(sender, receiver1, message.as_string()) print('邮件发送成功') except smtplib.SMTPException: print('邮件发送失败') if __name__ == '__main__': sentemail()
2.网易邮箱设置