2014-01-12 5 views
2
import smtplib 

sender = '[email protected]' 
receiver = ['[email protected]'] 

message = """From: From Person <[email protected]> 
To: To Person <[email protected]> 
Subject: SMTP e-mail test 

This is a test e-mail message. 
""" 

try: 

    print("trying host and port...") 

    smtpObj = smtplib.SMTP('smtp.gmail.com', 465) 

    print("sending mail...") 

    smtpObj.sendmail(sender, receiver, message) 

    print("Succesfully sent email") 

except SMTPException: 

    print("Error: unable to send email") 

두 개의 새 전자 메일 계정 (위)을 동일한 서버 (gmail)에 만들어 테스트했습니다. "시도하는 호스트와 포트 ..."가 인쇄되고 더 이상 진행되지 않습니다. 문제는 입력 한 주소와 포트 번호로 이루어져야합니다. 그러나 gmail의 보내는 메일 서버 세부 정보에 따르면 올바르게 입력했습니다. 어떤 아이디어가 잘못된거야?smtplib - python을 사용하여 전자 메일을 보내지 않았습니다.

포트 번호를 제거하거나 587과 같은 다른 포트 번호를 사용하면 오류가 발생합니다.

+0

는 smtplib''에서 진단을 활성화합니다.. 이 세부 수준에서 우리가 할 수있는 일은 모두 추측입니다. 내 생각 엔 Gmail에 인증이 필요하다는 것입니다.이 경우 거의 중복 된 제국의 crapload를 참조하십시오. – tripleee

답변

1

Sending email via Gmail's SMTP servers requires TLS 및 인증. 인증을 받으려면 make an application-specific password for your account이 필요합니다.

이 스크립트는 나를 위해 일했습니다 (비록 내 자신의 Gmail 이메일 주소와 내 응용 프로그램 전용 비밀번호를 사용했지만). 아래 코드에서 APPLICATION_SPECIFIC_PASSWORD를 생성 한 비밀번호로 바꿉니다.

import smtplib 

sender = '[email protected]' 
receiver = ['[email protected]'] 

message = """From: From Person <[email protected]> 
To: To Person <[email protected]> 
Subject: SMTP e-mail test 

This is a test e-mail message. 
""" 

try: 
    print("trying host and port...") 

    smtpObj = smtplib.SMTP_SSL('smtp.gmail.com', 465) 
    smtpObj.login("[email protected]", "APPLICATION_SPECIFIC_PASSWORD") 

    print("sending mail...") 

    smtpObj.sendmail(sender, receiver, message) 

    print("Succesfully sent email") 

except smtplib.SMTPException: 
    print("Error: unable to send email") 
    import traceback 
    traceback.print_exc() 

가 (이 문제를 디버깅하려면, 내가 문을 제외한에서 인쇄 역 추적 코드를 추가했습니다. 예외가 작동하도록하는 방법에 대한 특정 정보를했다. 코드는 포트 465에 접근, 나는 때문에 생각하면 걸려 TLS 협상 문제는, 그래서 포트 587를 사용하여 시도했다 후 내가 무엇을 설명 좋은 디버깅 정보를 가지고) 잘못거야 위치를 볼 수 있도록

You can see info on the SMTP_SSL object here.

관련 문제