2017-08-14 153 views
4

我在尝试使用python sendmail发送电子邮件时发生错误。这里是我的Python代码: (PAS D'OBJET)Python - 发送电子邮件时发生错误(中继访问被拒绝)

#! /usr/bin/python 

import smtplib 

from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText 

# me == my email address 
# you == recipient's email address 
me = "[email protected]" 
you = "[email protected]" 

# Create message container - the correct MIME type is multipart/alternative. 
msg = MIMEMultipart('alternative') 
msg['Subject'] = "Link" 
msg['From'] = me 
msg['To'] = you 

# Create the body of the message (a plain-text and an HTML version). 
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org" 
html = """\ 
<html> 
    <head></head> 
    <body> 
    <p>Hi!<br> 
     How are you?<br> 
     Here is the <a href="[http://www.python.org">link</a]http://www.python.org">link</a> you wanted. 
    </p> 
    </body> 
</html> 
""" 

# Record the MIME types of both parts - text/plain and text/html. 
part1 = MIMEText(text, 'plain') 
part2 = MIMEText(html, 'html') 

# Attach parts into message container. 
# According to RFC 2046, the last part of a multipart message, in this case 
# the HTML message, is best and preferred. 
msg.attach(part1) 
msg.attach(part2) 

# Send the message via local SMTP server. 
s = smtplib.SMTP('localhost') 
# sendmail function takes 3 arguments: sender's address, recipient's address 
# and message to send - here it is sent as one string. 
s.sendmail(me, you, msg.as_string()) 
s.quit() 

当我发这封邮件,我有这样的错误:

Traceback (most recent call last): 
    File "./mail.py", line 47, in <module> 
    s.sendmail(me, you, msg.as_string()) 
    File "/usr/lib/python2.7/smtplib.py", line 747, in sendmail 
    raise SMTPRecipientsRefused(senderrs) 
smtplib.SMTPRecipientsRefused: {'[email protected]': (454, '4.7.1 <[email protected]>: Relay access denied')} 

我真的不知道这是为什么不工作因为它在测试环境中工作。你能帮忙理解吗?

+0

你是不是想通过Gmail发送电子邮件?或者你真的有一个本地的SMTP服务器?看到这个答案 - https://stackoverflow.com/questions/10147455/how-to-send-an-email-with-gmail-as-provider-using-python – droravr

+0

@droravr不,我只是想能够发送一封电子邮件,发送至Gmail或其他人 – dmx

+0

您需要一台SMTP服务器才能发送电子邮件,或者使用Gmail(如果这是您拥有帐户的地方或其他相同内容)。 – droravr

回答

0

也许需要更多信息。此错误可能是由域配置引起的。

  • 您的电子邮件发送域是否与TEST和PROD完全相同?
  • 根据什么处理您的电子邮件发送的PROD VS测试,它可能是一个权限问题
+0

通常它是相同的。 – dmx