-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpython发送邮件.py
51 lines (45 loc) · 1.36 KB
/
python发送邮件.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# -*- codeing = utf-8 -*-
# @Time :2023/12/22 10:34
# @Author :xming
# @Version :1.0
# @Descriptioon :
# @Link :
# @File : python发送邮件.py
import smtplib
from email.header import Header
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import COMMASPACE
# 邮件服务器配置
SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587
SMTP_USER = 'your_email@gmail.com'
SMTP_PASSWORD = 'your_password'
# 收件人和邮件内容配置
TO = ['recipient1@example.com', 'recipient2@example.com']
SUBJECT = 'Test Email from Python'
BODY = 'This is a test email sent from Python.'
# 创建邮件对象
msg = MIMEMultipart()
msg['From'] = SMTP_USER
msg['To'] = COMMASPACE.join(TO)
msg['Subject'] = Header(SUBJECT, 'utf-8')
# 添加邮件正文
msg.attach(MIMEText(BODY, 'plain', 'utf-8'))
# 添加附件
filename = 'example.txt'
with open(filename, 'rb') as f:
part = MIMEApplication(f.read(), Name=filename)
part['Content-Disposition'] = 'attachment; filename="%s"' % filename
msg.attach(part)
# 发送邮件
try:
smtp = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
smtp.starttls()
smtp.login(SMTP_USER, SMTP_PASSWORD)
smtp.sendmail(SMTP_USER, TO, msg.as_string())
smtp.quit()
print('邮件发送成功.')
except Exception as e:
print('邮件发送失败:', e)