如何在不登录服务器的情况下在 Python 中发送电子邮件

Omk*_*kar 6 smtplib python-3.x

我想在没有登录 Python 服务器的情况下发送电子邮件。我正在使用 Python 3.6。我尝试了一些代码,但收到一个错误。这是我的代码:

import smtplib                          

smtpServer='smtp.yourdomain.com'      
fromAddr='from@Address.com'         
toAddr='to@Address.com'     
text= "This is a test of sending email from within Python."
server = smtplib.SMTP(smtpServer)
server.set_debuglevel(1)         
server.sendmail(fromAddr, toAddr, text) 
server.quit()
Run Code Online (Sandbox Code Playgroud)

我希望应该在不询问用户 ID 和密码的情况下发送邮件但收到错误:

"smtplib.SMTPSenderRefused: (530, b'5.7.1 客户端未通过身份验证', 'from@Address.com')"

Omk*_*kar 5

下面的代码对我有用。首先,我通过 Network Team 打开/启用端口 25 并在程序中使用它。

import smtplib                          
smtpServer='smtp.yourdomain.com'      
fromAddr='from@Address.com'         
toAddr='to@Address.com'     
text= "This is a test of sending email from within Python."
server = smtplib.SMTP(smtpServer,25)
server.ehlo()
server.starttls()
server.sendmail(fromAddr, toAddr, text) 
server.quit()
Run Code Online (Sandbox Code Playgroud)


小智 5

我是这样用的。它在我的私人 SMTP 服务器中对我有用。

import smtplib

host = "server.smtp.com"
server = smtplib.SMTP(host)
FROM = "testpython@test.com"
TO = "bla@test.com"
MSG = "Subject: Test email python\n\nBody of your message!"
server.sendmail(FROM, TO, MSG)

server.quit()
print ("Email Send")
Run Code Online (Sandbox Code Playgroud)


小智 5

import win32com.client as win32
outlook=win32.Dispatch('outlook.application')
mail=outlook.CreateItem(0)
mail.To='To address'
mail.Subject='Message subject'
mail.Body='Message body'
mail.HTMLBody='<h2>HTML Message body</h2>' #this field is optional

# To attach a file to the email (optional):
attachment="Path to the attachment"
mail.Attachments.Add(attachment)

mail.Send()
Run Code Online (Sandbox Code Playgroud)

  • 虽然此代码可以解决问题,但[包括解释](https://meta.stackexchange.com/q/114762) 如何以及为何解决问题将真正有助于提高帖子的质量,并且可能会产生结果更多的赞成票。请记住,您是在为将来的读者回答问题,而不仅仅是现在提问的人。请[编辑]您的答案以添加解释并指出适用的限制和假设。 (3认同)