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

Adding a Comment to a GPO with PowerShell

As I'm writing this article, I'm also writing a customization for a PowerShell course I'm teaching next week in Phoenix.  This customization deals with Group Policy and PowerShell.  For those of you who attend my classes may already know this, but I sit their and try to ask the questions to myself that others may ask as I present the material.  I finished up my customization a few hours ago and then I realized that I did not add in how to put a comment on a GPO.  This is a feature that many Group Policy Administrators may not be aware of. This past summer I attended a presentation at TechEd on Group Policy.  One organization in the crowd had over 5,000 Group Policies.  In an environment like that, the comment section can be priceless.  I always like to write in the comment section why I created the policy so I know its purpose next week after I've completed 50 other tasks and can't remember what I did 5 minutes ago. In the Group Policy module for PowerShell V3, th

Return duplicate values from a collection with PowerShell

If you have a collection of objects and you want to remove any duplicate items, it is fairly simple. # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   # Remove the duplicate values. $Set1 | Select-Object -Unique 1 2 3 4 5 6 7 What if you want only the duplicate values and nothing else? # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   #Create a second collection with duplicate values removed. $Set2 = $Set1 | Select-Object -Unique   # Return only the duplicate values. ( Compare-Object -ReferenceObject $Set2 -DifferenceObject $Set1 ) . InputObject | Select-Object – Unique 1 2 This works with objects as well as numbers.  The first command creates a collection with 2 duplicates of both 1 and 2.   The second command creates another collection with the duplicates filtered out.  The Compare-Object cmdlet will first find items that are diffe

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.