脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python3模块smtplib实现发送邮件功能

python3模块smtplib实现发送邮件功能

2021-02-23 00:17littlethunder Python

这篇文章主要为大家详细介绍了python3模块smtplib实现发送邮件功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了python3 smtplib发送邮件的具体代码,供大家参考,具体内容如下

smtplib模块是smtp简单邮件传输协议客户端的实现,为了通用性,有时候发送邮件的时候要带附件或图片,用email.mime来装载内容。代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import smtplib
import email.mime.multipart
import email.mime.text
 
msg=email.mime.multipart.MIMEMultipart()
msg['from']='ustchacker@tom.com'
msg['to']='blablabla@aliyun.com'
msg['subject']='test'
content='''''
 你好,
   这是一封自动发送的邮件。
 
  www.ustchacker.com
'''
txt=email.mime.text.MIMEText(content)
msg.attach(txt)
 
smtp=smtplib
smtp=smtplib.SMTP()
smtp.connect('smtp.tom.com','25')
smtp.login('ustchacker@tom.com','password')
smtp.sendmail('ustchacker@tom.com','blablabla@aliyun.com',str(msg))
smtp.quit()

查看邮箱内容:

python3模块smtplib实现发送邮件功能

可以看到,用Python发送邮件只需要用smtplib的connect(连接到邮件服务器)、login(登陆验证)、sendmail(发送邮件)三个步骤即可,简单方便。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/littlethunder/article/details/29355403

延伸 · 阅读

精彩推荐