Loading...

Send a mail using the current outlook user account without to provide credentials with PowerShell


In this post I will show you how to send a mail using the current outlook user account without to provide credentials.


Context

- You want to send a mail from the user device

- For instance to send logs without user interaction

- You don't want to provide credentials

- You don't want to provide server info

- Mail should be sent if outlook is opened or not


The solution

We will use a well known solution.

Indeed you can use COM objects with Office components like Word, Excel, Outlook, as below:

There will be a workaround depending if outlook is opened or not, we will see that.

Now let's see a mail in the below cases:

- If Outlook is closed

- If Outlook is already opened and user by the user


For both solution we will use below template:

- Dest mail: damien.vanrobaeys@gmail.components

- Subject: "Hello from $User_Name on device $Computer_Name"

- Body: "Hello from $User_Name on device $Computer_Name"


See below the code:


Error: Retrieving the COM class factory

You can have the below issue when you use the create the ComObject Outlook.Application

In my case this was because outlook was already opened, and the COM object can not be created.

In this case, we will use the second solution.


Outlook is closed

We will proceed as below:

1. Create the COM object

2. Create the mail

3. Assign destination mail to the mail

4. Assign subject to the mail

5. Assign body to the mail

6. Send the mail


Outlook is opened

We will proceed as below:

1. Use the opened Outlook object

2. Create the mail

3. Assign destination mail to the mail

4. Assign subject to the mail

5. Assign body to the mail

6. Send the mail

7. Close outlook


Add an attachment

You can add an attachment as below: 

See below the full code:

Send mail 2351482849896707074

Enregistrer un commentaire

Accueil item

Award

Join me soon

Join me soon

Endpoint Manager award

Mes articles en français

Author of Books

PowerShell GUI & WPF Group

PowerShell GUI & WPF Group

Join the FPSUGR

Join the FPSUGR

Stats