问题
I have seen the following question but I still have a few doubts.
Sending an email from a distribution list
Firstly I have an individual mail account as well as a distribution id used for a group in a particular mail server. I am able to send mails from the distribution mail id through outlook just by specifying the From
field. It requires no authentication.
I have been using the following code to send mails through my personal account:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import os
FROMADDR = "myaddr@server.com"
GROUP_ADDR = ['group@server.com']
PASSWORD = 'foo'
TOADDR = ['toaddr@server.com']
CCADDR = ['group@server.com']
# 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()
This works perfectly fine. Since I am able to send mail from a distribution mail id through outlook (without any password), is there any way that I can modify this code to send mail through the distribution id? I tried commenting out the
s.ehlo()
s.starttls()
s.login(FROMADDR, PASSWORD)
part but the code gives me the following error:
send: 'mail FROM:<group@server.com> 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
Would someone kindly help me out here?
回答1:
reply: retcode (530); Msg: b'5.7.1 Client was not authenticated'
This means you need authentication. Outlook is likely using the same authentication for your existing account (since you only changed the From
header).
来源:https://stackoverflow.com/questions/9972216/issue-with-sending-mails-from-a-distribution-mail-id-python