本文目录一览:
- 1、如何在python中使用smtplib连接gmail发送邮件
- 2、如何使用Python发送带的邮件
- 3、如何使用python发送各类邮件
- 4、如何在python程序中发邮件
- 5、如何用Python发邮件
- 6、如何通过python发送邮件啊?
如何在python中使用smtplib连接gmail发送邮件
单击"工具"菜单,然后选择"帐户..."。
单击"添加",然后单击"邮件..."
在"显示名:"字段中输入您的姓名,然后单击"下一步"。
在"电子邮件地址:"字段中输入您的完整 Gmail 电子邮件地址 (username@gmail.com),然后单击"下一步"。
在"接收邮件(POP3, IMAP 或 HTTP)服务器:"字段中输入"pop.gmail.com"。在"发送邮件服务器 (SMTP):"字段中输入"smtp.gmail.com"。
单击"下一步"。
在"帐户名:"字段中输入您的 Gmail 用户名(包括"@gmail.com")。在"密码:"字段中输入您的 Gmail 密码,然后单击"下一步"。
单击"完成"。
突出显示"帐户"下的"pop.gmail.com",并单击"属性"。
单击"高级"标签。
选中"发送邮件 (SMTP)"下"此服务器要求安全连接 (SSL)"旁边的复选框。
在"发送邮件 (SMTP):"字段中输入"465"。
选中"接收邮件 (POP3)"下"此服务器要求安全连接 (SSL)"旁边的复选框。此端口将更改为 995。
*"发送"和"接收"邮件服务器字段的顺序因版本而异。确保您在每一字段中输入了正确信息。
如何使用Python发送带的邮件
import smtplibfrom email import encodersfrom email.header import Headerfrom email.mime.text import MIMETextfrom email.utils import parseaddr, formataddrdef send_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发送各类邮件
以下代码调试通过:
# coding: utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header
sender = 'lucia_gaga@139.com'
receiver = 'lu.han@beebank.com'
subject = 'python email test'
smtpserver = 'smtp.139.com'
username = 'lucia_gaga@139.com'
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程序中发邮件
通过命令行发送邮件,功能强大
python ./mail -s $server -f $from -t $to -u $user -p $pass -S "$subject" -m "${mail_msg}" -F $file
Python 发送邮件可以添加附件:
#!/usr/bin/python
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email import Utils, Encoders
import mimetypes, sys,smtplib,socket,getopt
class SendMail:
def __init__(self,smtp_server,from_addr,to_addr,user,passwd):
self.mailserver=smtp_server
self.from_addr=from_addr
self.to_addr=to_addr
self.username=user
self.password=passwd
def attachment(self,filename):
fd=open(filename,'rb')
filename=filename.split('/')
mimetype,mimeencoding=mimetypes.guess_type(filename[-1])
if (mimeencoding is None) or (mimetype is None):
mimetype='application/octet-stream'
maintype,subtype=mimetype.split('/')
if maintype=='text':
retval=MIMEText(fd.read(), _subtype=subtype, _charset='utf-8')
else:
retval=MIMEBase(maintype,subtype)
retval.set_payload(fd.read())
Encoders.encode_base64(retval)
retval.add_header('Content-Disposition','attachment',filename=filename[-1])
fd.close()
return retval
def msginfo(self,msg,subject,filename):
# message = """Hello, ALL
#This is test message.
#--Anonymous"""
message=msg
msg=MIMEMultipart()
msg['To'] = self.to_addr
msg['From'] = 'sa '+self.from_addr+''
msg['Date'] = Utils.formatdate(localtime=1)
msg['Message-ID'] = Utils.make_msgid()
if subject:
msg['Subject'] = subject
if message:
body=MIMEText(message,_subtype='plain')
msg.attach(body)
#for filename in sys.argv[1:]:
if filename:
msg.attach(self.attachment(filename))
return msg.as_string()
def send(self,msg=None,subject=None,filename=None):
try:
s=smtplib.SMTP(self.mailserver)
try:
s.login(self.username,self.password)
except smtplib.SMTPException,e:
print "Authentication failed:",e
sys.exit(1)
s.sendmail(self.from_addr,self.to_addr.split(','),self.msginfo(msg,subject,filename))
except (socket.gaierror,socket.error,socket.herror,smtplib.SMTPException),e:
print "*** Your message may not have been sent!"
print e
sys.exit(2)
else:
print "Message successfully sent to %d recipient(s)" %len(self.to_addr)
if __name__=='__main__':
def usage():
print """Useage:%s [-h] -s SMTP Server -f FROM_ADDRESS -t TO_ADDRESS -u USER_NAME -p PASSWORD [-S MAIL_SUBJECT -m MAIL_MESSAGE -F ATTACHMENT]
Mandatory arguments to long options are mandatory for short options too.
-f, --from= Sets the name of the "from" person (i.e., the envelope sender of the mail).
-t, --to= Addressee's address. -t "test@test.com,test1@test.com".
-u, --user= Login SMTP server username.
-p, --pass= Login SMTP server password.
-S, --subject= Mail subject.
-m, --msg= Mail message.-m "msg, ......."
-F, --file= Attachment file name.
-h, --help Help documen.
""" %sys.argv[0]
sys.exit(3)
try:
options,args=getopt.getopt(sys.argv[1:],"hs:f:t:u:p:S:m:F:","--help --server= --from= --to= --user= --pass= --subject= --msg= --file=",)
except getopt.GetoptError:
usage()
sys.exit(3)
server=None
from_addr=None
to_addr=None
username=None
password=None
subject=None
filename=None
msg=None
for name,value in options:
if name in ("-h","--help"):
usage()
if name in ("-s","--server"):
server=value
if name in ("-f","--from"):
from_addr=value
if name in ("-t","--to"):
to_addr=value
if name in ("-u","--user"):
username=value
if name in ("-p","--pass"):
password=value
if name in ("-S","--subject"):
subject=value
if name in ("-m","--msg"):
msg=value
if name in ("-F","--file"):
filename=value
if server and from_addr and to_addr and username and password:
test=SendMail(server,from_addr,to_addr,username,password)
test.send(msg,subject,filename)
else:
usage()
如何用Python发邮件
Python发送邮件简单的实例:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.header import Header
sender = 'from@runoob.com'
receivers = ['429240967@qq.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
# 三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码
message = MIMEText('Python 邮件发送测试...', 'plain', 'utf-8')
message['From'] = Header("菜鸟教程", 'utf-8')
message['To'] = Header("测试", 'utf-8')
subject = 'Python SMTP 邮件测试'
message['Subject'] = Header(subject, 'utf-8')
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, message.as_string())
print "邮件发送成功"
except smtplib.SMTPException:
print "Error: 无法发送邮件"
如何通过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!"
如果要不经过邮件系统直接发,通常会被当作垃圾邮件扔了,所以还是这样吧。