利用Python发送邮件或带附件的邮件的攻略如下:
一、Python发送邮件的基本步骤
1. 导入smtplib和email模块
import smtplib
from email.mime.text import MIMEText
2. 连接SMTP服务器
mail_host = "smtp.xxx.com"
mail_port = 25
mail_user = "your_username"
mail_password = "your_password"
smtp_obj = smtplib.SMTP(mail_host, mail_port)
smtp_obj.login(mail_user, mail_password)
3. 创建邮件信息
sender = "sender_email_address"
receiver = "receiver_email_address"
subject = "邮件主题"
content = "邮件内容"
message = MIMEText(content, "plain", "utf-8")
message["Subject"] = subject
message["From"] = sender
message["To"] = receiver
4. 发送邮件
smtp_obj.sendmail(sender, receiver, message.as_string())
5. 关闭连接
smtp_obj.quit()
二、Python发送带附件的邮件
1. 导入email模块中的MIMEMultipart、MIMEApplication和encoders类
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email import encoders
2. 创建邮件信息
sender = "sender_email_address"
receiver = "receiver_email_address"
subject = "邮件主题"
content = "邮件内容"
file_path = "附件文件路径"
message = MIMEMultipart()
message["Subject"] = subject
message["From"] = sender
message["To"] = receiver
text = MIMEText(content, "plain", "utf-8")
message.attach(text)
with open(file_path, "rb") as f:
attachment = MIMEApplication(f.read(), Name="附件")
encoders.encode_base64(attachment)
attachment.add_header("Content-Disposition", "attachment", filename="附件")
message.attach(attachment)
3. 发送邮件
smtp_obj.sendmail(sender, receiver, message.as_string())
4. 关闭连接
smtp_obj.quit()
以上是发送邮件或带附件的邮件的Python代码攻略,下面是两个示例说明:
示例一:发送简单邮件
import smtplib
from email.mime.text import MIMEText
mail_host = "smtp.xxx.com"
mail_port = 25
mail_user = "your_username"
mail_password = "your_password"
sender = "sender_email_address"
receiver = "receiver_email_address"
subject = "这是一封测试邮件"
content = "邮件发送测试成功!"
message = MIMEText(content, "plain", "utf-8")
message["Subject"] = subject
message["From"] = sender
message["To"] = receiver
smtp_obj = smtplib.SMTP(mail_host, mail_port)
smtp_obj.login(mail_user, mail_password)
smtp_obj.sendmail(sender, receiver, message.as_string())
smtp_obj.quit()
print("邮件发送成功!")
示例二:发送带附件的邮件
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email import encoders
mail_host = "smtp.xxx.com"
mail_port = 25
mail_user = "your_username"
mail_password = "your_password"
sender = "sender_email_address"
receiver = "receiver_email_address"
subject = "这是一封测试邮件"
content = "邮件发送测试成功!"
file_path = "/path/to/attachment"
message = MIMEMultipart()
message["Subject"] = subject
message["From"] = sender
message["To"] = receiver
text = MIMEText(content, "plain", "utf-8")
message.attach(text)
with open(file_path, "rb") as f:
attachment = MIMEApplication(f.read(), Name="附件")
encoders.encode_base64(attachment)
attachment.add_header("Content-Disposition", "attachment", filename="附件")
message.attach(attachment)
smtp_obj = smtplib.SMTP(mail_host, mail_port)
smtp_obj.login(mail_user, mail_password)
smtp_obj.sendmail(sender, receiver, message.as_string())
smtp_obj.quit()
print("邮件发送成功!")
以上就是Python发送邮件或发带附件的邮件的攻略及两个示例说明,希望能够帮助到你。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:利用Python发送邮件或发带附件的邮件 - Python技术站