Skip to main content
 首页 » 编程设计

python之如何使用 Mimemultipart 在 Python 中发送邮件

2025年05月04日57bluestorm

我是 Python 的新手,所以在使用 python 脚本发送邮件时遇到了一些困难。我正在使用 mime 和 smtplib。由于某种原因,我无法发送并且我的邮件没有任何正文。附加代码。 任何想法 ??

import smtplib 
from email.mime.text import MIMEText 
from email.mime.base import MIMEBase 
from email.mime.multipart import MIMEMultipart 
 
hostname='hello' 
 
sender = 'xx@xx.com' 
receivers = ['vholla@xx.com'] 
 
msg = MIMEMultipart() 
msg['From'] = sender 
msg['To'] = receivers 
msg['Subject'] = "Upgrade started for"+hostname+"." 
 
 
try: 
   smtpObj = smtplib.SMTP('mail.xxx.com',25) 
   smtpObj.sendmail(sender, receivers, msg.as_string()) 
   print "Successfully sent email" 
except: 
        print "Error: unable to send email" 

请您参考如下方法:

我让这个选项通过 postfix 起作用:

import smtplib 
from email.mime.text import MIMEText 
s = smtplib.SMTP() 
s.connect('localhost') # connect to the SMTP server 
doc = "<html><h1>Test title</h1></html>" 
while True: 
    msg = MIMEText( doc, _subtype='html', _charset='utf-8' ) 
    s.sendmail('xx@xx.com', ['vholla@xx.com'], msg.as_string()) 
s.quit()