这里的 授权码(Authorization Code) 是 163 邮箱(以及 QQ 邮箱等国内常见邮箱服务商)专门为 SMTP/POP3/IMAP 等邮件协议提供的独立密码,与邮箱的登录密码不同。

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.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.application import MIMEApplication
def send_163_email(): smtp_server = "smtp.163.com" smtp_port = 465 sender = "your_username@163.com" password = "ABCDEFG123456" receiver = "recipient@example.com"
msg = MIMEMultipart() msg['From'] = sender msg['To'] = receiver msg['Subject'] = "测试邮件(带Markdown附件)"
msg.attach(MIMEText("这是邮件正文,附件是Markdown文件。", 'plain', 'utf-8'))
markdown_content = "# CSDN文章汇总\n| 标题 | 链接 |\n|------|------|\n| [Python教程] | https://example.com |" attachment = MIMEApplication(markdown_content.encode('utf-8'), Name="articles.md") attachment['Content-Disposition'] = 'attachment; filename="articles.md"' msg.attach(attachment)
try: with smtplib.SMTP_SSL(smtp_server, smtp_port) as server: server.login(sender, password) server.sendmail(sender, receiver, msg.as_string()) print("邮件发送成功!") except Exception as e: print(f"发送失败: {e}")
if __name__ == "__main__": send_163_email()
|