trying to write a scrip to send mails as a test to myself

Hi,

 

I am trying to use the send-mailmessage in powershell by passing through data using variables. I have cut down the script to simplest form so i can get it to work however i get errors " The specified string is not in the form required for an e-mail address"

 

Any thoughts please ?

 

$email = ‘xxx.xxxx@lxxxxxx.com’
$smtpServer =‘mail.us.xxxx.com
$subject= ‘subject’
$data = ‘This is a test’
Send-MailMessage To $email -subject $subject -Body $data -From $email -SmtpServer $smtpServer

 

Thanks in advance

If you’ve copied and pasted your script then it may just be as simple as you missing the “-” before “To” in your parameters.

Send-MailMessage -To $email -subject $subject -Body $data -From $email -SmtpServer $smtpServer

 

Hello fishandchips82,

This is what I’m using in my scripts to send email true powershell.

$smtp = "smtp.office365.com" $mailbeheerder = "systemadmin@domain.com" $username = "USER@domain.com" $Wachtwoord = "PASSWORD" $Poort = "587" $password = convertTo-secureString –string $Wachtwoord –asPlainText –force $creds = new-object System.Management.Automation.PSCredential($username,$password) $body = "Your Data"

Send-MailMessage -Port $Poort -UseSsl -to $mailbeheerder -Subject $subject -body $body -BodyAsHtml -SmtpServer $smtp -from $username -Credential $creds -Encoding UTF8

Thankyou both of you for responding so promptly.

@Montel your snippet looks interesting will take alook and rejig my script. Still wrestling with scripts as I am a novice :slight_smile:

 

To make your code easier to read, easier to understand and easier to maintain you should consider using splatting:

$SplatProps = @{
From = ‘sender@domain.com’
to = ‘recipient@otherdomain.com’
SmtpServer = ‘mail.us.xxxx.com
subject = ‘subject’
Body = ‘This is a test’
}

Send-MailMessage @SplatProps

Read more about: Get-Help about_Splatting

[quote quote=204504]To make your code easier to read, easier to understand and easier to maintain you should consider using splatting:

PowerShell
9 lines
<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;" spellcheck="false" wrap="off"></textarea>
1
2
3
4
5
6
7
8
9
$SplatProps = @{
From = 'sender@domain.com'
to = 'recipient@otherdomain.com'
SmtpServer = 'mail.us.xxxx.com'
subject = 'subject'
Body = 'This is a test'
}
Send-MailMessage @SplatProps
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Read more about: Get-Help about_Splatting

[/quote]
Thanks Olaf I like the concept however I am still learning powershell at 60 years old so its hard to ingrain all these concepts. However will take alook.

Ladies and Gents

I have another question but its related to my original so it makes sense for me to ask it here rather then starting a new thread.

 

When using the $body variable how does one start a new line. For example below i want to start a new line and display the users credentials. However it continues to stay on same line even if you use the $Body+= variable. What I want to achieve is to have new lines not have everything on one line

 

$Body = “Find below your logon details”

$Body += “Logon:$logon”

 

Does anyone know how to ensure that a new line is used in the mail ?

 

Thanks again everyone for your help.

 

 

 

 

 

You may read the chapter about “Here-Strings” in the help topic about Quoting Rules.

$Body = @"
Find below your logon details

Logon:$logon
"@

[quote quote=204585]You may read the chapter about “Here-Strings” in the help topic about Quoting Rules.

PowerShell
5 lines
<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;" spellcheck="false" wrap="off"></textarea>
1
2
3
4
5
$Body = @"
Find below your logon details
Logon:$logon
"@
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[/quote] Olaf thanks again you have been a great help :)