Skip to main content

How to Change the email address that PowerShell uses to send mail through Outlook

I’m using the code below to send mail from PowerShell through Outlook.

$olMailItem = 0  

$olApp = new-object -comobject outlook.application

$NewMail = $olApp.CreateItem($olMailItem)

$NewMail.Subject = "PowerShell Test e-mail"

$NewMail.To = "Jason@MCTExpert.com"

$NewMail.Body = "Have a nice day"

$NewMail.SentOnBehalfOfName = "PleaseWork@MCTExpert.com"

$NewMail.Send()

 


The problem was that the reply address was mine.  I needed to change this so I did a little bit of research.  I changed the final line of the script to


 

$olMailItem = 0  

$olApp = new-object -comobject outlook.application

$NewMail = $olApp.CreateItem($olMailItem)

$NewMail.Subject = "PowerShell Test e-mail"

$NewMail.To = "Jason@MCTExpert.com"

$NewMail.Body = "Have a nice day"

$NewMail | Get-Member
This get me the following results.  Take a look at the highlighted line.
 

Name                              MemberType Definition                                           
----                              ---------- ----------                                           
AddBusinessCard                   Method     void AddBusinessCard (ContactItem)                   
ClearConversationIndex            Method     void ClearConversationIndex ()                       
ClearTaskFlag                     Method     void ClearTaskFlag ()                                
Close                             Method     void Close (OlInspectorClose)                        
Copy                              Method     IDispatch Copy ()                                    
Delete                            Method     void Delete ()                                       
Display                           Method     void Display (Variant)                               
Forward                           Method     MailItem Forward ()                                  
GetConversation                   Method     _Conversation GetConversation ()                     
MarkAsTask                        Method     void MarkAsTask (OlMarkInterval)                     
Move                              Method     IDispatch Move (MAPIFolder)                          
PrintOut                          Method     void PrintOut ()                                     
Reply                             Method     MailItem Reply ()                                    
ReplyAll                          Method     MailItem ReplyAll ()                                 
Save                              Method     void Save ()                                         
SaveAs                            Method     void SaveAs (string, Variant)                        
Send                              Method     void Send ()                                         
ShowCategoriesDialog              Method     void ShowCategoriesDialog ()                         
Actions                           Property   Actions Actions () {get}                             
AlternateRecipientAllowed         Property   bool AlternateRecipientAllowed () {get} {set}        
Application                       Property   _Application Application () {get}                    
Attachments                       Property   Attachments Attachments () {get}                     
AutoForwarded                     Property   bool AutoForwarded () {get} {set}                    
AutoResolvedWinner                Property   bool AutoResolvedWinner () {get}                     
BCC                               Property   string BCC () {get} {set}                            
BillingInformation                Property   string BillingInformation () {get} {set}             
Body                              Property   string Body () {get} {set}                           
BodyFormat                        Property   OlBodyFormat BodyFormat () {get} {set}               
Categories                        Property   string Categories () {get} {set}                     
CC                                Property   string CC () {get} {set}                             
Class                             Property   OlObjectClass Class () {get}                         
Companies                         Property   string Companies () {get} {set}                      
Conflicts                         Property   Conflicts Conflicts () {get}                         
ConversationID                    Property   string ConversationID () {get}                       
ConversationIndex                 Property   string ConversationIndex () {get}                    
ConversationTopic                 Property   string ConversationTopic () {get}                    
CreationTime                      Property   Date CreationTime () {get}                           
DeferredDeliveryTime              Property   Date DeferredDeliveryTime () {get} {set}             
DeleteAfterSubmit                 Property   bool DeleteAfterSubmit () {get} {set}                
DownloadState                     Property   OlDownloadState DownloadState () {get}               
EnableSharedAttachments           Property   bool EnableSharedAttachments () {get} {set}          
EntryID                           Property   string EntryID () {get}                              
ExpiryTime                        Property   Date ExpiryTime () {get} {set}                       
FlagDueBy                         Property   Date FlagDueBy () {get} {set}                        
FlagIcon                          Property   OlFlagIcon FlagIcon () {get} {set}                   
FlagRequest                       Property   string FlagRequest () {get} {set}                    
FlagStatus                        Property   OlFlagStatus FlagStatus () {get} {set}               
FormDescription                   Property   FormDescription FormDescription () {get}             
GetInspector                      Property   _Inspector GetInspector () {get}                     
HasCoverSheet                     Property   bool HasCoverSheet () {get} {set}                    
HTMLBody                          Property   string HTMLBody () {get} {set}                       
Importance                        Property   OlImportance Importance () {get} {set}               
InternetCodepage                  Property   int InternetCodepage () {get} {set}                  
IsConflict                        Property   bool IsConflict () {get}                             
IsIPFax                           Property   bool IsIPFax () {get} {set}                          
IsMarkedAsTask                    Property   bool IsMarkedAsTask () {get}                         
ItemProperties                    Property   ItemProperties ItemProperties () {get}               
LastModificationTime              Property   Date LastModificationTime () {get}                   
Links                             Property   Links Links () {get}                                 
MAPIOBJECT                        Property   IUnknown MAPIOBJECT () {get}                         
MarkForDownload                   Property   OlRemoteStatus MarkForDownload () {get} {set}        
MessageClass                      Property   string MessageClass () {get} {set}                   
Mileage                           Property   string Mileage () {get} {set}                        
NoAging                           Property   bool NoAging () {get} {set}                          
OriginatorDeliveryReportRequested Property   bool OriginatorDeliveryReportRequested () {get} {set}
OutlookInternalVersion            Property   int OutlookInternalVersion () {get}                  
OutlookVersion                    Property   string OutlookVersion () {get}                       
Parent                            Property   IDispatch Parent () {get}                            
Permission                        Property   OlPermission Permission () {get} {set}               
PermissionService                 Property   OlPermissionService PermissionService () {get} {set} 
PermissionTemplateGuid            Property   string PermissionTemplateGuid () {get} {set}         
PropertyAccessor                  Property   PropertyAccessor PropertyAccessor () {get}           
ReadReceiptRequested              Property   bool ReadReceiptRequested () {get} {set}             
ReceivedByEntryID                 Property   string ReceivedByEntryID () {get}                    
ReceivedByName                    Property   string ReceivedByName () {get}                       
ReceivedOnBehalfOfEntryID         Property   string ReceivedOnBehalfOfEntryID () {get}            
ReceivedOnBehalfOfName            Property   string ReceivedOnBehalfOfName () {get}               
ReceivedTime                      Property   Date ReceivedTime () {get}                           
RecipientReassignmentProhibited   Property   bool RecipientReassignmentProhibited () {get} {set}  
Recipients                        Property   Recipients Recipients () {get}                       
ReminderOverrideDefault           Property   bool ReminderOverrideDefault () {get} {set}          
ReminderPlaySound                 Property   bool ReminderPlaySound () {get} {set}                
ReminderSet                       Property   bool ReminderSet () {get} {set}                      
ReminderSoundFile                 Property   string ReminderSoundFile () {get} {set}              
ReminderTime                      Property   Date ReminderTime () {get} {set}                     
RemoteStatus                      Property   OlRemoteStatus RemoteStatus () {get} {set}           
ReplyRecipientNames               Property   string ReplyRecipientNames () {get}                  
ReplyRecipients                   Property   Recipients ReplyRecipients () {get}                  
RetentionExpirationDate           Property   Date RetentionExpirationDate () {get}                
RetentionPolicyName               Property   string RetentionPolicyName () {get}                  
RTFBody                           Property   Variant RTFBody () {get} {set}                       
Saved                             Property   bool Saved () {get}                                  
SaveSentMessageFolder             Property   MAPIFolder SaveSentMessageFolder () {get} {set by ref}
Sender                            Property   AddressEntry Sender () {get} {set}                   
SenderEmailAddress                Property   string SenderEmailAddress () {get}                   
SenderEmailType                   Property   string SenderEmailType () {get}                      
SenderName                        Property   string SenderName () {get}                           
SendUsingAccount                  Property   Account SendUsingAccount () {get} {set}              
Sensitivity                       Property   OlSensitivity Sensitivity () {get} {set}             
Sent                              Property   bool Sent () {get}                                   
SentOn                            Property   Date SentOn () {get}                                 
SentOnBehalfOfName                Property   string SentOnBehalfOfName () {get} {set}             
Session                           Property   _NameSpace Session () {get}                          
Size                              Property   int Size () {get}                                    
Subject                           Property   string Subject () {get} {set}                        
Submitted                         Property   bool Submitted () {get}                              
TaskCompletedDate                 Property   Date TaskCompletedDate () {get} {set}                
TaskDueDate                       Property   Date TaskDueDate () {get} {set}                      
TaskStartDate                     Property   Date TaskStartDate () {get} {set}                    
TaskSubject                       Property   string TaskSubject () {get} {set}                    
To                                Property   string To () {get} {set}                             
ToDoTaskOrdinal                   Property   Date ToDoTaskOrdinal () {get} {set}                  
UnRead                            Property   bool UnRead () {get} {set}                           
UserProperties                    Property   UserProperties UserProperties () {get}               
VotingOptions                     Property   string VotingOptions () {get} {set}                  
VotingResponse                    Property   string VotingResponse () {get} {set} 
   

 
The SendOnBehalfOfName property is a string.  That is good because I can just place string that I want in there.  Also, notice the {Set} flag.  That means you can write to it.  Here is the final code.
$olMailItem = 0 
$olApp = new-object -comobject outlook.application 
$NewMail = $olApp.CreateItem($olMailItem) 
$NewMail.Subject = "PowerShell Test e-mail" 
$NewMail.To = "Jason@MCTExpert.com" 
$NewMail.Body = "Have a nice day" 
$NewMail.SentOnBehalfOfName = "PleaseWork@MCTExpert.com" 
$NewMail | Get-Member
 

Comments

Anonymous said…
This helped me out! thank you for sharing!
Fabian said…
It helped me aswell. I was not able to find this line by myself and tried with sendusingaccount which did not work.

Popular posts from this blog

How to list all the AD LDS instances on a server

AD LDS allows you to provide directory services to applications that are free of the confines of Active Directory.  To list all the AD LDS instances on a server, follow this procedure: Log into the server in question Open a command prompt. Type dsdbutil and press Enter Type List Instances and press Enter . You will receive a list of the instance name, both the LDAP and SSL port numbers, the location of the database, and its status.

How to run GPResult on a remote client with PowerShell

In the past, to run the GPResult command, you would need to either physically visit this client, have the user do it, or use and RDP connection.  In all cases, this will disrupt the user.  First, you need PowerShell remoting enabled on the target machine.  You can do this via Group Policy . Open PowerShell and type this command. Invoke-Command –ScriptBlock {GPResult /r} –ComputerName <ComputerName> Replace <ComputerName> with the name of the target.  Remember, the target needs to be online and accessible to you.

Error icon when creating a GPO Preference drive map

You may not have an error at all.  Take a look at the drive mapping below. The red triangle is what threw us off.  It is not an error.  It is simply a color representation of the Replace option of the Action field in the properties of the drive mappings. Create action This give you a green triangle. The Create action creates a new mapped drive for users. Replace Action The Replace action gives you a red triangle.  This action will delete and recreate mapped drives for users. The net result of the Replace action is to overwrite all existing settings associated with the mapped drive. If the drive mapping does not exist, then the Replace action creates a new drive mapping. Update Action The Update action will have a yellow triangle. Update will modify settings of an existing mapped drive for users. This action differs from Replace in that it only updates settings defined within the preference item. All other settings remain as configured on the mapped drive. If the