Home > Back-end >  Sending emails with outlook using Python and HTML
Sending emails with outlook using Python and HTML

Time:01-18

How do I successfully use a sender account with a password that is not already signed in to on the Outlook application on my laptop? I want to send a message from the example account, "[email protected]" but it has a password. I have this password. Am I able to enter this into the existing code?


import win32com.client as client
import datetime,time
outlook=client.Dispatch("Outlook.Application")
message=outlook.Createitem(0)
namespace=outlook.GetNameSpace('MAPI')
inbox=namespace.GetDefaultFolder(6)
message=inbox.items.add
message.To="[email protected]"
message.CC=""
message.BCC=""
From = "[email protected]"
#password = ""

message.Subject="Subject text here"


message.BodyFormat = 2
message.HTMLBody = "<html><h2><span style='color:red'>There is an error with the file </b></span></h2> <body>Please check your submission and try again </body></html>"
message.Save()
message.Display()
time.sleep(5)

message.Send()

CodePudding user response:

Firstly, MailItem object does not expose the From property. It exposes read-only Sender / SenderEmailAddress / SenderName properties, but they obviously cannot be set. You can set the MailItem.Account property to an instance of the Account object retrieved from the Application.Session.Accounts collection, but that requires you to configure the relevant account in Outlook first.

As a general rule, Outlook wont' let you send from an arbitrary one-off account not previously configured in Outlook.

  • Related