Python 使用 SMTP SSL/端口 465 发送电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24672079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:59:13  来源:igfitidea点击:

send email using SMTP SSL/Port 465

pythonpython-2.7

提问by yuyb0y

I need to send email using SMTP SSL/Port 465 with my bluehost email.
I can't find working code in google i try more than 5 codes. So, please any have working code for sending email using SMTP SSL/port 465 ?

我需要使用 SMTP SSL/端口 465 和我的 bluehost 电子邮件发送电子邮件。
我在 google 中找不到工作代码,我尝试了 5 个以上的代码。那么,请有使用 SMTP SSL/端口 465 发送电子邮件的工作代码吗?

回答by Beginner

You should never post a question like this. Please let us know what you have done, any tries? Any written code etc.

你永远不应该发布这样的问题。请让我们知道你做了什么,任何尝试?任何书面代码等。

Anyways I hope this helps

无论如何,我希望这会有所帮助

import smtplib  

fromaddr = '[email protected]'  
toaddrs  = '[email protected]'  
msg = "I was bored!"


# Credentials   

password = 'password'

# The actual mail send  
server = smtplib.SMTP('smtp.gmail.com:587')  
server.starttls()  
server.login(fromaddr,password)




server.sendmail(fromaddr, toaddrs, msg)


server.quit()

print "done" 

回答by dave

For SSL port 465, you need to use SMTP_SSL, rather than just SMTP.

对于 SSL 端口 465,您需要使用 SMTP_SSL,而不仅仅是 SMTP。

See here for more info.

请参阅此处了解更多信息。

https://docs.python.org/2/library/smtplib.html

https://docs.python.org/2/library/smtplib.html

回答by nclaflin

Jut to clarify the solution from dave here is how i got mine to work with my SSL server (i'm not using gmail but still same). Mine emails if a specific file is not there (for internal purposes, that is a bad thing)

在这里澄清一下 dave 的解决方案是我如何让我的 SSL 服务器使用我的(我没有使用 gmail 但仍然相同)。如果特定文件不存在,我的电子邮件(对于内部目的,这是一件坏事)

import smtplib
import os.path

from email.mime.text import MIMEText

if (os.path.isfile("filename")):
    print "file exists, all went well"
else:
    print "file not exists, emailing"

    msg = MIMEText("WARNING, FILE DOES NOT EXISTS, THAT MEANS UPDATES MAY DID NOT HAVE BEEN RUN")

    msg['Subject'] = "WARNING WARNING ON FIRE FIRE FIRE!"

    #put your host and port here 
    s = smtplib.SMTP_SSL('host:port')
    s.login('email','serverpassword')
    s.sendmail('from','to', msg.as_string())
    s.quit()
print "done"