Email credentials when using send-mailmessage command - email

I have searched through many many forums and they do explain how to do this but the technical language is just too difficult to understand as I'm very new to powershell. I would like this explained to me step by step (baby steps). I would like to run this powershell command in a batch file (.bat). I have a batch file that does robocopy backups weekly and I want the batch file to send me a email when the backup is complete. The only issue I have is the credentials, I get a pop-up box asking for the user name and password. When I eneter this information the email will send successfully. Here is what I have;
Using: powershell V2.0 Windows 7 Ultimate
Powershell -command send-mailmessage -to emailadress#provider.com -from emailaddress#provider.com -smtp smtp.broadband.provider.com -usessl -subject 'backup complete'

$from = "example#mail.com"
$to = "example#mail.com"
$smtp = "smtpAddress.com"
$sub = "hi"
$body = "test mail"
$secpasswd = ConvertTo-SecureString "yourpassword" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential($from, $secpasswd)
Send-MailMessage -To $to -From $from -Subject $sub -Body $body -Credential $mycreds -SmtpServer $smtp -DeliveryNotificationOption Never -BodyAsHtml

You could pass the credential object in your same command - which would avoid the popup:
Powershell -Command 'Send-MailMessage -to "emailadress#provider.com" -from "emailaddress#provider.com" -smtp "smtp.broadband.provider.com" -usessl -subject "backup complete" -credential (new-object System.Net.NetworkCredential("user","pass","domain"))'
I'd recommend storing the username/password in a somewhat more safer format, but this should do your trick.

I'm not sure you can do SMTP authentication using the send-mailmessage command. But, you can send a message through an SMTP server that requires authentication using the Net.Mail.SmtpClient object and the System.Net.Mail.MailMessage object. See How to pass credentials to the Send-MailMessage command for sending emails for a good example.

look at the last exemple of send-mailmessage helppage
you will see you can pass credential whith the parameter -credential domain01\admin01
look here Using PowerShell credentials without being prompted for a password if you dont want any prompt (save your cred in a text file)

Related

Can you use MFA in powershell without using Exchange Online Powershell Module?

I've tried using "Send-MailMessage", but it looks like there isn't a parameter for authentifaction.
Can someone help here?
Send-MailMessage has a Credential parameter, see an example:
$smtpServer = 'smtp.office365.com'
$user = 'smtp#domain.com'
$pass = 'Password'
$creds = [pscredential]::new($user,($pass | ConvertTo-SecureString -AsPlainText -Force))
Send-MailMessage -SmtpServer $smtpServer -Credential $creds -To[...]
It's should work with MFA Enabled, if not, Generate an App Password and use it instead, See this link

Trying to send email (gmail) using PowerShell

I'm trying to send an email using PowerShell. For testing purposes, I'm trying to send the email to myself first. I've looked at a couple online references, and here is what I have so far:
$from = my#email.com
$to = my#email.com
$subject = "Test"
$body = "Test"
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
Send-MailMessage -from $from -to $to -Subject $subject -Body $Body -SmtpServer $SMTPServer -Port $SMTPPort -UseSsl -Credentials (Get-Credential)
Once ran, I get the prompt to enter credentials. After entering credentials, I get an error reading:
Send-MailMessage : A parameter cannot be found that matches parameter name 'Credentials'.
So I'm led to believe that the error is thrown when I enter in my credentials. When using this feature and the username is the email, am I supposed to type in my entire email, or just the identifier prior to the "#....com"? Either way, it doesn't seem to like either and I'm getting an error all the same. Am I using this correctly?

run the powershell script so it checks and mails the faults

I am running an office 365 database of a lot of members, each in one of a bunch of groups. I used to check the groups and users by hand but there are too many members now to be able to do that efficiently. now I have found a powershell script somewhere on github but it looks as if it just does checking and displaying in powershell.
Is there a way to have a script compare the users and groups and if a user is in a group he does not belong in send a mail to the admin mail in the domain?
if this makes any sense...
You can use this piece of code to send an email in powershell.
$smtpServer = "mailserver.com"
$From = "from#mailserver.com"
$To = "to#example.com"
$Subject = "example"
$body = "test"
send-mailmessage -to $To -from $From -subject $Subject -SmtpServer $smtpServer -Body $body -BodyAsHtml -Port 25

Send-MailMessage Access to path is denied

I am trying to send files by email using powershell. I am able to pop up a prompt for password for credential to run. But the send-mailmessage come back saying the path is denied. Also is there a way to include the password in the script to bypass entering the password so that I can run this through task scheduler on the server?
Send-MailMessage -From 'David Brierton <Davidb#test.com>' -To 'David Brierton <Davidb#test.com>' -Subject "Website deployment" -Body "See attached file" -Attachments #("\\server\d$\Mdrive\test\test\Book1.csv", "\\server\d$\Mdrive\test\test\ExampleBook1.csv") -SmtpServer test.test.test -Credential DomainName\User
Remove the # and the bracket in the -attachement and try with only your email
Send-MailMessage -From "Davidb#test.com" -To "Davidb#test.com" -Subject "Website deployment" -Body "See attached file" -Attachments "\\server\d$\Mdrive\test\test\Book1.csv", "\\server\d$\Mdrive\test\test\ExampleBook1.csv" -SmtpServer test.test.test -Credential DomainName\User

Not receiving email sent using powershell command

Send-MailMessage -SmtpServer "smtp.com.au" -From "me#me.com" -To "you#you.com" -Subject "Testing" -Attachments "\\Test\archive.zip" -Body "attached"
I am using the above command to email a zip file but i don't see anything in the To (you#you.com) mailbox.
The command runs successfully without any errors.
I think the problem is in your function call. You miss a lot of parameters. Take a look on my working gmail example:
$password = ConvertTo-SecureString "123456789" -AsPlainText -Force
$emailCredentials = New-Object pscredential ("from#gmail.com", $password)
try {
Send-MailMessage -From "from#gmail.com" -To "to#to.com" -Subject "subject" -Body "body" -SmtpServer "smtp.gmail.com" -Port 587 -Credential $emailCredentials -Encoding UTF8 -UseSsl -ErrorAction Stop
}
catch {
echo $error[0]
}
There is no -Credential parameter in your call, and I'm almost sure, that's the problem. Also your smtp server may have this protocol on different port, this is what -Port is for. And finally -UseSsl, if smtp server is encrypting mails, then without this switch, your emails will be not send. -Encoding UTF8 is just for regional chars.
Check your $error[0], it may tell you, where the problem is. Check what is happening on your -From email. On gmail you can see sometimes auto-repiles, with error description.
I'm sorry for not giving you simple solution, but I don't have enough informations, to find it.
Ok guys i have an update:
My command mentioned above is working absolutely fine. IT team has told me that we had issues with our SMTP the other day (When i had reported this issue) the emails eventually did come in (too late). I have tested the same this morning and it seems to be fine so a big thanks to all for your support.
I have got another question but i will post it separately.