Skip to main content
 首页 » 编程设计

python之从 Protonmail 帐户、SMTP 库使用 Python 发送电子邮件

2024年09月03日40davidwang456

我正在使用带有 protonmail 的苹果邮件应用程序——我有 bridge 应用程序。 (MacOS and Windows install here;linux here。)

激活 bridge 应用程序后,我尝试使用 smtp 库使用 python 发送电子邮件,但它不起作用。这是我尝试运行但失败的代码:

import smtplib 
 
server = smtplib.SMTP("127.0.0.1", portnumber) 
server.login("mymail@protonmail.com", "my password") 
server.sendmail( 
    "mymail@protonmail.com", 
    "receiver@protonmail.com", 
    "hello") 
server.quit() 

我收到的错误信息:

smtplib.SMTPDataError: (554, b'Error: transaction failed, blame it to the weather: malformed MIME header line: 00')

请您参考如下方法:

这可能会有所帮助..

import smtplib  
from email.MIMEMultipart import MIMEMultipart  
from email.MIMEText import MIMEText 
 
port_number =1234 
msg = MIMEMultipart() 
msg['From'] = 'sender@protonmail.com' 
msg['To'] = 'receiver@protonmail.com' 
msg['Subject'] = 'My Test Mail ' 
message = 'This is the body of the mail' 
msg.attach(MIMEText(message)) 
mailserver = smtplib.SMTP('localhost',port_number) 
mailserver.login("sender@protonmail.com", "mypassword") 
mailserver.sendmail('sender@protonmail.com','receiver@protonmail.com',msg.as_string()) 
mailserver.quit()