2012-04-02 120 views
2

我已經看到以下問題,但我仍然有一些疑問。從發送郵件發送郵件的問題郵件標識[Python]

Sending an email from a distribution list

首先我有一個單獨的電子郵件帳戶,以及在一個特定的郵件服務器使用的一組分配的ID。通過指定From字段,我可以通過Outlook從分發郵件ID發送郵件。它不需要認證。

我一直在使用下面的代碼,通過我的個人賬戶發送郵件:

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


FROMADDR = "[email protected]" 
GROUP_ADDR = ['[email protected]'] 
PASSWORD = 'foo' 


TOADDR = ['[email protected]'] 
CCADDR = ['[email protected]'] 

# Create message container - the correct MIME type is multipart/alternative. 
msg   = MIMEMultipart('alternative') 
msg['Subject'] = 'Test' 
msg['From'] = FROMADDR 
msg['To']  = ', '.join(TOADDR) 
msg['Cc']  = ', '.join(CCADDR) 

# Create the body of the message (an HTML version). 
text = """Hi this is the body 
""" 

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

# Attach parts into message container. 
msg.attach(body) 

# Send the message via local SMTP server. 
s = smtplib.SMTP('server.com', 587) 
s.set_debuglevel(1) 
s.ehlo() 
s.starttls() 
s.login(FROMADDR, PASSWORD) 
s.sendmail(FROMADDR, TOADDR, msg.as_string()) 
s.quit() 

這工作完全正常。由於我能夠通過Outlook(沒有任何密碼)從分發郵件標識發送郵件,有沒有什麼方法可以修改此代碼以通過分發標識發送郵件?我試着註釋掉

s.ehlo() 
s.starttls() 
s.login(FROMADDR, PASSWORD) 

一部分,但該代碼給我下面的錯誤:

send: 'mail FROM:<[email protected]> size=393\r\n' 
reply: b'530 5.7.1 Client was not authenticated\r\n' 
reply: retcode (530); Msg: b'5.7.1 Client was not authenticated' 
send: 'rset\r\n' 
Traceback (most recent call last): 
    File "C:\Send_Mail_new.py", line 39, in <module> 
    s.sendmail(FROMADDR, TOADDR, msg.as_string()) 
    File "C:\Python32\lib\smtplib.py", line 743, in sendmail 
    self.rset() 
    File "C:\Python32\lib\smtplib.py", line 471, in rset 
    return self.docmd("rset") 
    File "C:\Python32\lib\smtplib.py", line 395, in docmd 
    return self.getreply() 
    File "C:\Python32\lib\smtplib.py", line 371, in getreply 
    raise SMTPServerDisconnected("Connection unexpectedly closed") 
smtplib.SMTPServerDisconnected: Connection unexpectedly closed 

會有人好心幫我在這裏?

回答

1

reply: retcode (530); Msg: b'5.7.1 Client was not authenticated'

這意味着你需要驗證。 Outlook可能對您的現有帳戶使用相同的身份驗證(因爲您只更改了From標題)。

+0

謝謝......我使用我的用戶名和密碼s.login()並將通訊組列表指定爲FROMADDR。有效 – Pulimon 2012-04-02 10:07:20