当前分类:python>>正文

Python SMTP发送邮件失败原因分析及解决方法

来源:互联网   更新时间:2023年8月21日  

Python 笔记

一、邮件发送失败的原因

二、解决方法

import smtplib

smtp_server = "smtp.example.com"
smtp_port = 587

smtp_conn = smtplib.SMTP(smtp_server, smtp_port)
import smtplib

smtp_server = "smtp.example.com"
smtp_port = 587
smtp_username = "user@example.com"
smtp_password = "password"

smtp_conn = smtplib.SMTP(smtp_server, smtp_port)
smtp_conn.login(smtp_username, smtp_password)
import smtplib
from email.mime.text import MIMEText

sender = "sender@example.com"
receivers = ["receiver1@example.com", "receiver2@example.com"]

msg = MIMEText("This is a test email.")
msg['Subject'] = "Test Email"
msg['From'] = sender
msg['To'] = ",".join(receivers)

smtp_server = "smtp.example.com"
smtp_port = 587

smtp_conn = smtplib.SMTP(smtp_server, smtp_port)
smtp_conn.sendmail(sender, receivers, msg.as_string())
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication

sender = "sender@example.com"
receivers = ["receiver1@example.com", "receiver2@example.com"]

msg = MIMEMultipart()
msg['Subject'] = "Test Email"
msg['From'] = sender
msg['To'] = ",".join(receivers)

text = MIMEText("This is a test email.")
msg.attach(text)

pdf_file = open("test.pdf", "rb").read()
pdf_part = MIMEApplication(pdf_file, Name="test.pdf")
pdf_part['Content-Disposition'] = 'attachment; filename="test.pdf"'
msg.attach(pdf_part)

smtp_server = "smtp.example.com"
smtp_port = 587

smtp_conn = smtplib.SMTP(smtp_server, smtp_port)
smtp_conn.sendmail(sender, receivers, msg.as_string())

三、总结

本文固定链接:https://6yhj.com/leku-p-5516.html  版权所有,转载请保留本地址!
[猜你喜欢]

标签: 云主机