【python】smtp邮件发送

纯文本:

#!/usr/bin/env python3
#coding: utf-8

import smtplib
from email.mime.text import MIMEText
from email.header import Header

sender = ‘***‘
receiver = ‘***‘
subject = ‘python email test‘
smtpserver = ‘smtp.163.com‘
username = ‘***‘
password = ‘***‘ 

msg=MIMEText(_text="你好",_charset=‘utf-8‘)#中文需参数‘utf-8’,单字节字符不需要
msg[‘Subject‘] = Header(subject, ‘utf-8‘)
msg[‘From‘]=sender
msg["To"]=receiver
smtp = smtplib.SMTP()
smtp.connect(‘smtp.exmail.qq.com‘)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()

 HTML格式:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage

sender = ‘***‘
receiver = ‘***‘
subject = ‘python email test‘
smtpserver = ‘smtp.163.com‘
username = ‘***‘
password = ‘***‘

msgRoot = MIMEMultipart()
msgRoot[‘Subject‘] = ‘test message‘

msgText = MIMEText(‘<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>good!‘,‘html‘,‘utf-8‘)
msgRoot.attach(msgText)

fp = open(‘e:\\1.jpg‘, ‘rb‘)
msgImage = MIMEImage(fp.read())
fp.close()

msgImage.add_header(‘Content-ID‘, ‘<image1>‘)
msgRoot.attach(msgImage)

smtp = smtplib.SMTP()
smtp.connect(‘smtp.exmail.qq.com‘)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit()

 附件邮件:

#coding: utf-8    

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage    

sender = ‘***‘
receiver = ‘***‘
subject = ‘python email test‘
smtpserver = ‘smtp.163.com‘
username = ‘***‘
password = ‘***‘    

msgRoot = MIMEMultipart(‘related‘)
msgRoot[‘Subject‘] = ‘test message‘    

#构造附件
att = MIMEText(open(‘h:\\python\\1.jpg‘, ‘rb‘).read(), ‘base64‘, ‘utf-8‘)
att["Content-Type"] = ‘application/octet-stream‘
att["Content-Disposition"] = ‘attachment; filename="1.jpg"‘
msgRoot.attach(att)    

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

  

时间: 2024-10-12 19:02:24

【python】smtp邮件发送的相关文章

python SMTP邮件发送

import stmplib from email.mime.text import MIMEText from email.Header import Header mailhost='stmp.qq.com'  #设置服务器 mailuser='[email protected]' #使用时亲测不能用qq.com, maipass='XXXXXXXX'#不是登陆密码,填写授权码,在邮箱中设置smtp服务. sender='[email protected]' #发件人 receiver=['

python学习笔记(SMTP邮件发送)

想着给框架添加邮件发送功能.所以整理下python下邮件发送功能 首先python是支持邮件的发送.内置smtp库.支持发送纯文本.HTML及添加附件的邮件 之后是邮箱.像163.qq.新浪等邮箱默认关闭SMTP服务,需要我们手动打开 打开后通过发件人邮箱.授权密码 通过发件人的SMTP服务发送 代码如下: 1 #!/usr/bin/env python 2 # -*- coding: utf_8 -*- 3 4 from email.mime.text import MIMEText 5 fr

python实现邮件发送完整代码(带附件发送方式)

实例一:利用SMTP与EMAIL实现邮件发送,带附件(完整代码) __author__ = 'Administrator'#coding=gb2312 from email.Header import Headerfrom email.MIMEText import MIMETextfrom email.MIMEMultipart import MIMEMultipartimport smtplib, datetime def SendMailAttach(): msg = MIMEMultip

基于Python实现邮件发送

实现功能: 邮件发送,支持文字,音频文件,文本文件,图形文件,应用程序及其它类型文件的发送: 支持不同的邮箱: 支持一次性往多个邮箱发送: 支持一次性发送n个附件: 支持中文命名的附件发送:   mail.conf配置: [SMTP] login_user = [email protected] login_pwd = xxxxx from_addr =  [email protected] to_addrs = ['[email protected]','[email protected]']

Python SMTP邮件模块

SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件.HTML邮件以及带附件的邮件. Python对SMTP支持有smtplib和email两个模块,email负责构造邮件,smtplib负责发送邮件. 实例: 1.使用Python发送纯文本格式和html格式的邮件. 1 #!/usr/bin/env python 2 #coding:utf-8 3 4 import smtplib 5 from email.mime.text import MIMEText 6 fr

python smtp邮件

SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式. python的smtplib提供了一种很方便的途径发送电子邮件.它对smtp协议进行了简单的封装. 参考:https://www.cnblogs.com/ysocean/p/7653252.html  https://blog.csdn.net/qq_42543250/article/details/81586663  https:/

python实现邮件发送功能

import smtplib import email.mime.multipart import email.mime.text def send_email(receiver,content): msg = email.mime.text.MIMEText(content, 'html', 'utf-8') msg['from'] = '[email protected]' # 从该邮箱发送 msg['to'] = receiver # 发送到该邮箱 msg['subject'] = 'Py

PHP SMTP邮件发送(可加附件)

<?php /** * @param $address mixed 收件人 多个收件人/或需要设置收件人昵称时为数组 array($address1,$address1)/array(array('address'=>$address1,'nickname'=>$nickname1),array('address'=>$address2,'nickname'=>$nickname2)) * @param $subject string 邮件主题 * @param $body

C#调用smtp邮件发送几个大坑

1.网易.新浪邮箱新增了一个叫“授权码”的东西,开通smtp服务时,必须开启授权码,并且邮件发送代码中也需要加上授权码,如下代码: //指定邮箱账号和密码,需要注意的是,这个密码是你在邮箱设置里开启服务的时候给你的那个授权码SmtpClient smtpclient = new SmtpClient(); …… NetworkCredential networkCredential = new NetworkCredential(sendEmail, sendpwd); smtpclient.C