Powershell to Send Email Message

georgedschneider
georgedschneider used Ask the Experts™
on
I'm trying to write a poweshell script that sends an outbound email.  I've gotten the script to work perfectly the problem is we have a third part application that embedds a logo in all outgoing messages in the message.  Sending a message through Outlook works flawlesssly but for whatever reason when using the script for whatever reason the message isn't being sent as an HTML message so the logo is sen as an attachment to a gmail address for example.  I essentialy trying to send a test message from every user in our environment to test outgoing signatures that are applied by this same 3rd part application..  
Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -BodyAsHtml -SmtpServer $SMTPServer -port $SMTPPort -Priority High

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2014

Commented:
I don't think the problem is with your command, as that will send an HTML message.
I'd suspect it's more something to do with the third-party app.

What happens when you send a plain-text email using Outlook?  Do you get the same result as what you're seeing when sending from PS?

Does $body actually contain HTML code?

Author

Commented:
$body does not contain HTML code.  $body = Test Email.  The problem with sending a plain text email is the signature program does not add the logo when the email is sent through plain text.  I thought about writing a script to send a message through outlook but the problem is I can figure out how to use send as to send as a another user who I have the rights to send as.  The code i was using is as follows:

$ol = New-Object -comObject Outlook.Application  
$mail = $ol.CreateItem(0)  
$Mail.Recipients.Add("user@gmail.com")  
$Mail.Subject = "Testing"  
$Mail.Body = "This is my test email"  
$Mail.Send() 

Open in new window

Top Expert 2014

Commented:
The reason I asked about sending a plain-text email with Outlook is because I'm trying to figure out if your third-party app embeds the logo by looking at specific HTML code in the email.  If you see the same behavior with Outlook sending plain-text, then it would seem to indicate that adjusting your code so that $body contains HTML code would resolve the issue.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
If  $body = "This is a test".  How can I adjust the code so it contains HTML code.

Commented:
I do not see the ConvertTo-HTML command in your script, nor the style to use as HTML.

Please reference these links:
http://exchangeserverpro.com/powershell-send-html-email/
http://exchangeserverpro.com/powershell-html-email-formatting/
I was able to accomplish the task using the below code but I don't exactly understand how its worksing since the account I'm running as has neither send as or send on behalf rights but has full mailbox to all mailboxes as well a member of org. management.

$ol = New-Object -comObject Outlook.Application  
$mail = $ol.CreateItem(0)  
$Mail.SentOnBehalfOfName = "send@domain.com"
$Mail.Recipients.Add("recipient@gmail.com")  
$Mail.Subject = "Testing"  
$Mail.Body = "This is my test"  
$Mail.Send() 

Open in new window

Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
"Full mailbox access" includes "Send on Behalf".
The Outlook approach works probably because it forces mail to be HTML. You need to do the same, by sending HTML code:
Send-MailMessage -From $From -to $To -Subject $Subject -Body ($Body | convertTo-HTML) BodyAsHtml -SmtpServer $SMTPServer -port $SMTPPort -Priority High

Open in new window

is the absolute minimum you need to do. You might have issues with forced line breaks, but since you only want to send a test mail, this should work.

Author

Commented:
Using Outlook to send the messages forced the email message to go via HTML

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial