Python群发邮件应用实例:从编写到发送
分类:默认分类
浏览:36
2024-10-28
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart sender_email = "[email protected]" receiver_email = "[email protected]" subject = "测试邮件" body = "这是一封测试邮件,由Python发送。" message = MIMEMultipart() message["From"] = sender_email message["To"] = receiver_email message["Subject"] = subject message.attach(MIMEText(body, "plain"))
smtp_server = "smtp.gmail.com" port = 587 # For starttls password = "your_password" # Create a secure SSL context context = ssl.create_default_context() with smtplib.SMTP(smtp_server, port) as server: server.ehlo() # Can be omitted server.starttls(context=context) server.ehlo() # Can be omitted server.login(sender_email, password) server.sendmail(sender_email, receiver_email, message.as_string())