2017-07-29 14 views
1

enter image description here私はgmailからメールを送信するためにpythonを使用しています。IMAPをGmailに設定してもセキュリティパスワード(16ビットのパスワード)を取得します。返信しますUserNameとパスワードは受け付けません。 Googleアカウントのパスワード、ポート25,587,465(sslを使用)は動作しません。Python 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

、Googleアカウントのパスワードやセキュリティパスワードで私のために働いていますか? –

+0

このコードは、Gmailのパスワード以外のパスワードを使用しようとしません。 –

+0

https://stackoverflow.com/questions/17332384/python-3-send-email-smtp-gmail-error-smtpexception/46754666#46754666も参照してください。 – axd