Reputation: 2448
When sending an email using PowerShell's Send-MailMessage -From 'Some person <[email protected]>
Outlook will display the sender in the format of Some Person <[email protected]>
yet when Outlook is used to send the email, the sender is displayed simply as Some Person
Is there any way to get the Send-MailMessage
cmdlet to format the mail message so Outlook will only display the Name and not the Name + Email address?
Searching around on this topic returns plenty of "How to use Send-MailMessage" or answers using .net
but doesn't really address this question directly.
Upvotes: 2
Views: 8561
Reputation: 174485
The MailAddress
class has a DisplayName
property you can use to override this.
Unfortunately, Send-MailMessage
only accepts a string as argument to the -From
parameter
Try using the SmtpClient
class manually, then overriding the display name of the from address with this MailAddress
constructor:
# Create message, add From mailaddress with custom display name
$Message = New-Object System.Net.Mail.MailMessage
$Message.From = New-Object System.Net.Mail.MailAddress '[email protected]','Some Person'
$Message.To.Add('[email protected]')
$Message.Subject = 'Exciting email!'
$Message.Body = @'
Hi Recipient
Check my cool display name in Outlook!
Regards
Some Person
'@
# Send using SmtpClient
$SmtpClient = New-Object System.Net.Mail.SmtpClient 'mailserver.fqdn'
$SmtpClient.Send($Message)
Upvotes: 3