Send email via outlook with "From" inserted

Get help with using AutoHotkey and its commands and hotkeys
Ernestas
Posts: 33
Joined: 16 Apr 2018, 02:07

Send email via outlook with "From" inserted

18 Mar 2020, 07:35

Hello guys!

I was searching forums and could not find the info on how to send email and insert "from" (see attached image).

my code now:


subject := "subject"
recipient := "[email protected]"
emailBody:= "body text"

from := "[email protected]" ; this is the mailbox that have to be in from.

Outlook := ComObjActive("Outlook.Application")
email := Outlook.Application.CreateItem(0)
email := email .GetNamespace("MAPI")
email.To := clientInfoFromGui.customerEmail
email.Subject := subject
email.To := recipient

email.SendUsingAccount := from

email.HTMLbody := emailBody
email.Display

Thank you in advance!!!
Attachments
from outlook.png
from outlook.png (7.88 KiB) Viewed 523 times
User avatar
Jovannb
Posts: 256
Joined: 17 Jun 2014, 02:44
Location: Austria

Re: Send email via outlook with "From" inserted

18 Mar 2020, 15:47

Hi,

what you are looking for is, how to change the outlook-account sending from.

That works for me:

Code: Select all

; change sending-Account ("from") to "[email protected]"
; Example, Source of knowledge: Account: https://docs.microsoft.com/de-de/office/vba/api/outlook.account
wanted_sender_address := "[email protected]"

if not (Citem.SendUsingAccount.smtpaddress == wanted_sender_address)	; if it is set already, nothing to change
{	
	olNameSpace := olApp.GetNamespace("MAPI")
	Acc_Anzahl := olNameSpace.Accounts.count	; how many sending-accounts are registered in Outlook ?
	loop, %Acc_Anzahl%							; which of those sending-accounts has that address in ?
	{
		if (olnamespace.accounts.item(a_index).SmtpAddress == wanted_sender_address)
		{
			Citem.SendUsingAccount := olNameSpace.Accounts.Item(A_Index)
			break						; leave, we set it (the email's "from") to the email-address/sending-account we wanted
		}
	}
}
J.B.
AHK: 1.1.33.01 Ansi, 32-Bit; Win10 (2004, 19041.685) 64 bit, german
Ernestas
Posts: 33
Joined: 16 Apr 2018, 02:07

Re: Send email via outlook with "From" inserted

19 Mar 2020, 09:02

thank you for the replays!!!

This is what I have so far:

the problem is that this variable is empty: "Citem.SendUsingAccount.smtpaddress" and it only shows the first account which is nr 1 marked in the photo(see attached). Any suggestions? :crazy:

subject := "subject"
recipient := "[email protected]"
emailBody:= "body text"

Outlook := ComObjActive("Outlook.Application")
email := Outlook.Application.CreateItem(0)
email.Subject := subject
email.HTMLbody := emailBody

wanted_sender_address := "[email protected]"

if not (Citem.SendUsingAccount.smtpaddress == wanted_sender_address) ; if it is set already, nothing to change
{
olNameSpace := Outlook.GetNamespace("MAPI")
Acc_Anzahl := olNameSpace.Accounts.count ; how many sending-accounts are registered in Outlook ?

MsgBox, % "acc len: " Acc_Anzahl

loop, %Acc_Anzahl% ; which of those sending-accounts has that address in ?
{

MsgBox, % "acc name: " olnamespace.accounts.item(a_index).SmtpAddress
if (olnamespace.accounts.item(a_index).SmtpAddress == wanted_sender_address)
{
Citem.SendUsingAccount := olNameSpace.Accounts.Item(A_Index)
break ; leave, we set it (the email's "from") to the email-address/sending-account we wanted
}
}
}

email.Display

MsgBox, done
return
Attachments
email.png
email.png (26.55 KiB) Viewed 459 times
User avatar
Jovannb
Posts: 256
Joined: 17 Jun 2014, 02:44
Location: Austria

Re: Send email via outlook with "From" inserted

19 Mar 2020, 10:21

Hi,

oh sorry, because I dont want to publish the whole code, it would be more irritating than helpful, I missed an important part.

Here is what you need before the above mentioned code:

Code: Select all

	Citem:=ActiveInspector.CurrentItem			; current Item
	Citem_Class:=Citem.class					; get class, class 43 for Emails, class 26 for appointments
	
	
; change sending-Account ("from") to "[email protected]"
; Account: https://docs.microsoft.com/de-de/office/vba/api/outlook.account
wanted_sender_address := "[email protected]"

if not (Citem.SendUsingAccount.smtpaddress == wanted_sender_address)	; it it is set already, nothing to change
{	
	olNameSpace := olApp.GetNamespace("MAPI")
	Acc_Anzahl := olNameSpace.Accounts.count	; how many sending-accounts are registered in Outlook ?
	loop, %Acc_Anzahl%							; which of those sending-accounts has that address in ?
	{
		if (olnamespace.accounts.item(a_index).SmtpAddress == wanted_sender_address)
		{
			Citem.SendUsingAccount := olNameSpace.Accounts.Item(A_Index)
			break
		}
	}
}
	
regards

J.B.
AHK: 1.1.33.01 Ansi, 32-Bit; Win10 (2004, 19041.685) 64 bit, german
Ernestas
Posts: 33
Joined: 16 Apr 2018, 02:07

Re: Send email via outlook with "From" inserted

20 Mar 2020, 08:22

Hi again!

Thank you for the replay, but still I get nothing..

I do not understand what are Citem and Citem_Class. I think I am missing some of the functions or classes..

Any thoughts?

Thanks in advance!

^f1::

subject := "subject"
recipient := "[email protected]"
emailBody:= "body text"

Outlook := ComObjActive("Outlook.Application")
email := Outlook.Application.CreateItem(0)
email.Subject := subject
email.HTMLbody := emailBody

Citem:=ActiveInspector.CurrentItem
Citem_Class:=Citem.class

; Account: https://docs.microsoft.com/de-de/office/vba/api/outlook.account

wanted_sender_address := "[email protected]"
if not (Citem.SendUsingAccount.smtpaddress == wanted_sender_address)
{
olNameSpace := Outlook.GetNamespace("MAPI")
Acc_Anzahl := olNameSpace.Accounts.count
loop, %Acc_Anzahl%
{
if (olnamespace.accounts.item(a_index).SmtpAddress == wanted_sender_address)
{
Citem.SendUsingAccount := olNameSpace.Accounts.Item(A_Index)
break
}
}
}

email.Display

MsgBox, done

return

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], fiendhunter, Google [Bot], Moa, seddion, XMCQCX and 38 guests