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 7:45pm

If your smtp server allows email to be relayed then this is easier:

$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

You must avoid smart quotes which you mayl get when copying form a web page.

Free Windows Admin Tool Kit Click here and download it now
July 13th, 2015 8:20pm

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


July 13th, 2015 11:04pm

The instructions are specific. The Credentials take a PsCredential object and not what you are passing. Do it my way until you understand how to make it work then you can try and change it how you like..
Free Windows Admin Tool Kit Click here and download it now
July 14th, 2015 1:14am

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

Other recent topics Other recent topics