您尚未登录.还有很多原因可能导致您无法通过登录,包括被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())