您還沒有登錄。也有幾個原因,你可能不通過包括由ISP封鎖令,則Gmail彈跳你,如果它不能讓你一個反向DNS等
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 = '[email protected]gmail.com'
my_passw = 'asecret!'
recipients = ['[email protected]', '[email protected]']
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())
你會得到什麼錯誤? – 2012-11-06 07:41:15
請提供錯誤的詳細信息。 – Nilesh
Tichodroma,Lafada,: socket.error:[Errno 10060]連接嘗試失敗,因爲連接方在一段時間後沒有正確響應,或者由於連接的主機未能響應而建立連接失敗 –