利用python和QQ邮箱服务器发送邮件

利用python SMTP 给自发送测试邮件

发送文本格式的

代码如下:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.header import Header

# 第三方 SMTP 服务
mail_host = "smtp.qq.com"  # 设置服务器
mail_user = "[email protected]"  # 用户名
mail_pass = "qfvwauysbbtmjgdj"  # 获取授权码
sender = '[email protected]'  # 发件人账号
receivers = ['[email protected]']  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
send_content = 'Python 邮件发送测试...'
message = MIMEText(send_content, 'plain', 'utf-8')  # 第一个参数为邮件内容,第二个设置文本格式,第三个设置编码
message['From'] = Header("我是发件人", 'utf-8')  # 发件人
message['To'] = Header("我是收件人", 'utf-8')   # 收件人

subject = '邮件大标题'
message['Subject'] = Header(subject, 'utf-8')
try:
    smtpObj = smtplib.SMTP()
    smtpObj.connect(mail_host, 25)  # 25 为 SMTP 端口号
    smtpObj.login(mail_user, mail_pass)
    smtpObj.sendmail(sender, receivers, message.as_string())
    print("邮件发送成功")
except smtplib.SMTPException:
    print("Error: 无法发送邮件")

发送html格式的邮件

授权码都不变,只需将MIMEText的第二个参数的文本类型改一下即可:

改成html格式,相当于发的邮件内容是html页面的样子,send_contet= html前端的代码

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.header import Header

# 第三方 SMTP 服务
mail_host = "smtp.qq.com"  # 设置服务器
mail_user = "[email protected]"  # 用户名
mail_pass = "qfvwauysbbtmjgdj"  # 获取授权码
sender = '[email protected]'  # 发件人账号
receivers = ['[email protected]']  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
send_content = """

我是童致远

Welcome! | Try_page View on GitHub

Try_page

creat to make a web of myself without serves

Welcome!

This is Vity-tong

This is MY first page,which is used to learn and make some notes.

click====》 BUTTON to start my journey.

Welcome to GitHub Pages

You can use the editor on GitHub to maintain and preview the content for your website in Markdown files.

Whenever you commit to this repository, GitHub Pages will run Jekyll to rebuild the pages in your site, from the content in your Markdown files.

Markdown

Markdown is a lightweight and easy-to-use syntax for styling your writing. It includes conventions for

Syntax highlighted code block

# Header 1
## Header 2
### Header 3

- Bulleted
- List

1. Numbered
2. List

**Bold** and _Italic_ and `Code` text

[Link](url) and ![Image](src)

For more details see GitHub Flavored Markdown.

Jekyll Themes

Your Pages site will use the layout and styles from the Jekyll theme you have selected in your repository settings. The name of this theme is saved in the Jekyll _config.yml configuration file.

Support or Contact

Having trouble with Pages? Check out our documentation or contact support and we’ll help you sort it out.

"""
message = MIMEText(send_content, 'html', 'utf-8') # 第一个参数为邮件内容,第二个设置文本格式,第三个设置编码 message['From'] = Header("我是发件人",'utf-8') message['To'] = Header("我是收件人", 'utf-8') # 收件人 subject = '邮件大标题' message['Subject'] = Header(subject, 'utf-8') try: smtpObj = smtplib.SMTP() smtpObj.connect(mail_host, 25) # 25 为 SMTP 端口号 smtpObj.login(mail_user, mail_pass) smtpObj.sendmail(sender, receivers, message.as_string()) print("邮件发送成功") except smtplib.SMTPException: print("Error: 无法发送邮件")

发送带附件的邮件

你可能感兴趣的:(自己学习,python)