xxxxxxxxxx
import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'To address'
mail.Subject = 'Message subject'
mail.Body = 'Message body'
mail.HTMLBody = '<h2>HTML Message body</h2>' #this field is optional
# To attach a file to the email (optional):
attachment = "Path to the attachment"
mail.Attachments.Add(attachment)
mail.Send()
xxxxxxxxxx
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# Sender's email credentials
sender_email = "<sender-email-address>"
sender_password = "<sender-email-password>"
# Receiver's email details
receiver_email = "<receiver-email-address>"
subject = "Test Email from Python"
message = "Hello, this is a test email sent from Python!"
# Constructing the email
msg = MIMEMultipart()
msg["From"] = sender_email
msg["To"] = receiver_email
msg["Subject"] = subject
msg.attach(MIMEText(message, "plain"))
try:
# Establishing a secure connection with the SMTP server
server = smtplib.SMTP("smtp.office365.com", 587)
server.starttls()
# Logging in to the sender's email account
server.login(sender_email, sender_password)
# Sending the email
server.sendmail(sender_email, receiver_email, msg.as_string())
print("Email sent successfully!")
except Exception as e:
print(f"Error: {str(e)}")
finally:
# Closing the connection to the SMTP server
server.quit()
xxxxxxxxxx
import win32com.client
def send_email(subject, body, recipient):
outlook = win32com.client.Dispatch("Outlook.Application")
mail = outlook.CreateItem(0) # Creates a new mail item
mail.Subject = subject
mail.Body = body
mail.Recipients.Add(recipient)
# You can add additional settings and recipients if needed
# For example, the following line sets the email importance to high
# mail.Importance = 2
mail.Send()
# Usage example:
subject = "Hello from Python!"
body = "This is the body of the email."
recipient = "example@example.com"
send_email(subject, body, recipient)