python3发送到ipv6到指定邮箱
ipv4需要登录,ipv6不需登陆,而考虑到开机的时候ipv4可能是未登录的状态,而qq邮箱是没法在纯ipv6的环境下使用的,因此采用ipv6下可以发送邮件的outlook邮箱。
设置该python程序开机自启,即在C盘start目录下加入一个批处理文件,方法是用记事本写下如下代码:python1.py(我命名上面的py程序为1.py,按照实际的文件名来处理),保存之后将该.txt改成.bat即变成了批处理文件然后把你的.py文件拷贝到c盘System32下即可。
ipv6状态下需要注意远程的电脑设置好这些之后不要反复的开机折腾,要不邮箱可能被视为发送垃圾邮件而被暂时锁定。
如何用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(['test@test.com'],"测试","测试"):
print "Success!"
else:
print "Fail!"
如果要不经过邮件系统直接发,通常会被当作垃圾邮件扔了,所以还是这样吧。
如何在python程序中发邮件
提供以下两种方式:
利用smtplib,email库
coding:utf-8
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import sys
reload(sys)
sys.setdefaultencoding("utf8")
# 配置收发件人
recvaddress = ['receiveemail@sina.com','receiveemail@qq.com']
# 163的用户名和密码
sendaddr_name = 'your_email@163.com'
sendaddr_pswd = 'your_password'
class SendMail:
def __init__(self,recver=None):
"""接收邮件的人:list or tuple"""
if recver is None:
self.sendTo = recvaddress
else:
self.sendTo = recver
def send(self,sub,content,attachcontent):
"""发送邮件"""
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))
#设置邮件附件,将附件的内容attachcontent,写到filename的文件中,作为附件发送
att1 = MIMEText(attachcontent, 'base64', 'gb2312')
att1["Content-Type"] = 'application/octet-stream'
att1["Content-Disposition"] = 'attachment; filename="testattach.txt"'#这里的filename可以任意写,写什么名字,邮件中显示什么名字
self.msg.attach(att1)
#发送邮件
smtp.sendmail(self.msg['from'], self.sendTo, self.msg.as_string())
smtp.close()
print(u"发送邮件成功")
except Exception,e:
print(u'发送邮件失败:'+str(e))
raise
#调用
s = SendMail()
subject = "主题"
content = "这里是邮件内容"
attachcontent = "这里是附件哦"
s.send(subject,content,attachcontent)
2.利用webpy自带的email模块
首先要安装web.py的依赖库
pip install web.py
#coding:utf-8
import web
##### email服务器配置 #####
web.config.smtp_server = 'smtp.sina.com'
web.config.smtp_port = 25
web.config.smtp_username = '你的email前缀,@前的字符串'
web.config.smtp_password = '你的密码'
web.config.smtp_starttls = True
send_from="youremail@sina.com"
receiveemailaddr = ["receiveemail@qq.com","receiveemail@163.com"]
subject=u'这里是主题'
message=u'这里是邮件的内容'
def sendemail(sendfrom,sendto,subject,message,cc=None, bcc=None,headers=({'User-Agent': 'webpy.sendmail', 'X-Mailer': 'webpy.sendmail',})):
try:
web.sendmail(send_from,sendto,subject,message,cc=cc,bcc=bcc)
print "邮件发送成功"
except Exception,e:
print(str(e))
#调用
sendemail(send_from,receiveemailaddr,subject,message)
如何用python发送email
有好几个模块,可以实现.
这里用smtplib和email来实现了一个简单的不带附件的邮件发送
以163邮件为服务邮箱配置的。
#coding:utf-8
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import sys
reload(sys)
sys.setdefaultencoding("utf8")
# 配置收发件人,可以发送给多个
recvaddress = ['xxx@sina.com','xxx@qq.com']
# 163的用户名和密码,修改为你自己的
sendaddr_name = 'your_email@163.com'
sendaddr_pswd = 'your_password'
class SendMail:
def __init__(self,recver=None):
"""接收邮件的人:list or tuple"""
if recver is None:
self.sendTo = recvaddress
else:
self.sendTo = recver
def send(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"发送邮件成功")
except Exception,e:
print(u'发送邮件失败:'+str(e))
raise
#调用
s = SendMail()
subject = "主题"
content = "这里是邮件内容"
s.send(subject,content)
ME format.'msgAlternative = MIMEMultipart('alternative')msgText = MIMEText(con, 'plain', 'utf-8')ms
tipart('related')msg['Subject'] = email.Header.Header(sub,'utf-8')msg['From'] = memsg['To'] = ";".join(to_list)msg.preamble = 'This is a multi-par
python3发送到ipv6到指定邮箱ipv4需要登录,ipv6不需登陆,而考虑到开机的时候ipv4可能是未登录的状态,而qq邮箱是没法在纯ipv6的环境下使用的,因此采用ipv6下可以发送邮件的outlook邮箱。设置该python程序开机自启,即在C盘star