4

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' 
Ken White
  • 123,280
  • 14
  • 225
  • 444
user3089120
  • 57
  • 2
  • 2
  • 6

4 Answers4

6
$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
2

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.

Harald F.
  • 4,505
  • 24
  • 29
0

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.

Community
  • 1
  • 1
mti2935
  • 11,465
  • 3
  • 29
  • 33
0

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)

Community
  • 1
  • 1
Loïc MICHEL
  • 24,935
  • 9
  • 74
  • 103