阅读量: 次  文章字数: 219字  阅读时长: 1分钟

使用Python发送邮件

Key_points:

  • SMTP服务器地址和端口
    • 使用TLS加密: 587
      1
      2
      server = smtplib.SMTP(smtp_server, smtp_port)
      server.starttls()
    • 使用SSL加密: 465
      1
      server = smtplib.SMTP_SSL(smtp_server, smtp_port)

代码如下:

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
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

# 发送邮件
def send_email():
print("Preparing to send email...")
sender_email = 'your_email@example.com'
receiver_email = 'receiver_email@example.com'
password = 'your_email_password'

smtp_server = 'smtp.yourmail.com'
smtp_port = 587 # SMTP端口, 通常为587(TSL)或465(SSL)

subject = 'subject'
body = 'body'

message = MIMEMultipart()
message['From'] = sender_email
message['To'] = receiver_email
message['Subject'] = subject
message.attach(MIMEText(body, 'plain'))

try:
print("Connecting to SMTP server...")
# If you want to use SSL, use `smtplib.SMTP_SSL()` instead and remove `server.starttls()`
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls() # 使用TLS加密
print("Logging in to SMTP server...")
server.login(sender_email, password) # 登录
print("Sending email...")
server.sendmail(sender_email, receiver_email, message.as_string()) # 发送邮件
print("Email sent successfully.")
except Exception as e:
print(f"Email sent failed: {e}")

# 主函数
def main():
send_mail()

Comments

2024-09-09