0

I already referred this post and post. Don't mark it as duplicate please.

I have 2 accounts in my outlook application (user1@org.com - default profile ,user2@org.com)

I 'am trying to send an email message from user2@org.com (which is automated email box(department email box) configured in my email box)

When I tried the below

for acc in outlook.Session.Accounts:
    print(acc)  

It prints both email accounts which is user1@org.com and user2@org.com

However, when I do the below

outlook.Session.Accounts[0]
outlook.Session.Accounts[1]

It returns the below message

<COMObject <unknown>> #but in for loop above, it returns the name of email account

So, I finally did the below

From = 'user2@org.com' # I manually entered the from address
mail.To = 'test@org.com'
mail.Subject = 'Test Email'
mail.HTMLBody = '<h3>This is HTML Body</h3>'
mail._oleobj_.Invoke(*(64209, 0, 8, 0, From)) # this results in error

--------------------------------------------------------------------------- com_error Traceback (most recent call last) C:\Users\user~1\AppData\Local\Temp/ipykernel_14360/3581612401.py in ----> 1 mail.oleobj.Invoke(*(64209, 0, 8, 0, From))

com_error: (-2147352571, 'Type mismatch.', None, 1)

How can I properly assign the From account?

update - smtp email script

import smtplib
from email.mime.text import MIMEText

sender = 'user2@org.com'
receivers = ['user1@example.com']


port = 25
msg = MIMEText('This is test mail')

msg['Subject'] = 'Test mail'
msg['From'] = 'admin@example.com'
msg['To'] = 'info@example.com'

with smtplib.SMTP('mail-innal.org.com', port) as server:
    
    # server.login('username', 'password')
    server.sendmail(sender, receivers, msg.as_string())
    print("Successfully sent email")
The Great
  • 7,215
  • 7
  • 40
  • 128

2 Answers2

1

You are printing a COM object, which makes no sense - print is probably smart enough to attempt to retrieve the default object property (dispid = 0). If you need a meaningful name, use Account.DisplayName.

All you need to do is store the Account objct in a variable and assign it to the MailItem.SendUsingAccount property.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • would it be possible for you to write what you suggest as a code (in your answer)? – The Great Mar 29 '22 at 05:39
  • Something like `mail.SendUsingAccount = outlook.Session.Accounts[1]` – Dmitry Streblechenko Mar 29 '22 at 14:20
  • Ah I tried this already. Doesn't seem to work for some reason – The Great Mar 29 '22 at 14:37
  • What kind of account is it? Also note that all collections in OOM are 1 based, 0, so Accounts[1] will be account #1. Have you tried Accounts[2]? – Dmitry Streblechenko Mar 29 '22 at 14:48
  • Outlook account. I tried 0 and 1 because i only have two acxounts. Typing 2 as index tbrows error – The Great Mar 29 '22 at 14:55
  • I understand that it is an Outlook account - Outlook Object Model cannot contain anything but Outlook accounts, right? Is that an Exchange, POP3/SMTP, IMAP4/SMTP, etc. account? Are you sure you are using Namespace.Accounts collection? Accounts.Item() only allows value 1 through Accounts.Count – Dmitry Streblechenko Mar 29 '22 at 18:59
  • What do you meam by Namesapce above? Sorry, am new to python and outlook automation. Hence, have difficukty understanding your terminologies. If you don't mind, can you write the code that you want me to try amd I will reproduce your code at my system and report the results back? Would really be grateful – The Great Mar 30 '22 at 01:14
  • Namespace is the Outlook object returned from Application.Session or Application.GetNamespace("MAPI") - see https://learn.microsoft.com/en-us/office/vba/api/outlook.namespace – Dmitry Streblechenko Mar 30 '22 at 02:17
  • All accounts are MAPI - Extended MAPI is the native Outlook API. – Dmitry Streblechenko Mar 30 '22 at 15:24
0

Manually entered email address is a string type and cannot be assigned as the from account. You need it to be the same object type as the mail item - <class 'win32com.client.CDispatch'>

message = 'xyz'
preferred_account = 'user2@org.com'
outlook = win32.Dispatch('Outlook.Application')

mail = outlook.CreateItem(0)
mail.Subject = subject
mail.Body = message

outlook_acc_to_use = None
for outlook_acc in outlook.Session.Accounts:
    if preferred_account in str(outlook_acc):
        outlook_acc_to_use = outlook_acc
        break

if outlook_acc_to_use != None:
    mail._oleobj_.Invoke(*(64209, 0, 8, 0, outlook_acc_to_use))

mail.To = '; '.join(receiver_emails)
mail.Send()
QP1
  • 15
  • 4