python自动化---各类发送邮件方法及其可能的错误

一、发送文本邮件

可能的问题1.:需要注意,目前QQ邮箱来讲,不能收到完整的邮件,即有些内容不能显示,最好全部使用网易邮箱:

可能的问题2.:在以往的文本邮件发送中,只写了

msg = MIMEText('hello,world,中文邮件测试', 'text', 'utf-8')
msg['Subject'] = Header(subject, 'utf-8')

目前来讲,需要有三个参数,即

#  中文需要参数utf-8
msg = MIMEText('hello,world,中文邮件测试', 'text', 'utf-8')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = 'lemon'
msg['To'] = '[email protected]'

文本代码参考(写代码的时候是本人的邮箱,在此已将邮箱更改,复制的时候需要注意)

# -*- coding:utf-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.header import Header

#  发送邮箱
sender = '[email protected]'

#  接收邮箱
receiver = '[email protected]'

#  发送邮件主题
subject = 'Python email testing'

#  发送邮箱服务器
smtpserver = 'smtp.163.com'

#  发送邮箱用户名/密码
username = "[email protected]"
password = "123456"

#  中文需要参数utf-8
msg = MIMEText('hello,world,中文邮件测试', 'text', 'utf-8')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = 'lemon'
msg['To'] = '[email protected]'

smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
test_text_mail

二、发送HTML邮件

将参数内容修改即可:如下

#  HTML形式的文件内容
msg = MIMEText('

你好!!欢迎使用html格式

','html','utf-8') msg['Subject'] = Header(subject, 'utf-8') # msg['From'] = 'lemon' # msg['To'] = '[email protected]'

其他内容,与上述代码一致;

三、发送img邮件

# -*- coding:utf-8 -*-
import smtplib
import mimetypes
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.header import Header


sender = '[email protected]'
receiver = '[email protected]'
subject = 'Python email image testing'
smtpserver = 'smtp.163.com'
username = "[email protected]"
password = "123456"

#  发送图片附件的邮件
msg = MIMEMultipart()
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = 'lemon'
msg['To'] = '[email protected]'

txt = MIMEText("这是中文的邮件内容哦","plain",'utf-8')
msg.attach(txt)

file1 = r"E:\images\2.jpg"
image = MIMEImage(open(file1, 'rb').read())
image.add_header('Content-ID', '')
msg.attach(image)

smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
test_img_mail

显示如下:

python自动化---各类发送邮件方法及其可能的错误_第1张图片

新内容:

msg = MIMEMultipart()
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = 'lemon'
msg['To'] = '[email protected]'

txt = MIMEText("这是中文的邮件内容哦","plain",'utf-8')
msg.attach(txt)

file1 = r"E:\images\2.jpg"
image = MIMEImage(open(file1, 'rb').read())
image.add_header('Content-ID', '')
msg.attach(image)

以上是将img图片以附件格式发送,若让其插在正文中,代码改成

# -*- coding:utf-8 -*-
import smtplib
import mimetypes
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.header import Header

sender = '[email protected]'
receiver = '[email protected]'
subject = 'Python email image testing2'
smtpserver = 'smtp.163.com'
username = "[email protected]"
password = "123456"

#  发送图片插在正文中的邮件
msg = MIMEMultipart()
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = 'lemon'
msg['To'] = 'def@163.com'

msgText = MIMEText('Some HTML text and an image.

Nifty!
','html'
) msg.attach(msgText) file1 = r"E:\images\2.jpg" image = MIMEImage(open(file1, 'rb').read()) image.add_header('Content-ID', '') msg.attach(image) smtp = smtplib.SMTP() smtp.connect('smtp.163.com') smtp.login(username, password) smtp.sendmail(sender, receiver, msg.as_string()) smtp.quit()

四、发送附件邮件

import mimetypes
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.header import Header

sender = '[email protected]'
receiver = '[email protected]'
subject = 'Python email attachment testing2'
smtpserver = 'smtp.163.com'
username = "[email protected]"
password = "123456"

#  发送有附件的邮件
msg = MIMEMultipart('related')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = 'lemon'
msg['To'] = '[email protected]'

#  构造附件
msgatt = MIMEText(open(r"E:\attachment\ API.docx").read(),'base64','utf-8')
msgatt["Content-Type"] = 'application/octet-stream'
msgatt["Content-Disposition"] = 'attachment;filename="API.docx"'
msg.attach(msgatt)

smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()

五、发送群发邮件

添加多个接收人即可:

六、发送各种元素都包含的邮件

# -*- coding:utf-8 -*-
import smtplib
import mimetypes
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.header import Header

sender = '[email protected]'
receiver = '[email protected]'
subject = 'Python email many testing2'
smtpserver = 'smtp.163.com'
username = "[email protected]"
password = "123456"

#  构造消息容器
msg = MIMEMultipart()
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = 'lemon'
msg['To'] = '[email protected]'

#  构造文件的本身,用html格式:html = """

    
        

你好!!欢迎使用html格式

""" # 记录以上信息 part2 = MIMEText(html, 'html','utf-8') msg.attach(part2) # 构造附件 msgatt = MIMEText(open(r"E:\attachment\API.docx").read(),'base64','utf-8') msgatt["Content-Type"] = 'application/octet-stream' msgatt["Content-Disposition"] = 'attachment;filename="API.docx"' msg.attach(msgatt) smtp = smtplib.SMTP() smtp.connect('smtp.163.com') smtp.login(username, password) smtp.sendmail(sender, receiver, msg.as_string()) smtp.quit()

注意:此处,若将html格式写成

html = """\
Hi!
       How are you?
       Here is the you wanted.
""" 

则邮件无法发送到,故一定要写成规范的格式;

七、基于SSL的邮件

 用的少,暂未涉及,可参考文档:http://blog.csdn.net/u012063703/article/details/41009955

后续会跟上:http://www.jb51.net/article/49216.htm

转载于:https://www.cnblogs.com/xiaobucainiao/p/6210536.html

你可能感兴趣的:(python自动化---各类发送邮件方法及其可能的错误)