Python client.Dispatch("outlook.application") fails in specific case (#company emails) - python

Hi there,
Everything is working fine, except one thing I can't send email to clients that have non-standard email (if their email are #gmail, #outlook, etc, it sends the email normally)
I have a client with the following email client#company.com (not the real email obviously), I can't send that email to him. There goes my method. I'd love some help.
mail.Send()
File "", line 2, in Send pywintypes.com_error:
(-2147024809, 'The parameter is incorrect.', None, None)
def main_send_email(self, to, header, attached_msg, pdf_files=None):
import pythoncom
# return super().main_send_email(to, header, attached_msg, pdf_files)
self.outlook_app = client.Dispatch(
'outlook.application', pythoncom.CoInitialize())
s = client.Dispatch("Mapi.Session")
mail = self.outlook_app.CreateItem(0)
# set the account
account = None
for acc in mail.Session.Accounts:
if "39" in acc.DisplayName:
account = acc
mail._oleobj_.Invoke(*(64209, 0, 8, 0, account))
# mail.SendUsingAccount = self.outlook_app.Session.Accounts.Item(1)
# set email sender
# mail.To = 'silsilinhas#gmail.com'
mail.To = to
mail.Subject = header
mail.HTMLBody = attached_msg
if pdf_files is not None:
for pdf in pdf_files:
mail.Attachments.Add(pdf)
mail.Send()

First of all, I've noticed the following line of code which is useless and never used:
s = client.Dispatch("Mapi.Session")
It seems there is no need to create a new MAPI session in the code if you automate Outlook.
Second, the Send method may trigger a security issue when automating Outlook from external applications. It can be a security prompt or just an exception thrown at runtime. There are several ways for suppressing such prompts/issues:
Use a third-party components for suppressing Outlook security warnings. See Security Manager for Microsoft Outlook for more information.
Use a low-level API which doesn't trigger such issues/prompts instead of OOM. Or any other third-party wrappers around that API, for example, Redemption.
Develop a COM add-in which has access to the trusted Application object. And then communicate from a standalone application with an add-in using standard .Net tools (Remoting).
Use group policy objects for setting up machines to not throw such issues.
Install the latest AV software.
Third, you may try to set recipients for the mail item using the Recipients collection which provides the Resolve method which attempts to resolve a Recipient object against the Address Book. Read more about that in the How To: Fill TO,CC and BCC fields in Outlook programmatically article.

In a program i wrote I have found the error "(-2147024809, 'The parameter is incorrect.', None, None)" whenever I try using mail.Send() before calling mail.Display().
Seems fairly consistent but can't explain why!
Hope this helps (sorry if it doesn't!)
...
if pdf_files is not None:
for pdf in pdf_files:
mail.Attachments.Add(pdf)
mail.Display() ##this line here
mail.Send()

Related

Sending emails with outlook using Python and HTML

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, "from#outlook.com" 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="to#outlook.com"
message.CC=""
message.BCC=""
From = "from#outlook.com"
#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()
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.

how to send email in internal/restricted/ highly restricted mood from outlook in python

I am using win32com.client liberary.
import win32com.client as client
outlook = client.Dispatch("Outlook.Application")
message = outlook.CreateItem(0)
message.To = "xxxx#officemail.com"
message.Subject = "abcd"
message.Body = "abc xyz"
message.Send()
Here how to add option as internal/restricted/ highly restricted ?
Without adding I am getting error as follows:
com_error: (-214747260,"Operation aborted",None,None)
The Outlook object model doesn't provide any built-in property for that.
If you use AIP add-in in Outlook you need to add a corresponding user property by using the UserProperties.Add method. Read more about API labeling in the Azure Information Protection (AIP) labeling, classification, and protection article. You can explore existing items and their labels using any low-level exploring tool such as MFCMAPI or OutlookSpy.
You may find the What is Azure Information Protection? page helpful.

Sending email via Outlook using Python, contact group doesn't resolve

The recipient address and the distro lists do not immediately resolve. Outlook is smart enough to recognize the email addresses, but it doesn't recognize my distro list. At least not immediately.
I'm using Office 365 if that matters.
from win32com.client import Dispatch
outlook = Dispatch('Outlook.Application')
Mail_Item = outlook.CreateItem(0)
# This sends no problem
# Mail_Item.To = 'first.last#company.com'
# This does not send
Mail_Item.To = 'Contact_Group_Test'
Mail_Item.Subject = "Subject_text"
Mail_Item.Body = "Body_text"
Mail_Item.Recipients.ResolveAll()
Mail_Item.Send()
While troubleshooting I used Mail_Item.Display() to see the message. After a few seconds it resolves all my addresses, including the contact group. HOWEVER, the contact group itself still doesn't work despite this.
If you couldn't solve, you can try smtplib.
import smtplib
sender = 'from#fromdomain.com'
receivers = ['to#todomain.com']
message = """From: From Person <from#fromdomain.com>
To: To Person <to#todomain.com>
Subject: SMTP e-mail test
This is a test e-mail message.
"""
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, message)
print "Successfully sent email"
except SMTPException:
print "Error: unable to send email"
If you will send mail from outlook account, you have to write smtp-mail.outlook.com instead of localhost
mtbenj's answer may work for others. I'm just choosing to go the win32com route because of my organization.
For whatever reason using 'Test', and I'm assuming, any one-word contact list takes ~5 seconds to resolve. I counted after the .Display() popup came up. Naming it 'Test_' didn't do it for me either.
I decided to try one named 'Test_Test'. After calling .Recipients.ResolveAll(), it works perfect.
So use an underscore and multiple words.

receiving emails with python api O365

I am just starting out in Python and I am trying to accomplish a manual task I have heard is on the simpler side to accomplish with python. My company uses Office 365 for their emails and I want to retrieve an email attachment and store it locally so I can save time . So far have established how to send a simple email, call the names of the folders in my account but I cannot figure out how to read any specific email .
my idea goes a little like this ,
from O365 import Account, message,mailbox
credentials = ('username', 'given password')
account = Account(credentials)
mailbox = account.mailbox()
mail_folder = mailbox.inbox_folder()
mail_folder = mailbox.get_folder(folder_name='Inbox')
print(mail_folder)
#_init__(*,parent= Inbox, con=None,**kwargs)
Message_body = message.body()
message.get_subject('email subject here!')
print(Message.body)
right now I am lost and trying anything within the O365 documentation page but the message module does not have the attribute subject according to how I am using it . Any guidance would be much appreciated
From your example - it's not clear if you are authenticated or not...
If you are then you will be able to list the mailbox folders. In the case below - you can access the inbox and then list the sub-folders:
from O365 import Account, Connection, MSGraphProtocol, Message, MailBox, oauth_authentication_flow
scopes=['basic', 'message_all']
credentials=(<secret>, <another secret>)
account = Account(credentials = credentials)
if not account.is_authenticated: # will check if there is a token and has not expired
account.authenticate(scopes=scopes)
account.connection.refresh_token()mailbox = account.mailbox()
inbox = mailbox.get_folder(folder_name='Inbox')
child_folders = inbox.get_folders(25)
for folder in child_folders:
print(folder.name, folder.parent_id)
This part will allow you to list folders (and also messages).
If I look at your code - it looks as though you are trying to do both?
Try doing something like the following to get the hang of paging through your inbox:
for message in inbox.get_messages(5):
if message.subject == 'test':
print(message.body)
Note that I'm looping through the first 5 messages in the inbox looking for a message with subject 'test'. If it finds the message - then it prints the body.
Hopefully this will shed a little light.

Create outlook draft email in python with out launching outlook application

I need to create an email draft and save in msg format without launching the outlook application.
(Or)
I have an existing draft msg file, I need to modify the sender, body, and attachment to that file and save as msg file.
I tried win32 it is working fine, but it is launching the outlook application in my system. In my server, there is no outlook application.
Can you please tell me is there any other ways to generate the msg file.
If you don't want to to use the Outlook Object Model, you are pretty much limited to either using a library like Aspose (it handles MSG files without having to install Outlook, but your mileage may vary) or Redemption (disclosure: I am its author) - it requires the MAPI system to be installed (which means Outlook must be installed), but it won't start Outlook if you are using RDOSession.CreateMsgFile (ollowed by setting various RDOMail properties and/or importing an existing MSG file using RDOMail.Import followed by RDOMail.Save.
Update per OP request.
I don't use Python, but in VB script it would be something like the following:
Set Session = CreateObject("Redemption.RDOSession")
set newMsg = Session.CreateMessageFromMsgFile("c:\temp\new.msg")
newMsg.Import("c:\temp\template.msg", 3)
newMsg.Body = "updated body"
newMsg.Save
You can create an email draft and save it as MSG with Aspose.Email for Python via .NET using the code sample given below:
eml = MailMessage()
# Set from, to, subject and body properties
eml.from_address = "sender#domain.com";
eml.to.append("receiver#domain.com");
eml.subject = "This is test message";
eml.body = "This is test body";
# Create an instance of the MapiMessage class and pass MailMessage as argument
outlookMsg = MapiMessage.from_mail_message(eml);
# Save the message (MSG) file
strMsgFile = "CreatingAndSavingOutlookMessages_out.msg"
outlookMsg.save(dataDir + strMsgFile);
Note: I am working as Support developer/ Evangelist at Aspose.

Categories

Resources