Send mail via gmail with PowerShell V2's Send-MailMessage


SUBMITTED BY: Guest

DATE: Nov. 14, 2013, 7:04 a.m.

FORMAT: Text only

SIZE: 4.9 kB

HITS: 1017

  1. $ss = new-object Security.SecureString
  2. foreach ($ch in "password".ToCharArray())
  3. {
  4. $ss.AppendChar($ch)
  5. }
  6. $cred = new-object Management.Automation.PSCredential "uid@domain.com", $ss
  7. Send-MailMessage -SmtpServer smtp.gmail.com -UseSsl -Credential $cred -Body...
  8. Send-MailMessage : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn
  9. more at
  10. At foo.ps1:18 char:21
  11. + Send-MailMessage <<<< `
  12. + CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException
  13. + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
  14. $EmailFrom = "notifications@somedomain.com"
  15. $EmailTo = "me@earth.com"
  16. $Subject = "Notification from XYZ"
  17. $Body = "this is a notification from XYZ Notifications.."
  18. $SMTPServer = "smtp.gmail.com"
  19. $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
  20. $SMTPClient.EnableSsl = $true
  21. $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("username", "password");
  22. $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
  23. $smtpClient = new-object system.net.mail.smtpClient
  24. $smtpClient.Host = 'smtp.gmail.com'
  25. $smtpClient.Port = 587
  26. $smtpClient.EnableSsl = $true
  27. $smtpClient.Credentials = [Net.NetworkCredential](Get-Credential GmailUserID)
  28. $smtpClient.Send('GmailUserID@gmail.com','yourself@somewhere.com','test subject', 'test message')
  29. # append to Christian's code above --^
  30. $emailMessage = New-Object System.Net.Mail.MailMessage
  31. $emailMessage.From = $EmailFrom
  32. $emailMessage.To.Add($EmailTo)
  33. $emailMessage.Subject = $Subject
  34. $emailMessage.Body = $Body
  35. $emailMessage.Attachments.Add("C:Test.txt")
  36. $SMTPClient.Send($emailMessage)
  37. ## Send-Gmail.ps1 - Send a gmail message
  38. ## By Rodney Fisk - xizdaqrian@gmail.com
  39. ## 2 / 13 / 2011
  40. # Get command line arguments to fill in the fields
  41. # Must be the first statement in the script
  42. param(
  43. [Parameter(Mandatory = $true,
  44. Position = 0,
  45. ValueFromPipelineByPropertyName = $true)]
  46. [Alias('From')] # This is the name of the parameter e.g. -From user@mail.com
  47. [String]$EmailFrom, # This is the value [Don't forget the comma at the end!]
  48. [Parameter(Mandatory = $true,
  49. Position = 1,
  50. ValueFromPipelineByPropertyName = $true)]
  51. [Alias('To')]
  52. [String[]]$Arry_EmailTo,
  53. [Parameter(Mandatory = $true,
  54. Position = 2,
  55. ValueFromPipelineByPropertyName = $true)]
  56. [Alias( 'Subj' )]
  57. [String]$EmailSubj,
  58. [Parameter(Mandatory = $true,
  59. Position = 3,
  60. ValueFromPipelineByPropertyName = $true)]
  61. [Alias( 'Body' )]
  62. [String]$EmailBody,
  63. [Parameter(Mandatory = $false,
  64. Position = 4,
  65. ValueFromPipelineByPropertyName = $true)]
  66. [Alias( 'Attachment' )]
  67. [String[]]$Arry_EmailAttachments
  68. )
  69. # From Christian @ StackOverflow.com
  70. $SMTPServer = "smtp.gmail.com"
  71. $SMTPClient = New-Object Net.Mail.SMTPClient( $SmtpServer, 587 )
  72. $SMTPClient.EnableSSL = $true
  73. $SMTPClient.Credentials = New-Object System.Net.NetworkCredential( "GMAIL_USERNAME", "GMAIL_PASSWORD" );
  74. # From Core @ StackOverflow.com
  75. $emailMessage = New-Object System.Net.Mail.MailMessage
  76. $emailMessage.From = $EmailFrom
  77. foreach ( $recipient in $Arry_EmailTo )
  78. {
  79. $emailMessage.To.Add( $recipient )
  80. }
  81. $emailMessage.Subject = $EmailSubj
  82. $emailMessage.Body = $EmailBody
  83. # Do we have any attachments?
  84. # If yes, then add them, if not, do nothing
  85. if ( $Arry_EmailAttachments.Count -ne $NULL )
  86. {
  87. $emailMessage.Attachments.Add()
  88. }
  89. $SMTPClient.Send( $emailMessage )
  90. $smtpmail = [System.Net.Mail.SMTPClient]("127.0.0.1")
  91. $smtpmail.Send("myacct@gmail.com", "myacct@gmail.com", "Test Message", "Message via local smtp")
  92. $filename = “c:scripts_scotttest9999.xls”
  93. $smtpserver = “smtp.gmail.com”
  94. $msg = new-object Net.Mail.MailMessage
  95. $att = new-object Net.Mail.Attachment($filename)
  96. $smtp = new-object Net.Mail.SmtpClient($smtpServer )
  97. $smtp.EnableSsl = $True
  98. $smtp.Credentials = New-Object System.Net.NetworkCredential(“username”, “password_here”); # Put username without the @GMAIL.com or – @gmail.com
  99. $msg.From = “username@gmail.com”
  100. $msg.To.Add(”boss@job.com”)
  101. $msg.Subject = “Monthly Report”
  102. $msg.Body = “Good MorningATTACHED”
  103. $msg.Attachments.Add($att)
  104. $smtp.Send($msg)

comments powered by Disqus