Advanced Windows PowerShell Scripting Video Training

Advanced Windows PowerShell Scripting Video Training
Advanced Windows PowerShell Scripting Video Training

Monday, April 16, 2012

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
 

2 comments:

xajuan 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.