134 lines
3.5 KiB
Markdown
134 lines
3.5 KiB
Markdown
---
|
||
title: Python发信示例
|
||
date: 2022-05-31 11:31:44.524
|
||
updated: 2023-02-13 21:38:40.253
|
||
url: /archives/python-fa-xin-shi-li
|
||
categories:
|
||
- Python
|
||
tags:
|
||
- Python
|
||
---
|
||
|
||
|
||
> 当前Python环境版本为**3.9**
|
||
# 无附件示例
|
||
```python
|
||
import smtplib
|
||
from email.mime.text import MIMEText
|
||
from email.utils import formatdate
|
||
from email.header import Header
|
||
import sys
|
||
import importlib
|
||
# 设置默认字符集为UTF8
|
||
|
||
default_encoding = 'utf-8'
|
||
if sys.getdefaultencoding() != default_encoding:
|
||
importlib.reload(sys)
|
||
sys.setdefaultencoding(default_encoding)
|
||
|
||
# 发送邮件的相关信息(根据实际情况填写)
|
||
smtpHost = ''
|
||
smtpPort = '25'
|
||
sslPort = '465'
|
||
fromMail = ''
|
||
toMail = 'rember1997@163.com'
|
||
username = ''
|
||
password = ''
|
||
|
||
# 邮件标题和内容
|
||
subject = u'白云间的测试邮件'
|
||
body = u'Hello,这是一封测试邮件' + fromMail
|
||
|
||
# 初始化邮件
|
||
encoding = 'utf-8'
|
||
mail = MIMEText(body.encode(encoding), 'plain', encoding)
|
||
mail['Subject'] = Header(subject, encoding)
|
||
mail['From'] = fromMail
|
||
mail['To'] = toMail
|
||
mail['Date'] = formatdate()
|
||
|
||
try:
|
||
# 连接smtp服务器,明文/SSL方式,根据使用的SMTP支持情况选择一种
|
||
# 普通方式,通信过程不加密
|
||
# smtp = smtplib.SMTP(smtpHost,smtpPort)
|
||
# smtp.ehlo()
|
||
# smtp.login(username,password)
|
||
#
|
||
# 纯粹的ssl加密方式,通信过程加密,邮件数据安全
|
||
# smtp = smtplib.SMTP(smtpHost, smtpPort)
|
||
smtp = smtplib.SMTP_SSL(smtpHost, sslPort)
|
||
smtp.ehlo()
|
||
smtp.login(username, password)
|
||
|
||
# 发送邮件
|
||
smtp.sendmail(fromMail, toMail, mail.as_string())
|
||
smtp.close()
|
||
print('OK')
|
||
except Exception as e:
|
||
print(e)
|
||
```
|
||
|
||
# 邮件上传附件示例
|
||
```python
|
||
import smtplib
|
||
import time
|
||
from email.mime.text import MIMEText
|
||
from email.mime.multipart import MIMEMultipart
|
||
from email.header import Header
|
||
localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
|
||
|
||
# 发送邮件的相关信息,根据实际情况填写
|
||
smtpHost = ''
|
||
smtpPort = '25'
|
||
sslPort = '465'
|
||
username = 'rember1997@163.com'
|
||
password = ''
|
||
fromMail = 'rember1997@163.com'
|
||
toMail = ''
|
||
subject = '白云间的SMTP邮件测试'
|
||
body = '邮件发送测试\n \nby Python\n \n' + localtime
|
||
encoding = 'utf-8'
|
||
|
||
# 创建
|
||
mail = MIMEMultipart()
|
||
mail['Subject'] = Header(subject, encoding)
|
||
mail['From'] = Header("rember1997@163.com")
|
||
mail['To'] = Header("byj@hz-qj.com")
|
||
|
||
# 正文内容
|
||
mail.attach(MIMEText(body, 'plain', 'utf-8'))
|
||
|
||
# 构造附件1,上传当前目录下 test.txt 文件
|
||
att1 = MIMEText(open('test.txt', 'rb').read(), 'base64', 'utf-8')
|
||
att1["Content-Type"] = 'application/octet-stream'
|
||
# filename即为邮件中附件的显示名称
|
||
att1["Content-Disposition"] = 'attachment; filename="test.txt"'
|
||
mail.attach(att1)
|
||
# 构造附件2,传送当前目录下的 29M.zip 文件
|
||
att2 = MIMEText(open('29M.zip', 'rb').read(), 'base64', 'utf-8')
|
||
att2["Content-Type"] = 'application/octet-stream'
|
||
att2["Content-Disposition"] = 'attachment; filename="29M.zip"'
|
||
mail.attach(att2)
|
||
|
||
|
||
try:
|
||
# 连接smtp服务器,明文/SSL方式,根据使用的SMTP支持情况选择一种
|
||
# 普通方式,通信过程不加密
|
||
smtp = smtplib.SMTP(smtpHost, smtpPort)
|
||
smtp.ehlo()
|
||
smtp.login(username, password)
|
||
|
||
# # 纯粹的ssl加密方式,通信过程加密,邮件数据安全
|
||
# # smtp = smtplib.SMTP(smtpHost, smtpPort)
|
||
# smtp = smtplib.SMTP_SSL(smtpHost, sslPort)
|
||
# smtp.ehlo()
|
||
# smtp.login(username, password)
|
||
|
||
# 发送邮件
|
||
smtp.sendmail(fromMail, toMail, mail.as_string())
|
||
smtp.close()
|
||
print('OK')
|
||
|
||
except Exception as e:
|
||
print(e)
|
||
``` |