python smtp_无法使用python smtp发送电子邮件

您尚未登录.还有很多原因可能导致您无法通过登录,包括被ISP阻止,如果无法在您的DNS上反向发送Gmail邮件等.

try:

smtpObj = smtplib.SMTP('smtp.gmail.com', 587) # or 465

smtpObj.ehlo()

smtpObj.starttls()

smtpObj.login(account, password)

smtpObj.sendmail(sender, receivers, message)

print "Successfully sent email"

except:

print "Error: unable to send email"

我刚刚注意到您的请求,希望能够附加文件.因为现在您需要处理编码,所以事情发生了变化.尽管我不认为,但遵循起来并不难.

import os

import email

import email.encoders

import email.mime.text

import smtplib

# message/email details

my_email = 'myemail@gmail.com'

my_passw = 'asecret!'

recipients = ['jack@gmail.com', 'jill@gmail.com']

subject = 'This is an email'

message = 'This is the body of the email.'

file_name = 'C:\temp\test.txt'

# build the message

msg = email.MIMEMultipart.MIMEMultipart()

msg['From'] = my_email

msg['To'] = ', '.join(recipients)

msg['Date'] = email.Utils.formatdate(localtime=True)

msg['Subject'] = subject

msg.attach(email.MIMEText.MIMEText(message))

# build the attachment

att = email.MIMEBase.MIMEBase('application', 'octet-stream')

att.set_payload(open(file_name, 'rb').read())

email.Encoders.encode_base64(att)

att.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file_name))

msg.attach(att)

# send the message

srv = smtplib.SMTP('smtp.gmail.com', 587)

srv.ehlo()

srv.starttls()

srv.login(my_email, my_passw)

srv.sendmail(my_email, recipients, msg.as_string())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值