Outlook COM: Setting account to send from

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
Location: Middle of the round cube
Contact:

Outlook COM: Setting account to send from

28 Jul 2017, 06:28

Yet another MS Office COM/Outlook thread.

I looked through as many tutorials and so forth as I could. But I still need help. I have this much from here: https://autohotkey.com/board/topic/1147 ... mail-task/

Code: Select all

Outlook := ComObjActive("Outlook.Application")
email := Outlook.Application.CreateItem(0)
email.To := "[email protected]"
email.Body := "this is a test"
email.Subject := "COM email test with attachment"
email.Attachments.Add("some full path", 1, 1, "some full path")
email.Send()
This works slick. but in Micker's list here: https://autohotkey.com/board/topic/7133 ... for-ahk-l/ I cannot find any object to set the Account to send the email from. In Outlook I have my own email address, 1 or 2 others, and one that I want to send the automatic emails from. Is there not a line or 2 that could set that? Or does this not make sense?
Last edited by derz00 on 31 Jul 2017, 12:38, edited 1 time in total.
try it and see
...
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Outlook COM: Setting account to send from

28 Jul 2017, 06:32

SentOnBehalfOfName but you need SendAs rights for this
email.SentOnBehalfOfName := "[email protected]"
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
Location: Middle of the round cube
Contact:

Re: Outlook COM: Setting account to send from

28 Jul 2017, 06:51

OK. Great, it works. I should have tried that one. I thought it would send it like "[email protected] on behalf of [email protected]" like you can do sometimes.

Anyway, thanks for pointing it out.
Sadly, it isn't the solution. ↓
Last edited by derz00 on 31 Jul 2017, 12:40, edited 1 time in total.
try it and see
...
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
Location: Middle of the round cube
Contact:

Re: Outlook COM: Setting account to send from

28 Jul 2017, 07:12

Oh.

Wait a minute. That is what it did. I was looking at the email in a different mail client. Now I open it in Outlook and it says "[email protected] on behalf of [email protected]"

So that isn't the solution, sadly.

Maybe I can find some other way to do it, but it seems to me this should be easy to do. ?
try it and see
...
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
Location: Middle of the round cube
Contact:

Re: Outlook COM: Setting account to send from

31 Jul 2017, 12:38

Bump.

Can someone help me with this? I want to send an email, but not with my default email account. Isn't there a line I could use to do this? What am I missing?

Thanks.
try it and see
...
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Outlook COM: Setting account to send from

31 Jul 2017, 15:51

Below is an example of creating an HTML email and using the SendUsingAccount property.

Code: Select all

Recipient := "Them <[email protected]>"
RecipientCC := "Us <[email protected]>"
RecipientBCC1 := "Sneaky <[email protected]>"
RecipientBCC2 := "Very Sneaky <Very [email protected]>"
Subject := "Reports"
Body = 
(Join
<a href="http://www.ahkscript.org/">http://www.ahkscript.org/</a><br>
<img src="%A_Desktop%\Temp\PictureTest.gif" />
)

;example of creating a MailItem and setting it's format to HTML
olApp := ComObjCreate("Outlook.Application")
olEmail := olApp.CreateItem(0)	; olMailItem := 0
olNameSpace := olApp.GetNamespace("MAPI")
olEmail.SendUsingAccount := olNameSpace.Accounts.Item("DisplayName of Account to Use") ; Can also be an index number
olEmail.BodyFormat := 2	; olFormatHTML := 2
olEmail.Subject := Subject
olEmail.HTMLBody := Body
olRecipient := olEmail.Recipients.Add(Recipient)
olRecipient.Type := 1 ; To: CC: = 2 BCC: = 3
olRecipient := olEmail.Recipients.Add(RecipientCC)
olRecipient.Type := 2 ; To: CC: = 2 BCC: = 3
olRecipient := olEmail.Recipients.Add(RecipientBCC1)
olRecipient.Type := 3 ; To: CC: = 2 BCC: = 3
olRecipient := olEmail.Recipients.Add(RecipientBCC2)
olRecipient.Type := 3 ; To: CC: = 2 BCC: = 3
olEmail.Attachments.Add(A_Desktop "\Temp\Temp.jpg")

olEmail.Display
olNameSpace is all about the account and folder information you typically see on the left side of Outlook.

FG

(Edit: Added more options and examples for defining things in an email)
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Vh_
Posts: 203
Joined: 17 Mar 2017, 22:06

Re: Outlook COM: Setting account to send from

31 Jul 2017, 18:24

Alternately, does anyone know how to have a script read emails and if it containts X, do Y? :D
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Outlook COM: Setting account to send from

31 Jul 2017, 18:38

Vh_ wrote:Alternately, does anyone know how to have a script read emails and if it containts X, do Y? :D
Yes, but would need more clarity. Two big questions are which emails and where in the email?
Also depending on what you want to do when found, it might be worth considering if this would be easier with a "Rule" in Outlook.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Vh_
Posts: 203
Joined: 17 Mar 2017, 22:06

Re: Outlook COM: Setting account to send from

31 Jul 2017, 18:40

The idea would be a loop to read the text inside of the new emails. If it contained "This ID is now associated with" then it would then trigger a script to do X with that code, outside of outlook.
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Outlook COM: Setting account to send from

31 Jul 2017, 18:58

Vh_ wrote:The idea would be a loop to read the text inside of the new emails. If it contained "This ID is now associated with" then it would then trigger a script to do X with that code, outside of outlook.
Like new emails as they are received or new emails as they are sent? Or all emails in the Inbox? All unread emails?

New emails is still vague.

When you press a key? Automatically based on some event?

I am not trying to be difficult but you have to very clearly state what you want.

It is all relatively easy to do. Meaning it would probably only take a dozen or two lines of code.

For example:

Code: Select all

F12::
    Emails := ComObjActive("Outlook.Application").ActiveExplorer.Selection
    For Email in Emails
		if (Email.Body ~= "i)please")
			MsgBox % "Subject: " Email.Subject "`nContains the word 'Please'" 
return
This code will allow you to select as many emails as you want in the Explorer window of Outlook then push F12 to list the subject of all the emails that contain "please" regardless of case.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Vh_
Posts: 203
Joined: 17 Mar 2017, 22:06

Re: Outlook COM: Setting account to send from

31 Jul 2017, 19:15

Sorry, great questions.

Newly recieved emails. this would just be a continuous loop always running, checking the inbox.
if I received the email it would prompt me before executing, so I could finish my current task at work.
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Outlook COM: Setting account to send from

31 Jul 2017, 19:31

Vh_ wrote:Sorry, great questions.

Newly recieved emails. this would just be a continuous loop always running, checking the inbox.
if I received the email it would prompt me before executing, so I could finish my current task at work.
If you want it to check all emails as they are received there is probably not a need for a loop. Outlook has events that can be used to trigger an action. Uses a little more obscure code that I will have to look up the exact event name but still just a few lines. Got to go for now but will see about posting an example tomorrow.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Vh_
Posts: 203
Joined: 17 Mar 2017, 22:06

Re: Outlook COM: Setting account to send from

31 Jul 2017, 20:00

Thanks, its not a big deal, just would be fun to know how and if it could be done. In reality, I probably wouldnt use it a whole lot, but might keep me on task when its been two weeks since the request has been processed.. L O L
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
Location: Middle of the round cube
Contact:

Re: Outlook COM: Setting account to send from

01 Aug 2017, 07:45

FanaticGuru wrote:Below is an example of creating an HTML email and using the SendUsingAccount property.
olNameSpace is all about the account and folder information you typically see on the left side of Outlook.

FG

(Edit: Added more options and examples for defining things in an email)
Thanks a lot. COM is a amazing, and your example is well done. This is just what I needed.


@Vh_ check out outlook mail rules. They can check for text in the body, and I think there would be a way for the rule to trigger an AHK script, but not totally sure. See what @FG has in mind...
try it and see
...
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Outlook COM: Setting account to send from

01 Aug 2017, 18:53

Vh_ wrote: Newly recieved emails. this would just be a continuous loop always running, checking the inbox.
if I received the email it would prompt me before executing, so I could finish my current task at work.
Below are several event triggers that can be used related to new emails in Outlook:

This is the most basic. It triggers the procedure anytime a new email is received.

Code: Select all

olApp := ComObjActive("Outlook.Application")
ComObjConnect(olApp, "EventApp_")

EventApp_NewMail()
{
	MsgBox % NEW MAIL
}
It does not give you a handle to the new email though and it can be problematic to figure out what email was received if several are received in the same download.

This is better. The procedure is passed a list of IDs for all emails downloaded in the last batch.

Code: Select all

 ;~ NEW MAIL USING EXCHANGE LIST
global olApp := ComObjActive("Outlook.Application")
ComObjConnect(olApp, "EventApp_")

EventApp_NewMailEx(IDs)
{
	IDs := StrSplit(IDs, ",")
	for index, ID in IDs
		MsgBox % olApp.Session.GetItemFromID(ID).Subject
}
Another approach is to set an event trigger for when a new item is added to a folder.

Code: Select all

olApp := ComObjActive("Outlook.Application")
olNameSpace := olApp.GetNamespace("MAPI")
olFolderItems := olNameSpace.Folders("Account Display Name").Folders("Inbox").Folders("Important Stuff").Items

ComObjConnect(olFolderItems, "EventFolderItems_")

EventFolderItems_ItemAdd(olItem)
{
	MsgBox % olItem.Subject
}
This will trigger everytime a new item is added to the Important Stuff subfolder of the Inbox for a specific account.

Connecting to an objects events is very similar to the way hotkeys work. Hotkeys are events that trigger a procedure. In all of these examples the "Event" procedure is trigger anytime the event occurs in the object connected.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
emretoprak
Posts: 19
Joined: 14 Jun 2017, 07:56

Re: Outlook COM: Setting account to send from

30 Aug 2017, 09:04

FanaticGuru wrote:Below is an example of creating an HTML email and using the SendUsingAccount property.

Code: Select all

Recipient := "Them <[email protected]>"
RecipientCC := "Us <[email protected]>"
RecipientBCC1 := "Sneaky <[email protected]>"
RecipientBCC2 := "Very Sneaky <Very [email protected]>"
Subject := "Reports"
Body = 
(Join
<a href="http://www.ahkscript.org/">http://www.ahkscript.org/</a><br>
<img src="%A_Desktop%\Temp\PictureTest.gif" />
)

;example of creating a MailItem and setting it's format to HTML
olApp := ComObjCreate("Outlook.Application")
olEmail := olApp.CreateItem(0)	; olMailItem := 0
olNameSpace := olApp.GetNamespace("MAPI")
olEmail.SendUsingAccount := olNameSpace.Accounts.Item("DisplayName of Account to Use") ; Can also be an index number
olEmail.BodyFormat := 2	; olFormatHTML := 2
olEmail.Subject := Subject
olEmail.HTMLBody := Body
olRecipient := olEmail.Recipients.Add(Recipient)
olRecipient.Type := 1 ; To: CC: = 2 BCC: = 3
olRecipient := olEmail.Recipients.Add(RecipientCC)
olRecipient.Type := 2 ; To: CC: = 2 BCC: = 3
olRecipient := olEmail.Recipients.Add(RecipientBCC1)
olRecipient.Type := 3 ; To: CC: = 2 BCC: = 3
olRecipient := olEmail.Recipients.Add(RecipientBCC2)
olRecipient.Type := 3 ; To: CC: = 2 BCC: = 3
olEmail.Attachments.Add(A_Desktop "\Temp\Temp.jpg")

olEmail.Display
olNameSpace is all about the account and folder information you typically see on the left side of Outlook.

FG

(Edit: Added more options and examples for defining things in an email)
Hi all,

I am trying to use the code above to add reminder to the emails I create using COM. However I could not find how to access some features of outlook. First, I want to change the Send From to another account rather than using the standard account. I have 4 accounts that I could use to send emails. One is my standard account, two of them are also separate accounts that my colleagues also use, and one account I use the send emails in behalf of a generic email.

Is it possible to change it somehow?

Code: Select all

oOutlook := ComObjCreate("Outlook.Application") ; create Outlook object
oOutlookMsg := oOutlook.CreateItem(0) ; 0 = Outlook Mail Item
olNameSpace := oOutlook.GetNamespace("MAPI")
oOutlookMsg.SendUsingAccount := olNameSpace.Accounts.Item("[email protected]")
; This does not work. Apparently I can't read the NameSpace correctly.

Second, I want to include a reminder to every email that I create using ComObjCreate. Is it possible and how can I do that?

I try to understand the syntax but apparently AutoHotkey COM is not exactly same as VB or VBA and some code won't be interpreted by AHK as expected.

Cheers,
Emre
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Outlook COM: Setting account to send from

30 Aug 2017, 12:33

emretoprak wrote:

Code: Select all

oOutlook := ComObjCreate("Outlook.Application") ; create Outlook object
oOutlookMsg := oOutlook.CreateItem(0) ; 0 = Outlook Mail Item
olNameSpace := oOutlook.GetNamespace("MAPI")
oOutlookMsg.SendUsingAccount := olNameSpace.Accounts.Item("[email protected]")
; This does not work. Apparently I can't read the NameSpace correctly.
This basically looks correct unless there is a typo in there somewhere.
I know for a fact that this works for me:

Code: Select all

olApp := ComObjCreate("Outlook.Application")
olEmail := olApp.CreateItem(0)	; olMailItem := 0
olNameSpace := olApp.GetNamespace("MAPI")
olEmail.SendUsingAccount := olNameSpace.Accounts.Item("DisplayName of Account to Use") ; Can also be an index number
It is important to realize that the "[email protected]" is requiring the displayname of the account which often defaults to the email address but an account can have a different displayname than its email address.

Here is code that will display all of your accounts displaynames:

Code: Select all

olApp := ComObjCreate("Outlook.Application")
olEmail := olApp.CreateItem(0)	; olMailItem := 0
olNameSpace := olApp.GetNamespace("MAPI")
x := 0
Loop
{
	x++
	try
		MsgBox % x "`t" olNameSpace.Accounts.Item(x).DisplayName
	catch
		break
}
You need to use one of these exact displaynames or index numbers to refer to the account you want to use.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
Location: Middle of the round cube
Contact:

Re: Outlook COM: Setting account to send from

30 Aug 2017, 12:43

FanaticGuru wrote: Here is code that will display all of your accounts displaynames:
Great code, I was wishing for that when I set mine up, but it worked to just type in the Display Name that you can see.
try it and see
...
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
Location: Middle of the round cube
Contact:

Re: Outlook COM: Setting account to send from

30 Aug 2017, 12:48

emretoprak wrote: Second, I want to include a reminder to every email that I create using ComObjCreate. Is it possible and how can I do that?

I try to understand the syntax but apparently AutoHotkey COM is not exactly same as VB or VBA and some code won't be interpreted by AHK as expected.

Cheers,
Emre
Just curious (I likely won't be smart enough to help you), what exactly do you mean by "reminders"?
try it and see
...
emretoprak
Posts: 19
Joined: 14 Jun 2017, 07:56

Re: Outlook COM: Setting account to send from

31 Aug 2017, 01:19

I will try and see if I can manage to figure out the display name of my account.

@derz00: I mean the reminder function of outlook. In outlook it is possible to set a reminder for an email so that you check the status. It can found under Follow Up --> Add Reminder

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, Lamron750, mikeyww and 231 guests