smtplib详解,发送邮件

创建邮箱账号

1、官网登录邮箱。

2、在邮箱的主界面找到“设置”,新版的主界面与旧版稍有不同,一般位于上方,齿轮状的即是。

3、点击齿轮状的设置标志,会弹出一个下拉菜单,在最后有我们找的关于邮箱的更多设置,点击进入。

4、此时切换到了设置面板,在设置区我们需要切换至账户界面,因为一般情况下pop3和smtp服务都是在这里设置的。

5、继续把网页往下拉,下面出现了我们要找的pop3和smtp服务设置。默认状态下显示为关闭。此时我们需要开启它,只需要点击开启就行了,然后点击保存。

6、填入验证码,保存设置成功。

 

新浪邮箱怎么不能用邮件客户端登陆了?

需要开启IMAP服务:

1、先登录自己的sina邮箱。

2、 单击窗口上的 "设置" 连接,进入到"邮箱设置"中,,切换到"账户"标签页,选中下方的开启“IMAP/SMTP”这时它的附加选项被激活,最后点击"保存更改"即可

发送邮件程序1

import smtplib
server = "smtp.sina.com"
fromaddr= "dachxxxx@sina.com" #须修改
toaddr = "8xxxx@qq.com" #须修改
msg = """
to:%s
from:%s
Hello,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)
程序

 

 发送邮件程序2

参考:https://docs.python.org/2/library/email-examples.html

官网的缺少一个登陆认证,没有的话报错认证啥问题的

# Import smtplib for the actual sending function
import smtplib

# Import the email modules we'll need
from 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 message
msg = MIMEText(fp.read())
fp.close()

# me == the sender's email address
# you == the recipient's email address
msg['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

原文地址:https://www.cnblogs.com/machangwei-8/p/15739131.html