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

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 10:56pm

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 15th, 2015 2:46am

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'"

Fix your SMTP server connection.  Yohave a network problem.  It is not a scripting issue.

July 15th, 2015 5:08am

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

Other recent topics Other recent topics