Hier ist ein Beispielcode für das Senden einer Email mit dem Paket SMTPLib.
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# Email configuration
smtp_server = 'smtp.example.com'
smtp_port = 587
smtp_username = 'your_email@example.com'
smtp_password = 'your_password'
# Email content
from_email = 'your_email@example.com'
to_email = 'recipient@example.com'
subject = 'Test Email'
body = 'This is a test email sent using Python.'
# Create a MIME object
message = MIMEMultipart()
message['From'] = from_email
message['To'] = to_email
message['Subject'] = subject
message.attach(MIMEText(body, 'plain'))
# Send the email
try:
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls() # Secure the connection
server.login(smtp_username, smtp_password) # Log in to the server
server.sendmail(from_email, to_email, message.as_string()) # Send the email
print('Email sent successfully.')
except Exception as e:
print(f'Error: {e}')