Send an email from my Smtp Server

Here is my powershell to send an email from my smtp server. 

My real smtp server exists and functions correctly.

This PS script takes a long time to run and below is the error code:

$subject = "This Subject"
$body =  "The Body and the subBody"
$emailTo = "toemail@email.com"
$emailFrom ="fromemail@email.com"
$smtpServer = smtp.email.com
$port="25"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$credentials=new-object system.net.networkcredential(email@email.com,password)
$smtp.credentials=$credentials.getcredential($smtpserver,$port)
$PlainPassword = "mainpassword"
$SecurePassword = $PlainPassword | ConvertTo-SecureString -AsPlainText -Force 
$UserName = "MYSITE\Administrator"
$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $UserName, $SecurePassword 
Send-MailMessage -To $emailTo -Subject $subject -Body $body -SmtpServer $smtpServer -From $emailFrom -Credential $Credentials -Port $port -UseSsl

PS Error Code:

InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException


July 13th, 2015 11:41pm

I an still getting an error on the SmtpServer even with the smtp credentials

$PlainPassword = 'pass1'
$SecurePassword = $PlainPassword | ConvertTo-SecureString -AsPlainText -Force 
$UserName = email1@email.com'
$smtpcredentials = New-Object System.Management.Automation.PSCredential -ArgumentList $UserName, $SecurePassword 
$subject = 'This Subject'
$body =  'The Body and the rest of the Body'
$emailTo = 'emailto@email.com
$emailFrom ='emailfrom@email.com'
$smtpServer = 'smtp.email.com'
$port='25'
Send-MailMessage -To $emailTo -Subject $subject -Body $body -SmtpServer $smtpServer -From $emailFrom -Credential $smtpcredentials -Port $port -UseSsl

I get Send-MailMessage  from here:

https://technet.microsoft.com/en-us/library/hh849925.aspx?f=255&MSPPError=-2147217396


Free Windows Admin Tool Kit Click here and download it now
July 14th, 2015 3:00am

There needs to be a username and password on the SmtpServer for it to function but

https://technet.microsoft.com/en-us/library/hh849925.aspx?f=255&MSPPError=-2147217396

does not mension them. 

How can they be entered elsewhere?

$subject = "This Subject"
$body =  "The Body..."
$emailTo = "emailto@email.com"
$emailFrom ="emailfrom@email.com"
$smtpServer = smtp.live.com
$port="25"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$credentials=new-object system.net.networkcredential(philosophaie@live.com,electrical)
$smtp.credentials=$credentials.getcredential($smtpserver,$port)
$PlainPassword = "PSpassword"
$SecurePassword = $PlainPassword | ConvertTo-SecureString -AsPlainText -Force 
$UserName = "MYSITE\Administrator"
$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $UserName, $SecurePassword 
Send-MailMessage -To $emailTo -Subject $subject -Body $body -SmtpServer $smtpServer -From $emailFrom -Credential $Credentials -Port $port -UseSsl

This gives the same error as the top of post:

InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException

July 14th, 2015 5:49pm

I posted the exact code you need.  Here it is again:

$mailprops=@{
    Subject = 'This Subject'
    Body = 'The Body and the subBody'
    To = 'toemail@email.com'
    From = 'fromemail@email.com'
    SmtpServer = 'smtp.email.com'
}
Send-MailMessage @mailprops -Credential mailuserid

This will prompt you to enter the password.  Change "mailuserId" to you username.

Free Windows Admin Tool Kit Click here and download it now
July 14th, 2015 5:57pm

I changed to my email data then:

$mailprops=@{ Subject = 'This Subject' Body = 'The Body and the subBody' To = 'toemail@email.com' From = 'fromemail@email.com' SmtpServer = 'smtp.email.com' } Send-MailMessage @mailprops -Credential mailuserid

I enter my credentials but still get the same error as above:

InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException
I am certain my SmtpServer is correct!


July 14th, 2015 6:58pm

I changed to my email data then:

$mailprops=@{ Subject = 'This Subject' Body = 'The Body and the subBody' To = 'toemail@email.com' From = 'fromemail@email.com' SmtpServer = 'smtp.email.com' } Send-MailMessage @mailprops -Credential mailuserid

I enter my credentials but still get the same error as above:

InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException
I am certain my SmtpServer is correct!


Free Windows Admin Tool Kit Click here and download it now
July 14th, 2015 7:01pm

Can you please post the complete error message?

July 14th, 2015 7:33pm

Send-MailMessage : Unable to connect to the remote server
At line:8 char:1
+ Send-MailMessage @mailprops -Credential mailuserid
+---------------------------------------------------
    + CategoryInfo          : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpExcept
   ion
    + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
Straight from PowerShell in 2012 R2
Free Windows Admin Tool Kit Click here and download it now
July 14th, 2015 7:56pm

Now take the time to actually read the whole error message.
July 14th, 2015 8:50pm

The SmtpClient format is wrong in the "Send-MailMessage". 

How do I fix that?

Free Windows Admin Tool Kit Click here and download it now
July 14th, 2015 10:48pm

The SmtpClient format is wrong in the "Send-MailMessage". 

How do I fix that?

July 14th, 2015 10:50pm

The SmtpClient is wrong?

This:

Send-MailMessage : Unable to connect to the remote server

Free Windows Admin Tool Kit Click here and download it now
July 14th, 2015 10:52pm

"smtp.email.com" works in many different venues.
July 14th, 2015 11:14pm

This ps script is what I am basing this on:

$MainPassword = "MainPass"
$SecurePassword = $MainPassword | ConvertTo-SecureString -AsPlainText -Force
$Username = "MYSITE\Administrator"
$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $SecurePassword
$mailprops=@{
    Subject = 'This Subject'
    Body = 'The Body and the subBody'
    To = 'to@mail.com'
    From = 'from@email.com'
    SmtpServer = 'smtp.email.com'
}
Send-MailMessage @mailprops -Credential $Credentials

"smtp.email.com" works in many different instances other than PS. 

Is there something wrong with "SmtpServer = 'smtp.email.com'"

Free Windows Admin Tool Kit Click here and download it now
July 15th, 2015 12:28am

Is there something wrong with "SmtpServer = 'smtp.email.com'"
http://mxtoolbox.com/SuperTool.aspx?action=smtp%3asmtp.email.com&run=toolpage
July 15th, 2015 12:36am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics