How to Send Mail throw PowerShell Script I my Trying But i got error

$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SMTPClient( $SmtpServer, 587 )  
$SMTPClient.EnableSSL = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential( "venkatesh.pnetwork08", "Password" );
$SMTPClient.Send('venkatesh.pnetwork08@gmail.com','manohar.hm@accenture.com','Hi testmail','Test mail for scrptiing')

Error Is:

ERROR: Exception calling "Send" with "4" argument(s): "Failure sending mail."
Script_eventlog_by mailid.ps1 (7): ERROR: At Line: 7 char: 17
ERROR: + $SMTPClient.Send <<<< ('venkatesh.pnetwork08@gmail.com','manohar.hm@accenture.com','Hi testmail','Test mail for scrptiing')
ERROR:     + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
ERROR:     + FullyQualifiedErrorId : DotNetMethodException
ERROR:

March 8th, 2013 2:06pm

 $creds = New-Object System.Net.NetworkCredential( "venkatesh.pnetwork08", "Password" ); 
 $props=@{
      To='venkatesh.pnetwork08@gmail.com',
      From='manohar.hm@accenture.com'
      Subject='Hi testmail'
      Body='Test mail for scrptiing'
}


Send-MailMessage @props -Port 587 -SmtpServer smtp.gmail.com -UseSsl -credentials $creds

Use the CmdLet it is safer.
Free Windows Admin Tool Kit Click here and download it now
March 8th, 2013 2:22pm

its Showing Error Like

ERROR: Send-MailMessage : A parameter cannot be found that matches parameter name 'Port'.
Script_eventlog_by mailid.ps1 (10): ERROR: At Line: 10 char: 30
ERROR: + Send-MailMessage @props -Port <<<<  587 -SmtpServer smtp.gmail.com -UseSsl -credentials $creds
ERROR:     + CategoryInfo          : InvalidArgument: (:) [Send-MailMessage], ParameterBindingException
ERROR:     + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.SendMailMessage
ERROR:

March 8th, 2013 2:26pm

What version of Windows are you using?
Free Windows Admin Tool Kit Click here and download it now
March 8th, 2013 2:28pm

Windows Power Shell 

SAPIEN PowerShell Studio 2012

March 8th, 2013 2:29pm

Windows Power Shell 

SAPIEN PowerShell Studio 2012

What version of Windows? 98,95,xp,2003,7,8...???

Free Windows Admin Tool Kit Click here and download it now
March 8th, 2013 2:33pm

Windows Server 2008 R2
March 8th, 2013 2:34pm

Windows Server 2008 R2

You need to upgrade PowerShell to V3.  It has more management functions for WS2008R2 and can more easily manage the server.  It also has updated CmdLets for most things like Send-MailMessage.

Free Windows Admin Tool Kit Click here and download it now
March 8th, 2013 2:44pm

http://www.microsoft.com/en-us/download/details.aspx?id=34595

Includes Windows PowerShell 3.0, WMI, WinRM, Management OData IIS Extension, and Server Manager CIM Provider

March 8th, 2013 2:47pm

Here is a better method for setting up the mail client.

http://www.powershellpro.com/powershell-email-alerts/210/

Free Windows Admin Tool Kit Click here and download it now
March 8th, 2013 2:54pm

Send-MailMessage : Cannot process argument transformation on parameter 'Credential'. userName
At line:10 char:82
+ ... sl -credential $creds
+                    ~~~~~~
    + CategoryInfo          : InvalidData: (:) [Send-MailMessage], ParameterBindingArgumentTransformationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Microsoft.PowerShell.Commands.SendMailMessage
March 8th, 2013 3:05pm

 $creds = Get-Credential venkatesh.pnetwork08
Free Windows Admin Tool Kit Click here and download it now
March 8th, 2013 3:08pm

Powershell's Send-MailMessage -Credential parameter accepts only System.Management.Automation.PSCredential, not System.Net.NetworkCredential. By default, Powershell's interactive Get-Credential command returns a PSCredential object.

To manually create a PSCredential object, follow these instructions (hat tip to bit.ly/RPR4NJ):
$secpasswd = ConvertTo-SecureString "PlainTextPassword" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ("username", $secpasswd)
Of course, this means your password is now stored and hard-coded in your script, which is not security best-practice.
May 19th, 2014 7:54pm

Sweet ! that did the trick
Free Windows Admin Tool Kit Click here and download it now
June 23rd, 2015 7:05am

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

Other recent topics Other recent topics