-2

I am trying to send an email via SMTP using powershell.

I have stored these variables in config.ps1 file

$smtpServer = "10.0.52.10"
$emailFrom = "prudhvi.g@xiomi.com"
$emailTo = "prudhvi@gmail.com"
$emailsubject = "Health Check"
$emailbody = "Hi"

This is my script to send email :

##################################################
#Send email with results
##################################################
. .\config.ps1

foreach ($line in $bodystring) 
    {
    $emailbody = $emailbody + $line + "`r`n"
    }


Send-MailMessage -To "$emailTo" -From "$emailFrom" -smtpServer "$smtpserver" -subject "$emailsubject" -body "$emailbody"

I get this error :

Send-MailMessage : Unable to connect to the remote server
At C:\Users\administrator.SDC\Desktop\email.ps1:12 char:1
+ Send-MailMessage -To "$emailTo" -From "$emailFrom" -smtpServer "$smtpserver" -su ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpExcept
   ion
    + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage

What is my mistake?? Should I include something else to make it work?

prudhvi
  • 1,141
  • 6
  • 23
  • 46
  • 2
    Hi, don't you need a login and password? Is the SMTP server reachable from this computer? – sodawillow Jan 02 '17 at 17:17
  • yaaa....u r right? But, how and where do i provide the login and password?/ – prudhvi Jan 02 '17 at 17:20
  • 1
    See [this question](http://stackoverflow.com/questions/12460950/how-to-pass-credentials-to-the-send-mailmessage-command-for-sending-emails) for example. – sodawillow Jan 02 '17 at 17:23

1 Answers1

3

With Gmail I've had more luck doing it like the following. I am currently using this as we speak and it is working just fine.

$EmailTo = "UserNameHere@gmail.com"
$EmailFrom = "UserNameHere@gmail.com"
$Subject = "New Posting"
$Body = $text 
$SMTPServer = "smtp.gmail.com"
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) 
$SMTPClient.EnableSsl = $true 
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("UserNameHere@gmail.com", "PasswordHere");
$SMTPClient.Send($SMTPMessage)

I believe UserName,Password, SSL, and Port all have to be there in order to work with Gmail. I could be wrong. You also need to make sure your Gmail is set up to allow this.

EDIT: If you have no already done so, allow your Gmail to use Less secure apps in the security settings, otherwise it will likely be blocked. You can see more and configure it here. https://myaccount.google.com/intro/security

JonnyBoy
  • 417
  • 1
  • 6
  • 16
  • Using your code, I could send an email from my gmail account. I have put your entire code in my script and ran it. But, could u please tell me what to write in my config.ps1 file and what to include in my script. Credentials have to be config.ps1 file and rest should be in my script. If I use the username and password in config.ps1, how could I link them to my script? – prudhvi Jan 02 '17 at 18:00