当前位置:首页 » 编程语言 » python发送邮件

python发送邮件

发布时间: 2022-01-08 09:01:34

❶ 如何使用python发送各类邮件

以下代码调试通过:

#coding:utf-8
importsmtplib
fromemail.mime.textimportMIMEText
fromemail.headerimportHeader

sender='[email protected]'
receiver='[email protected]'
subject='pythonemailtest'
smtpserver='smtp.139.com'
username='[email protected]'
password='xxxxxxxx'

msg=MIMEText('你好lucia这是你的第一封python发出的邮件','text','utf-8')
#中文需参数‘utf-8',单字节字符不需要
msg['Subject']=Header(subject,'utf-8')

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

运行效果:

❷ python,如何发送带附件的邮件

可以通过以下代码实现:

importsmtplib
fromemail.mime.textimportMIMEText
importemail.mime.multipart
fromemail.
fromemail.MIMEBaseimportMIMEBase
fromemailimportEncoders

defsend_mail():
mailto_list=['xxxx','xxx']#收件人
mail_host="xxxxx"#设置服务器
mail_user="xxxx"#用户名
mail_pass="xxxxxx"#口令
mail_postfix="xxxx.xxx"#发件箱的后缀
me="hello"+"<"+mail_user+"@"+mail_postfix+">"#这里的hello可以任意设置,收到信后,将按照设置显示
content='Thisistestmail!'#邮件正文
msg=MIMEMultipart()
body=MIMEText(content,_subtype='html',_charset='gb2312')#创建一个实例,这里设置为html格式邮件
msg.attach(body)
msg['Subject']="SubjectTest"#设置主题
msg['From']=me
msg['To']=";".join(mailto_list)
#附件内容,若有多个附件,就添加多个part,如part1,part2,part3
part=MIMEBase('application','octet-stream')
#读入文件内容并格式化,此处文件为当前目录下,也可指定目录例如:open(r'/tmp/123.txt','rb')
part.set_payload(open('test.txt','rb').read())
Encoders.encode_base64(part)
##设置附件头
part.add_header('Content-Disposition','attachment;filename="test.txt"')
msg.attach(part)

try:
s=smtplib.SMTP()
s.connect(mail_host)#连接smtp服务器
s.login(mail_user,mail_pass)#登陆服务器
s.sendmail(me,mailto_list,msg.as_string())#发送邮件
s.close()
print'sendmailsucess'
returnTrue
exceptException,e:
printstr(e)
returnFalse

❸ python发送电子邮件问题

改成r'你好',或者u'你好'

❹ python发送html格式邮件与发送text格式邮件的不同

HTML格式请用压缩包方式打包发送,否测有些网站会认为你在发送特殊代码,要么出错,要么直接定义你为不友善行为,拒绝发送或接受。TEXT格式就没有这个问题。

❺ python中如何实现发送邮件及附件功能的具体详解

思路如下:
1. 构造MIMEMultipart对象做为根容器
2. 构造MIMEText对象做为邮件显示内容并附加到根容器
3. 构造MIMEBase对象做为文件附件内容并附加到根容器
a. 读入文件内容并格式化
b. 设置附件头
4. 设置根容器属性
5. 得到格式化后的完整文本
6. 用smtp发送邮件

❻ 如何使用Python发送带的邮件

.headerimportHeaderfromemail.mime.textimportMIMETextfromemail.utilsimportparseaddr,formataddrdefsend_email(from_addr,to_addr,subject,password):
msg=MIMEText("邮件正文",'html','utf-8')
msg['From']=u'<%s>'%from_addr
msg['To']=u'<%s>'%to_addr
msg['Subject']=subject

smtp=smtplib.SMTP_SSL('smtp.163.com',465)
smtp.set_debuglevel(1)
smtp.ehlo("smtp.163.com")
smtp.login(from_addr,password)
smtp.sendmail(from_addr,[to_addr],msg.as_string())if__name__=="__main__":
#这里的密码是开启smtp服务时输入的客户端登录授权码,并不是邮箱密码
#现在很多邮箱都需要先开启smtp才能这样发送邮件
send_email(u"from_addr",u"to_addr",u"主题",u"password")

❼ 如何用python自动发送邮件

注意如果使用qq的smtp转发功能的话,需要在设置页面中将该功能打开,然后设定密码,改密码就是赋值给下文password中的。

开启smtp转发功能文档


[python]view plain

#fromemail.MIMETextimportMIMEText#

fromemail.MIMETextimportMIMEText

fromemail.HeaderimportHeader

msg=MIMEText('hello,sendbypython','plain','utf-8');

from_addr="*****@qq.com"#

password="******"#password

to_addr="****@qq.com"#targetemailaddress

smtp_server="smtp.qq.com"#smtpseverdomainforqqissmtp.qq.com

importsmtplib

server=smtplib.SMTP(smtp_server,25);

server.set_debuglevel(1)

server.login(from_addr,password);

server.sendmail(from_addr,[to_addr],msg.as_string())

server.quit()



上面这个是没有主题的,有主题的话建议采用下面这个代码

下面是连续发送有主题的文件10封


[python]view plain

fromemail.HeaderimportHeader

fromemail.MIMETextimportMIMEText

fromemailimportencoders

fromemail.utilsimportparseaddr,formataddr

importsmtplib

def_format_addr(s):

name,addr=parseaddr(s)

returnformataddr((Header(name,'utf-8').encode(),addr))

from_addr="*****@qq.com"#

password="******"#password

to_addr="****@qq.com"#targetemailaddress

smtp_server="smtp.qq.com"#smtpseverdomainforqqissmtp.qq.com

fornuminrange(1,11):

msg=MIMEText('hello,sendbyTom','plain','utf-8');

msg['From']=_format_addr('Tom<%s>'%from_addr)

msg['To']=_format_addr('addministrator<%s>'%to_addr)

msg['Subject']=Header('ThegreetingfromTom','utf-8').encode()

server=smtplib.SMTP(smtp_server,25)

server.set_debuglevel(1)

server.login(from_addr,password)

#server.sendmail(from_addr,[to_addr],msg.as_string())

server.sendmail(from_addr,[to_addr],msg.as_string())

server.quit()

❽ 如何通过python发送邮件啊

一般最好有个smtp服务器,比如说你在163注册个邮箱,这样可以用smtplib通过这个邮箱来发送。以下是示例:

#-*- coding:utf8 -*-
import smtplib
import email
import mimetypes
from email.MIMEMultipart import MIMEMultipart
from email.mime.text import MIMEText

mail_host="smtp.163.com"
mail_user="yourusername"
mail_pass="yourpassword"
mail_postfix="mail.163.com"

def sendmail(to_list,sub,con):
"""发送邮件
"""
# translation
me = mail_user+"<"+mail_user+"@"+mail_postfix+">"

msg = MIMEMultipart('related')
msg['Subject'] = email.Header.Header(sub,'utf-8')
msg['From'] = me
msg['To'] = ";".join(to_list)
msg.preamble = 'This is a multi-part message in MIME format.'

msgAlternative = MIMEMultipart('alternative')
msgText = MIMEText(con, 'plain', 'utf-8')
msgAlternative.attach(msgText)
msg.attach(msgAlternative)

try:
s = smtplib.SMTP()
s.connect(mail_host)
s.login(mail_user,mail_pass)
s.sendmail(me, to_list, msg.as_string())
s.quit()

except Exception,e:
return False

return True

if __name__ == '__main__':
if sendmail(['[email protected]'],"测试","测试"):
print "Success!"
else:
print "Fail!"

如果要不经过邮件系统直接发,通常会被当作垃圾邮件扔了,所以还是这样吧。

❾ 如何在python程序中发邮件

比如我用自己的 139 邮箱 [email protected]
发送邮件到网易邮箱 也是我的办公邮箱 [email protected]

以下代码调试通过:

#coding:utf-8
importsmtplib
fromemail.mime.textimportMIMEText
fromemail.headerimportHeader

sender='[email protected]'
receiver='[email protected]'
subject='pythonemailtest'
smtpserver='smtp.139.com'
username='[email protected]'
password='xxxxxxxx'

msg=MIMEText('你好lucia这是你的第一封python发出的邮件','text','utf-8')
#中文需参数‘utf-8',单字节字符不需要
msg['Subject']=Header(subject,'utf-8')

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

运行结果:

❿ 如何用python发送email

有好几个模块,可以实现.

这里用smtplib和email来实现了一个简单的不带附件的邮件发送

以163邮件为服务邮箱配置的。

#coding:utf-8
importsmtplib
fromemail.mime.textimportMIMEText
fromemail.mime.multipartimportMIMEMultipart
importsys
reload(sys)
sys.setdefaultencoding("utf8")
#配置收发件人,可以发送给多个
recvaddress=['[email protected]','[email protected]']
#163的用户名和密码,修改为你自己的
sendaddr_name='[email protected]'
sendaddr_pswd='your_password'


classSendMail:
def__init__(self,recver=None):
"""接收邮件的人:listortuple"""
ifrecverisNone:
self.sendTo=recvaddress
else:
self.sendTo=recver

defsend(self,sub,content):
"""发送邮件"""
self.msg=MIMEMultipart()
#设置发送邮件
self.msg['from']=sendaddr_name
try:
#设置邮箱服务器
smtp=smtplib.SMTP('smtp.163.com',25)
#登录
smtp.login(sendaddr_name,sendaddr_pswd)
#设置发送主题
self.msg['Subject']=sub
#设置邮件内容
self.msg.attach(MIMEText(content))
#发送邮件
smtp.sendmail(self.msg['from'],self.sendTo,self.msg.as_string())
smtp.close()
print(u"发送邮件成功")
exceptException,e:
print(u'发送邮件失败:'+str(e))
raise

#调用
s=SendMail()
subject="主题"
content="这里是邮件内容"
s.send(subject,content)
热点内容
装缓存下载 发布:2024-09-20 05:42:36 浏览:72
gon引擎自动回收脚本 发布:2024-09-20 05:39:39 浏览:246
好医生连锁店密码多少 发布:2024-09-20 05:09:38 浏览:15
魔兽脚本代理 发布:2024-09-20 05:09:35 浏览:99
python登陆网页 发布:2024-09-20 05:08:39 浏览:758
安卓qq飞车如何转苹果 发布:2024-09-20 04:54:30 浏览:178
存储过程中in什么意思 发布:2024-09-20 04:24:20 浏览:315
php显示数据 发布:2024-09-20 03:48:38 浏览:501
源码安装软件 发布:2024-09-20 03:44:31 浏览:354
入门编程游戏的书 发布:2024-09-20 03:31:26 浏览:236