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")