AI智能
改变未来

smtplib详解,发送邮件


创建邮箱账号

1、官网登录邮箱。

import smtplibserver = \"smtp.sina.com\"fromaddr= \"dachxxxx@sina.com\" #须修改toaddr = \"8xxxx@qq.com\" #须修改msg = \"\"\"to:%sfrom:%sHello,I am smtp server\"\"\" %(toaddr,fromaddr)s = smtplib.SMTP(server)s.set_debuglevel(1)s.login(\"dachxxx@sina.com\",\"1535d05c76ca6xxx\")#须修改s.sendmail(fromaddr,toaddr,msg)

程序

# Import smtplib for the actual sending functionimport smtplib# Import the email modules we\'ll needfrom email.mime.text import MIMEText# Open a plain text file for reading.  For this example, assume that# the text file contains only ASCII characters.fp = open(\'textfile\', \'r\',encoding=\'utf-8\')# Create a text/plain messagemsg = MIMEText(fp.read())fp.close()# me == the sender\'s email address# you == the recipient\'s email addressmsg[\'Subject\'] = \'The contents of %s\' %(\"mcw\")msg[\'From\'] = \'dacxxx@sina.com\'msg[\'To\'] = \'8xxxx@qq.com\'# Send the message via our own SMTP server, but don\'t include the# envelope header.s = smtplib.SMTP(\'smtp.sina.com\')s.login(\"dacxxxi@sina.com\",\"1535d05c76ca6xxx\")s.sendmail(\'dacxxx@sina.com\', \'8xxx@qq.com\', msg.as_string())s.quit()

程序

结果:

解析HTML,需要添加html参数。

参考链接:https://vimsky.com/zh-tw/examples/detail/python-method-smtplib.SMTPSenderRefused.html

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » smtplib详解,发送邮件