Send-MailMessage asking for credentials - powershell

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.

Related

Not able to send the mail through powershell script

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

Convert type string to Secure String

I have prepared the PowerShell script for sending emails using SendGrid. But while converting plain text password of SendGrid account into secure string, then I am getting the following error:
“unable to convert type string to secure string”
Sample Script:
$SecurePassword = ConvertTo-SecureString "Demo123" -AsPlainText -Force
So, can anyone suggest me how to resolve this issue.
$Username ="Name#azure.com"
$Password = ConvertTo-SecureString "Demo123" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential $Username, $Password
$SMTPServer = "smtp.sendgrid.net"
$EmailFrom = "from#mail.com"
$EmailTo = "to#mail.com"
$Subject = "SendGrid test"
$Body = "SendGrid testing successful"
Send-MailMessage -smtpServer $SMTPServer -Credential $credential -Usessl -Port 587 -from $EmailFrom -to $EmailTo -subject $Subject -Body $Body
I am able to run same command in Powershell:
My Powershell Version is 5.7.0.18831.

Powershell to send mail

Error
New-Object : Cannot find an overload for "PSCredential" and the
argument count: "2". At D:\Scripts\gsend.ps1:12 char:15
Code
#Create Password File (Only need once)
#$Credential = Get-Credential
#$Credential.Password | ConvertFrom-SecureString | Set-Content "D:\scripts\gsendcred.txt"
#Send Email
$EncryptedCredential = "D:\scripts\gsendcred.txt"
$EmailUsername = "me#gmail.com"
$EncryptedPW = Get-Content "D:\scripts\gsendcred.txt"
$EncryptedCredential = ConvertTo-SecureString -String $EncryptedCredential -
AsPlainText -Force
$Credential = New-Object Management.Automation.PSCredential ($EmailUsername,
$EncryptedPW)
$EmailFrom = "me#gmail.com"
$EmailTo = "me#gmail.com"
$EmailSubject = "GSEND Test Subject"
$EmailBody = "Test Body"
$SMTPServer = "smtp.gmail.com"
$SMTPPort = 587
$SMTPSsl = $true
I believe your issue is handling credentials. Here is how I handle the smtp credentials:
$smtpPwd = "password"
$smtpCredentialsUsername = "jdonnelly#xxxxx.com"
$smtpCredentialsPassword = ConvertTo-SecureString -String $smtpPwd -AsPlainText -Force
$Credentials = New-Object –TypeName System.Management.Automation.PSCredential
–ArgumentList $smtpCredentialsUsername, $smtpCredentialsPassword
Then:
$smtp.Credentials = $Credentials
Or if you are using Send-MailMessage store your email credentials locally by running this short script by itself beforehand:
Get-Credential | Export-Clixml C:\fso\myemailcreds.xml
It will prompt you to enter your credentials and then store them locally in (in this case) a folder called fso. Then in any Send-MailMessage cmdlet use the locally stored credentials as follows:
Send-MailMessage -To 'Recipient <recipient#yahoo.com>' -from 'John Donnelly <jdonnelly#xxx.com>'
-cc 'whoever <whoever#xxx.com>' -Subject $subject -Body $body
-BodyAsHtml -smtpserver "smtp.office365.com" -usessl
-Credential (Import-Clixml C:\fso\myemailcreds.xml) -Port 587
You Can use below Powershell script for send mail
Send-MailMessage -To "abc#abc.com" -From "def#abc.com" -Cc "gef#abc.com","hij#abc.com" -Subject Test Mail -BodyAsHtml $htmlbody -SmtpServer "mailhost.abc.com"
where $htmlbody is string variable that contain html.

Sending a email with powershell

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

Can I use TLS with Send-MailMessage cmdlet?

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!