2017-07-29 69 views
1

enter image description here我使用python從gmail發送電子郵件。我把gmail IMAP上也獲得一個安全密碼(一個16位密碼)。但回覆我的用戶名和密碼不被接受。谷歌帳戶密碼,端口25,587,465(使用SSL)。無法正常工作。蟒蛇smtp發送電子郵件從gmail,

#! /usr/bin/python

# -*- coding: UTF-8 -*-

from email.mime.text import MIMEText

from email.header import Header

from email import encoders

import smtplib 

sender = "[email protected]"

rec= "[email protected]"

passwd = "security password"

#passwd = 'the really google account password'

message = MIMEText("郵件發送","plain","utf-8")

message['From'] =sender

message['To'] = rec

message['Subject'] =Header("from google","utf-8").encode()

smtpObj = smtplib.SMTP("smtp.gmail.com",587)

smtpObj.ehlo()

smtpObj.starttls()

smtpObj.set_debuglevel(1)

smtpObj.login(sender,passwd)

smtpObj.sendmail(sender,[rec],message.as_string) smtpObj.close

回答

1

試試下面,在過去

#!/usr/bin/python 

#from smtplib import SMTP # Standard connection 
from smtplib import SMTP_SSL as SMTP #SSL connection 
from email.MIMEMultipart import MIMEMultipart 
from email.MIMEText import MIMEText 

sender = '[email protected]' 
receivers = ['[email protected]'] 


msg = MIMEMultipart() 
msg['From'] = '[email protected]' 
msg['To'] = '[email protected]' 
msg['Subject'] = 'simple email via python test 1' 
message = 'This is the body of the email line 1\nLine 2\nEnd' 
msg.attach(MIMEText(message)) 

ServerConnect = False 
try: 
    smtp_server = SMTP('smtp.gmail.com','465') 
    smtp_server.login('#name#@gmail.com', '#password#') 
    ServerConnect = True 
except SMTPHeloError as e: 
    print "Server did not reply" 
except SMTPAuthenticationError as e: 
    print "Incorrect username/password combination" 
except SMTPException as e: 
    print "Authentication failed" 

if ServerConnect == True: 
    try: 
     smtp_server.sendmail(sender, receivers, msg.as_string()) 
     print "Successfully sent email" 
    except SMTPException as e: 
     print "Error: unable to send email", e 
    finally: 
     smtp_server.close() 
+0

其密碼我應該使用,谷歌帳戶密碼或安全密碼很適合我? –

+0

此代碼不會嘗試使用除gmail密碼以外的任何密碼 –

+0

另請參閱https://stackoverflow.com/questions/17332384/python-3-send-email-smtp-gmail-error-smtpexception/46754666#46754666 – axd

相關問題