zoukankan      html  css  js  c++  java
  • python3使用email模块发送邮件

    这里是以163邮件为例

    ​```python
    #! /usr/bin/env python
    # coding=utf-8
    """
    example: python3 send_mail.py
    """
    
    from email.mime.text import MIMEText
    from email.header import Header
    import smtplib
    from email.utils import parseaddr, formataddr
    
    
    def __format_addr(k):
    	name, addr = parseaddr(k)
    	return formataddr((Header(name, 'utf-8').encode(), addr))
    
    
    def send_mail():
    
    	# 163邮箱smtp服务器
    	wy_server = 'smtp.163.com'
    	# sender_163为发件人的163号码
    	sender_163 = 'xxx'
    	# pwd为163邮箱的授权码
    	pwd = 'WPBXNAGFNDWHGDXGFS'
    	# 发件人的邮箱
    	sender_163_mail = 'xxx3@163.com'
    	# 收件人邮箱
    	receivers = ['xxx1@126.com','xxx2@163.com']
    
    	# 邮件的正文内容
    	mail_content = '你好,这是使用python登录163邮箱发邮件的测试'
    	# 邮件标题
    	mail_title = 'mike的邮件'
    
    	try:
    
    		# ssl登录
    		smtp = smtplib.SMTP_SSL(wy_server)
    		# set_debuglevel()是用来调试的。参数值为1表示开启调试模式,参数值为0关闭调试模式
    		smtp.set_debuglevel(0)
    		smtp.ehlo(wy_server)
    		smtp.login(sender_163, pwd)
    
    		msg = MIMEText(mail_content, "plain", 'utf-8')
    		msg["Subject"] = Header(mail_title, 'utf-8').encode()
    		msg["From"] = __format_addr(sender_163_mail)
    		msg["To"] = __format_addr(receivers)
    		smtp.sendmail(sender_163_mail, receivers, msg.as_string())
    		smtp.quit()
    		return "success"
    	except smtplib.SMTP_SSLException as e:
    		return "error"
    
    
    if __name__ == "__main__":
    	print(send_mail())
    
    
    
    

    原文地址: https://www.cnblogs.com/sonyy/p/13156348.html

    微醺生活,醉美人生
  • 相关阅读:
    Codeforces 1316B String Modification
    Codeforces 1305C Kuroni and Impossible Calculation
    Codeforces 1305B Kuroni and Simple Strings
    Codeforces 1321D Navigation System
    Codeforces 1321C Remove Adjacent
    Codeforces 1321B Journey Planning
    Operating systems Chapter 6
    Operating systems Chapter 5
    Abandoned country HDU
    Computer HDU
  • 原文地址:https://www.cnblogs.com/sonyy/p/13156348.html
Copyright © 2011-2022 走看看