Joh
Joh

Reputation: 337

How to send Email from secondary Account in Outlook and Python

I have access to a second email account and I want to send automated emails with this email. I already tried THIS and THIS but it still sends mails with my primary account and not with the second one. I am using python with outlook.

Here is my code:

import os
import csv

def Emailer(message, subject, recipient, anrede, name):
    import win32com.client as win32   

    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = recipient
    mail.Subject = subject
    mail.GetInspector 

    header = 'MyHeader'
    message = 'MyHTMLMessage'

    index = mail.HTMLbody.find('>', mail.HTMLbody.find('<body')) 
    mail.HTMLbody = "<font size=-1 face='Arial'>" + mail.HTMLbody[:index + 1] + header + message + mail.HTMLbody[index + 1:] 

    mail.send


with open('Komplette Liste.csv', 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter=';')
csv_list = list(reader)

row_count = sum(1 for row in csv_list)

for i in range(1,row_count):    
            unternehmen = str(csv_list[i][0])
            mail_address = str(csv_list[i][7])
            name = str(csv_list[i][8])

            infomail_count = infomail_count + 1 
            print(mail_address)

            Emailer("", "My Subject", "MailTo")

I would appreciate your help!

Upvotes: 0

Views: 3357

Answers (2)

Samnater
Samnater

Reputation: 77

Try using mail.SentOnBehalfOfName = '[email protected]'

Upvotes: 2

Alina Li
Alina Li

Reputation: 884

You could try the below code:

import win32com.client
o = win32com.client.Dispatch("Outlook.Application")
oacctouse = None
for oacc in o.Session.Accounts:
  if oacc.SmtpAddress == "[email protected]":
    oacctouse = oacc
    break

#print oacc   
#dir(oacc)
#oacc.CLSID
#oacc.GetAddressEntryFromID
Msg = o.CreateItem(0)
if oacctouse:
   Msg._oleobj_.Invoke(*(64209, 0, 8, 0, oacctouse))  # Msg.SendUsingAccount = oacctouse
Msg.To="[email protected]"    
Msg.HTMLBody = "test env instance #"
Msg.Send()

For more information, please refer to this link:

How to use RDCOMClient to send Outlook email from a secondary account - translate existing VBA code?

Upvotes: 1

Related Questions