I need to click on an opened outlook email, a specific approve link that says "Approve request".
I opened the wanted email correctly, but I can't click on the specific link. Here is the code:
import win32com.client
outlook=win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox=outlook.GetDefaultFolder(6)
messages = inbox.Items
messages.Sort("[ReceivedTime]", Descending=True)
for i in range(100):
message = messages.GetNext()
print("" message.Subject, str(message.ReceivedTime))
if message.Subject == "Mail to approve request":
message.Display(False)
else:
pass
CodePudding user response:
You will need to parse the MailItem.HTMLBody
property, extract the relevant link, launch the browser with that link.
Also, never loop through all items in a folder, use Items.Find/FindNext
or Items.Restrict
with a query like "[Subject]='Mail to approve request'"
CodePudding user response:
There are several ways to open a hyperlink programmatically in Python. The How to open a URL in python page explains possible scenarios. For example:
import os
os.system("start \"\" https://example.com")
Use the HTMLBody property which returns the a string representing the HTML body of the specified item. So, you may find the required URL programmatically by parsing the message body and execute it programmatically.
And, finally, to find items that correspond to your conditions use the Find
/FindNext
or Restrict
methods of the Items
class. Read more about them in the following articles:
- How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)
- How To: Use Restrict method to retrieve Outlook mail items from a folder
See Filtering Items Using Query Keywords for building a search criteria string properly.
Also you may find the AdvancedSearch
method of the Application class helpful. The key benefits of using the AdvancedSearch
method in Outlook are:
- The search is performed in another thread. You don’t need to run another thread manually since the
AdvancedSearch
method runs it automatically in the background. - Possibility to search for any item types: mail, appointment, calendar, notes etc. in any location, i.e. beyond the scope of a certain folder. The
Restrict
andFind
/FindNext
methods can be applied to a particularItems
collection (see theItems
property of theFolder
class in Outlook). - Full support for DASL queries (custom properties can be used for searching too). You can read more about this in the Filtering article in MSDN. To improve the search performance, Instant Search keywords can be used if Instant Search is enabled for the store (see the
IsInstantSearchEnabled
property of theStore
class). - You can stop the search process at any moment using the
Stop
method of theSearch
class.
Read more about this method in the Advanced search in Outlook programmatically: C#, VB.NET article.