Copyright: Public Number: Fresh Site. QQ group: 690 274 159. Reprinted with my blog, please attach a reprint address, thank you! ^ _ ^ I'm a hip-hop program ape freshman. https://blog.csdn.net/wuhongxia29/article/details/90797842
Day14-B Web Application Development
send email
SMTP (Simple Mail Transfer Protocol) -TCP (Transmission Control Protocol)
When you run the following code, the first in the mailbox "Settings" to open the SMTP service.
# Python发送邮件
import smtplib
from email.mime.text import MIMEText
from email.header import Header
def main():
sender = '[email protected]'
pwd = 'password'
receivers = ['[email protected]']
# 参数(文本内容,文本格式,编码)
message = MIMEText('用Python发送邮件的示例代码', 'plain', 'utf-8')
message['From'] = Header('[email protected]', 'utf-8')
message['To'] = Header('[email protected]', 'utf-8')
message['Subject'] = Header('示例代码实现邮件', 'utf-8')
subject = 'Python邮件测试'
message['Subject'] = Header('测试', 'utf-8')
try:
# 使用非本地服务器,需要建立ssl连接
smtper = smtplib.SMTP_SSL('smtp.126.com', 465)
smtper.login(sender, pwd)
smtper.sendmail(sender, receivers, message.as_string())
print('邮件发送成功!')
except smtplib.SMTPException as e:
print('Error:无法发送邮件.Case:%s' % e)
if __name__ == '__main__':
main()