2017-07-29 1 views
1

enter image description here J'utilise python pour envoyer des e-mails à partir de gmail.J'ai activé gmail IMAP pour obtenir également un mot de passe de sécurité (un mot de passe de 16 bits) .Mais répondez-moi Nom d'utilisateur et mot de passe non acceptés. mot de passe compte google, port 25,587,465 (utilisation ssl) .can ne fonctionne pas.Python smtp envoyer un e-mail à partir de 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

Répondre

1

Essayez ce qui suit, il a travaillé pour moi dans le passé

#!/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

qui passe dois-je utiliser, mot de passe de compte Google ou mot de passe de sécurité? –

+0

Ce code ne tente pas d'utiliser un mot de passe autre que le mot de passe gmail –

+0

Voir aussi https://stackoverflow.com/questions/17332384/python-3-send-email-smtp-gmail-error-smtpexception/46754666#46754666 – axd