Localhost SMTP server on Windows Server 2012 R2 Standard not working - powershell

Newly installed Windows Server 2012 R2 Standard. I set up SMTP server by this tutorial http://www.vsysad.com/2012/04/setup-and-configure-smtp-server-on-windows-server-2008-r2/ Tested using this powershell command:
Send-MailMessage -SMTPServer localhost -To receiver_mail -From sender_mail -Subject "This is a test email" -Body "Hi Japinator, this is a test email sent via PowerShell"
It worked for some time, but accidentally stopped working at the moment. Tried to review tutorial step-by-step - everything is ok. Receiving this error message when testing in powershell:
Send-MailMessage : Unable to connect to the remote server
At line:1 char:1
+ Send-MailMessage -SMTPServer localhost -To receiver_email -From sender_email ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [
ion
+ FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage

As the answer above alludes to, the issue is due to the SMTP service not being started.
I stopped the SMTP Service on my 2012 test server and then ran the Send-MailMessage command and confirmed that the error was the same:
PS C:\Users\Admin> Send-MailMessage -SMTPServer localhost -To xxxxxxxxx#gmail.com -From blog#vsysad.com
-Subject "This is a test email" -Body "Hi, this is a test email sent via PowerShell"
Send-MailMessage : Unable to connect to the remote server
At line:1 char:1
+ Send-MailMessage -SMTPServer localhost -To xxxxxxxxx#gmail.com -From blog#vsysad ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpExcept
ion
+ FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
The first thing to do would be to check whether the service is running:
PS C:\Users\Admin> get-service smtpsvc
Status Name DisplayName
------ ---- -----------
Stopped smtpsvc Simple Mail Transfer Protocol (SMTP)
Per the above, if the SMTP Service is not running then run the command below to start it:
PS C:\Users\Admin> start-service smtpsvc
Then set the startup to Automatic. Run the command below to do this:
PS C:\Users\Admin> set-service smtpsvc -StartupType Automatic
This will ensure that the SMTP service starts up automatically when the server boots up.
Your SMTP server should now be able to process your Send-mailMessage request and assuming the rest of the config is sound, relay the message successfully.

SMTP Virtual Server On IIS6 was not started. By default it doest not start automatically. Autostart can be done according this thread https://serverfault.com/questions/263546/automatically-start-smtp-server-in-iis

Related

PowerShell Send-MailMessage and Microsoft Exchange 2007

I'm having a problem sending emails using Microsoft Exchange 2007.
I've used the exact same powershell code on a different machine and it sends the email.
The error message it gives is:
Send-MailMessage : Mailbox unavailable. The server response was: 5.7.1 Unable to relay
At C:\Users\alex\Desktop\Email.ps1:37 char:29
+ Send-MailMessage <<<< #Parameters
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpFailedRecipientException
+ FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
How would I fix this?
Your receive connector for relaying probably doesn't have the IP address of the computer you are running the script from. You need to check this on the Exchange server.

Learning an Email script using Gmail SMTP authentication

I'm new to power shell scripting.I wrote a script which should send an email to my inbox
I found few solution and improved my script
$cred = Get-Credential
Send-MailMessage -SmtpServer "smtp-relay.gmail.com" -Credential $cred -UseSsl -Port 587 -From "test#gmail.com" -To "test#gmail.com" -Subject "test" -Body "test"
I got an error message as follows below
Send-MailMessage : Mailbox unavailable. The server response was: 5.7.0 Mail relay denied
[117.193.79.33]. bd10sm271633igb.4 - gsmtp
At line:2 char:1
+ Send-MailMessage -SmtpServer "smtp-relay.gmail.com" -Credential $cred -UseSsl -P ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-M
ailMessage], SmtpException
+ FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
Again I changed the port number 465 or 25,it get operation timeout.Help me out!!
PS:Telnet on 25 or 587 is working and no AV program in computer
Thanks in advance.
$cred = Get-Credential Send-MailMessage -SmtpServer "smtp.gmail.com" -Credential $cred -UseSsl -Port 587 -From "test#gmail.com" -To "test#gmail.com" -Subject "test" -Body "test"
This Testing with Gmail works,after i followed this steps
1.Disable two way verification
2.Allow less secure apps in settings
Lesssecure Apps

What can cause this error: ERROR_DESTINATION_NOT_REACHABLE, Microsoft.Web.Deployment.PowerShell.SyncPackage

Using a Powershell scripted web deploy job, I am receiving this error on some hosts:
C:\> Get-Service msdepsvc,wmsvc
Status Name DisplayName
------ ---- -----------
Running msdepsvc Web Deployment Agent Service
Running wmsvc Web Management Service
C:\> (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\WebManagement\Server' -Name EnableRemoteManagement).EnableRemoteManagement
1
C:\> Add-PSSnapin WDeploySnapin3.0
C:\> $credential = New-Object System.Management.Automation.PSCredential ("some-domain\some-user", (ConvertTo-SecureString "password" -AsPlainText -Force))
C:\> New-WDPublishSettings -ComputerName localhost -Credentials $credential -AllowUntrusted -Site "Default Web Site" -SiteUrl "http://localhost" -AgentType WMSvc -FileName publish.xml
AllowUntrusted : True
PublishUrl : localhost
SiteName : Default Web Site
SiteUrl : http://localhost
Username : some-domain\some-user
Password : password
MySqlDBConnectionString :
SqlDBConnectionString :
AgentType : WMSvc
UseNTLM : False
OtherAttributes : {}
C:\> Restore-WDPackage "package.zip" -DestinationPublishSettings publish.xml
Restore-WDPackage : Could not connect to the remote computer ("localhost"). On the remote computer, make sure that Web
Deploy is installed and that the required process ("Web Management Service") is started. Learn more at:
http://go.microsoft.com/fwlink/?LinkId=221672#ERROR_DESTINATION_NOT_REACHABLE.
At line:1 char:1
+ Restore-WDPackage "package.zip" -DestinationPublishSett ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Restore-WDPackage], DeploymentDetailedException
+ FullyQualifiedErrorId : ERROR_DESTINATION_NOT_REACHABLE,Microsoft.Web.Deployment.PowerShell.SyncPackage
The Windows firewall is not running and the same script runs successfully on other machines (the package is successfully deployed, the website works as expected). As shown above, msdepsvc and wmsvc are both running and wmsvc is configured to allow remote connections. The same machine that fails to accept the Restore-WDPackage command, does accept remote management connections from the IIS management console (after running Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\WebManagement\Server' -Name EnableRemoteManagement -Value 1 and restarting the service). The MS help link contains no clues and the documentation for WDeploySnapin3.0 is not really complete. Other WinRM commands work fine (we create the target website with this script).
What else could be causing the error?

Trouble sending email using Powershell

I've run the following script:
PS C:\> Send-MailMessage -To <EmailAddress1> -From <EmailAddress2> -Body "This is a test" -Subject "TEST MAIL" -SmtpServer <INTERNAL IP OF SMTP SERVER>
And I receive the following error:
Send-MailMessage : Unable to read data from the transport connection: net_io_connectionclosed.
At line:1 char:17
+ Send-MailMessage <<<< -To <EmailAddress1> -From <EmailAddress2> -Body "This is a test" -Subject "TEST MAIL" -SmtpServer <INTERNAL IP OF SMTP SERVER>
+ CategoryInfo: InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-Mail Message], SmtpException
+ FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage`
I was made aware of the fact that I need permissions to send email from my local machine through the SMTP server, and as far as I know, I've been granted those rights.
Would somebody please help point me in the right direction on this one?
The ultimate goal is to be able to send emails as part of some Powershell scripts.
Thanks!
I prefer the Net.Mail.SmtpClient method of sending email. This script would send the contents of a file passed as a parameter.
$emailFrom = "AUTOMATED_PRERUN#somehost.com"
$emailTo = "somebody#somehost.com"
$subject = "TEST"
get-content $Args[0] | %{$Body+= " {0} `n" -f $_}
$smtpServer = "mailserver.somehost.com"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)
Though, your error sounds more like a networking issue of some type.
You might want to check that you can reach the SMTP port on the server:
https://learn.microsoft.com/en-us/Exchange/mail-flow/test-smtp-with-telnet
Probably your antivirus file service is locking mail delivery.

Sending E-mail from Powershell Failing on One Machine

I have a bit of powershell to send an e-mail:
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)
This works perfectly on every machine I've ever tried it on until today. I have one machine where I get this error message:
Exception calling "Send" with "4" argument(s): "Failure sending mail."
At R:\sqlrestore\scripts\Utilities\Check-Lag.ps1:15 char:12
+ $smtp.Send <<<< ($emailFrom, $emailTo, $subject, $body)
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : DotNetMethodException
The error message is ambiguous. It turns out the issue is that the SMTP server was inaccessible because outbound SMTP traffic was not allowed.
By the way, if you're using PowerShell 2.0 then you may want to try the Send-MailMessage cmdlet:
Send-MailMessage -SmtpServer YourServerName -From $emailFrom -To $emailTo -Subject $subject -Body $body
I have just come across the same problem, turns out it was because of two factor authentication. I created a 'app specific password' in Google security settings and all works fine now.