欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

python 发邮件 有问题的代码 有大用 有大大用

无可奈何花落去sd

下面的代码 有问题,执行到 server = smtplib.SMTP("10.54.21.20", 25, timeout=10)  时 ,会有异常 Mailer Error: Connection unexpectedly closed   很可能 10.54.21.20  25 端口有问题 ,telnet 试一下吧

import smtplib    
from email.mime.multipart import MIMEMultipart    
from email.mime.text import MIMEText    

   
# Email configuration    
smtp_host = '192.168。21.20'    
smtp_port = 25    
username = 'bbbb.aaaa@mmmmm.com'    
password = 'mypassword'    
from_email = 'bbbb.aaaa@mmmmm.com'    
to_email = 'bbbb.aaaa@mmmmm.com'    
subject = 'PHPMailer'    
body = '3333333333'    

   
# Prepare the email    
msg = MIMEMultipart()
msg['From'] = from_email    
msg['To'] = to_email    
msg['Subject'] = subject    
msg.attach(MIMEText(body, 'html'))

   
try:
    # Create a secure SSL context    
    print("AAAAAA")
    server = smtplib.SMTP("10.54.21.20", 25, timeout=10) #这里报异常,可能是 25 端口有问题    
    print("BBBBBB")

   
    # Uncomment the following line if SMTP authentication is needed    
    # server.login(username, password)    

   
    # Send the email    
    server.send_message(msg)
    print("Message sent!")

   
except Exception as e:
    print(f"Mailer Error: {e}")
finally:
    server.quit()


普通分类: