【问题标题】:SMTP Email script doesn't work on Mac but works on WindowsSMTP 电子邮件脚本在 Mac 上不工作,但在 Windows 上工作
【发布时间】:2011-09-14 09:55:53
【问题描述】:

我有一些用于发送电子邮件的代码。它可以在 windows 上运行,但是当我尝试在 mac 上运行它时(使用与 windows 机器上相同的用户登录名),它不会发送电子邮件或返回任何错误。

有人对这种事情有任何经验,或者例子,提示,解决方案吗?

import smtplib
import mimetypes
from email import encoders
from email.message import Message
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText


import platform

SERVER = "mail.myCompany.co.za"
COMMASPACE = ', '


SUBJECT = "testing"
TEXT = "mac dev test\n\nSent from: " + platform.platform()
TO = [ 'jared.glass@myCompany.co.za' ]
FROM = "jared.glass@myCompany.co.za"


# connect to the server
smtp = smtplib.SMTP( SERVER )

smtp.ehlo()


# if attachments are too big, try gain and just add them as links
# Create the container (outer) email message.
msg = MIMEMultipart()
msg['Subject'] = SUBJECT
msg['From'] = FROM
msg['To'] = COMMASPACE.join(TO)      
msg.attach( MIMEText( TEXT , 'plain') )

smtp.sendmail( FROM , TO , msg.as_string() )


smtp.close()               
print(" ---- SUCCESS ---- mail sent ---- ")

【问题讨论】:

  • sendmail 调用返回什么?它是字典或抛出异常
  • 我认为您不需要 ehlo() 调用,因此请尝试删除它。也尝试设置 smtp.set_debuglevel(1) 看看发生了什么。
  • 在控制台中执行telnet mail.myCompany.co.za 25 会发生什么?

标签: python macos email smtplib


【解决方案1】:

sendmail 调用没有返回任何结果 - 只是 {}。最后我不知道为什么,但出于纯粹的烦恼,我尝试了

smtp.starttls()
smtp.login('username', 'password')

它似乎解决了这个问题。

对我来说,你需要这样做才能使脚本在 mac 上运行没有任何意义,因为它在没有登录的情况下在 windows 机器上运行良好...... Grrrrr mac。

感谢您的建议:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-26
    • 1970-01-01
    • 1970-01-01
    • 2018-07-11
    • 2018-07-05
    • 2023-02-22
    • 2017-02-01
    相关资源
    最近更新 更多