I am trying to send an email using PowerShell, but need to use TLS. Any way I can do that using Send-MailMessage cmdlet?
This is my code:
$file = "c:\Mail-content.txt"
if (test-path $file)
{
$from = "afgarciact#gmail.com"
$to = "<slopez#comfama.com.co>","<carloscu#comfama.com.co>"
$pc = get-content env:computername
$subject = "Test message " + $pc
$smtpserver ="172.16.201.55"
$body = Get-Content $file | Out-String
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { return $true }
foreach ($recipient in $to)
{
write-host "Sent to $to"
Send-MailMessage -smtpServer $smtpserver -from $from -to $recipient -subject $subject -bodyAsHtml $body -Encoding ([System.Text.Encoding]::UTF8)
}
}
else
{
write-host "Configuración"
}
Thanks a lot!
Make sure your specify the -UseSsl switch:
Send-MailMessage -SmtpServer $smtpserver -UseSsl -From $from -To $recipient -Subject $subject -BodyAsHtml $body -Encoding ([System.Text.Encoding]::UTF8)
If the SMTP server uses a specific port for SMTP over TLS, use the -Port parameter:
Send-MailMessage -SmtpServer $smtpserver -Port 465 -UseSsl -From $from -To $recipient -Subject $subject -BodyAsHtml $body -Encoding ([System.Text.Encoding]::UTF8)
If you want to make sure that TLS is always negotiated (and not SSL 3.0), set the SecurityProtocol property on the ServicePointManager class:
[System.Net.ServicePointManager]::SecurityProtocol = 'Tls,TLS11,TLS12'
I have been fighting this all day. The fix for me ended up being needing to run this in a separate line first, and then I was able to run my Send-MailMessage commands:
[System.Net.ServicePointManager]::SecurityProtocol = 'Tls,TLS11,TLS12'
Thank you for the suggestion!
Related
Below is the code:
$TLS12Protocol = [System.Net.SecurityProtocolType] 'Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $TLS12Protocol
$smtpencryption = 'tls'
$From = "JIT.Batch#duckcreek.com"
$To = "abhishek.chawla#duckcreek.com”
$recipients = "<abhishek.chawla#duckcreek.com>"
[string[]]$To = $recipients.Split(',')
$Cc = "abhishek.chawla#duckcreek.com"
$Subject = "JITAppointment batch Started on StateAuto Production"
$Body = "JITAppointment batch Started on SA Production for latest file"
$SMTPServer = "smtp.office365.com"
$SMTPPort = "587"
$secpasswd = ConvertTo-SecureString "*******" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential($from, $secpasswd)
Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject -Body $Body -BodyAsHtml -SmtpServer $SMTPServer -Port $SMTPPort -UseSsl -Credential $mycreds
#Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject -Body $Body -SmtpServer $SMTPServer
and i am getting below error when i run this on server:
enter image description here
I wont be able to send the mail through this code.
I can see you're sending via smtp.office365.com. Microsoft 365 no longer supports basic authentication which means you cannot use Send-MailMessage. Alternatives include Send-MgUserMail which is part of Module Microsoft.Graph.Users.Actions
I'm scheduling script if windows scheduled task is disabled from the given list then send email to me with Taskname in email subject and Taskinfo in body.
But I am unable to format email body and add disabled task name in to subject.
Please help me to format email body and add disabled task name to subject.
Here is my script-
$tasknamelist= Import-Csv "C:\Documents\task.csv"
foreach ($task in $tasknamelist) {
$service=Get-ScheduledTask -TaskName "$taskname" | select -ExpandProperty State | Out-String
if ($task.State -eq "Disabled") {
$Body ="$service is not running"
}
else {
Write-Host "$Body Task is enabled" | Out-Null
}
} $Body
$From = "xxx#outlook.com"
$To = "xxx#outlook.com"
$Cc = "xxxx#outlook.com"
#$Attachment = "C:\temp\Some random file.txt"
$Subject = ""
$Body = "$Body"
$computer = $env:computername
$SMTPServer = "outlook.office365.com"
$SMTPPort = "587"
Send-MailMessage -From $From -to $To -Cc $Cc -Subject "Task Scheduler is disabled on $computer" `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
-Credential $cred
You have declared $body as an empty vaiable after ending foreach loop. The fixed code is:
$tasknamelist = Import-Csv "C:\Documents\task.csv"
foreach ($task in $tasknamelist) {
$service = Get-ScheduledTask -TaskName "$taskname" | select -ExpandProperty State | Out-String
if ($task.State -eq "Disabled") {
$Body = "$service is not running"
}
else {
Write-Host "$Body Task is enabled" | Out-Null
}
}
$From = "xxx#outlook.com"
$To = "xxx#outlook.com"
$Cc = "xxxx#outlook.com"
#$Attachment = "C:\temp\Some random file.txt"
$Subject = ""
$computer = $env:computername
$SMTPServer = "outlook.office365.com"
$SMTPPort = 587
Send-MailMessage -From $From -to $To -Cc $Cc -Subject "Task Scheduler is disabled on $computer" `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
-Credential $cred
And you didn't add white space in declaring variable.
$tasknamelist= Import-Csv "C:\Documents\task.csv"
foreach ($task in $tasknamelist)
{
$service=Get-ScheduledTask -TaskName $task
if ($service.State -eq "Disabled")
{
$Body ="$service is not running"
$From = "xxx#outlook.com"
$To = "xxx#outlook.com"
$Cc = "xxxx#outlook.com"
$computer = $env:computername
$Subject = $task + " disabled on " + $computer
$SMTPServer = "outlook.office365.com"
$SMTPPort = "587"
Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl -Credential $cred
}
else
{
Write-Host "$Body Task is enabled" | Out-Null
}
}
Send-Mailmessage prompts for my credentials when executed.
I've tried creating a $from variable to provide Powershell with my credentials (technically this is working since I'm only being asked for my password but want it to send the email without authenticating)
$from = (Get-ADUser $env:UserName -Properties emailaddress).emailaddress
$To = "example#example.org"
$Subject = "test2"
$Body = "Test2"
$SMTPServer = "10.98.0.20"
$SMTPPort = "25"
Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -Credential $from
Don’t use -Credential. Just pass -From an address as a string. SMTP doesn’t care. This is happening because you are including -Credential, or your mail server doesn’t accept anonymous relay access.
I want to send a email with powershell. The script works fine if I type my credential in manualy. But I want to give the credential parameters within the script. My script looks like this:
$From = "test#yahoo.de"
$To = "test2#yahoo.de"
$Cc = "test#yahoo.de"
$Attachment = "C:\Users\test\test\test.ini"
$Subject = "Email Subject"
$Body = "Insert body text here"
$SMTPServer = "smtp.mail.yahoo.com"
$SMTPPort = "587"
Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
-Credential (
$MyClearTextUsername=’test#yahoo.de’
$MyClearTextPassword=’test123’
$SecurePassword=Convertto-SecureString –String $MyClearTextPassword –AsPlainText –force
$MyCredentials=New-object System.Management.Automation.PSCredential $MyClearTextPassword,$SecurePassword) -Attachments $Attachment
Here's how you can create a credentials object:
$cred = ([pscredential]::new('test#yahoo.de',(ConvertTo-SecureString -String 'test123' -AsPlainText -Force)))
so in your case use:
Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
-Credential $cred -Attachments $Attachment
I see no point in trying to fit that into the Send-MailMessage, just create it before and reference it. easier to read.
If you're using Office365 to send email, you might want to try this:
# Sending an email from PowerShell 5.1 script through outlook.office365.com
#
# 1. Create an encrypted password file
# PS > Read-Host -AsSecureString | ConvertFrom-SecureString | Out-File -FilePath <passwordfile>
# This will prompt you for a password, encrypt and save in <passwordfile>
# 2. Obtain Outlook Office365 SMTP server name.
# Go to your ISP and find the value of the MX record. For example <yourdomain>.mail.protection.outlook.com
# 3. If after running the script you get this error:
# Send-MailMessage : Mailbox unavailable. The server response was: 5.7.606 Access denied, banned sending IP [X.X.X.X].
# You will need to delist your IP by going here: https://sender.office.com/
# Note: Removing you IP from the block list could take up to 30 minutes.
#
$User = "<SMPT loging username>"
$PasswordFile = "<passwordfile>"
$SMTPCredentials=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $PasswordFile | ConvertTo-SecureString)
$EmailTo = "<to email address>"
$EmailFrom = "<from email address>"
$Subject = "<email subject>"
$Body = "<email body>"
$SMTPServer = "<Outlook STMP Server from MX record>"
Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $Subject -Body $Body -SmtpServer $SMTPServer -Port 25 -Credential $SMTPCredentials -UseSsl
I have this code:
Clear-Host
$file = "c:\Mail-content.txt"
if (test-path $file)
{
$from = "afgarciact#gmail.com"
$to = "<slopez#comfama.com.co>","<carloscu#comfama.com.co>"
$pc = get-content env:computername
$subject = "Test message " + $pc
$smtpserver ="172.16.201.55"
$body = Get-Content $file | Out-String
[System.Net.ServicePointManager]::SecurityProtocol = 'Tls,TLS11,TLS12'
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { return $true }
foreach ($recipient in $to)
{
Write-Host "Sent to $to"
Send-MailMessage -SmtpServer $smtpserver -UseSsl -From $from -To $recipient -Subject $subject -BodyAsHtml $body -Encoding ([System.Text.Encoding]::UTF8)
}
}
else
{
Write-Host "Configuración"
}
I want to know if I can use a CRT certificated instead of setting ServerCertificateValidationCallback = TRUE.
This is a test environment.
The CRT certificate was already added to the SMTP server.