$EmailSmtpServer = "smtp.domain.com"
$EmailFrom = "email#domain.com"
$EmailTo = "email#domain.com"
$EmailSubject = "Subject"
$EmailBody = "Body"
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$EmailSubject,$EmailBody)
$SMTPClient = New-Object Net.Mail.SmtpClient($EmailSmtpServer, 465)
$SMTPClient.EnableSsl = $false
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("Username", "Password");
$SMTPClient.Send($SMTPMessage)
I keep getting the below error:
Exception calling "Send" with "1" argument(s): "Failure sending mail."
At line:10 char:1
+ $SMTPClient.Send($SMTPMessage)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SmtpException
Related
i am trying to make a simple powershell mail sender script , but i keep getting an exception in the "Send-MailMessage " command , but i don't quite understand why .
this is the script : $Sender = "powershell121#gmail.com" $Reciever = "powershell122#gmail.com" $Subject = "testing script" $Body = " this is a mail sender powershell script , from me to me " $SMTPServer = "smtp.gmail.com" $SMTPPort = "587" Send-MailMessage -From $Sender -to $Reciever -Subject $Subject
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl -Credential (Get-Credential)
---------------------------------------------------------------------------------------- the exception i'm getting :
` Send-MailMessage : The SMTP server requires a secure connection or the client was not authenticated. The server
response was: 5.7.0 Authentication Required. Learn more at
At line:7 char:1
+ Send-MailMessage -From $Sender -to $Reciever -Subject $Subject `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpExcept
ion
+ FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
I am trying to send the mail from PowerShell.
$EmailFrom = "xxxxxx#gmail.com"
$EmailTo = "xxxxx#gmail.com"
$Subject = "Subject"
$Body = "Body"
$filenameAndPath = "C:\Desktop\EE.txt"
$SMTPServer = "smtp.gmail.com"
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom, $EmailTo, $Subject, $Body)
$attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
$SMTPMessage.Attachments.Add($attachment)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("xxxx#gmail.com", "password");
$SMTPClient.Send($SMTPMessage)
When I run this code I get the following exception:
Exception calling "Send" with "1" argument(s): "Failure sending mail."
At line:13 char:1
+ $SMTPClient.Send($SMTPMessage)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SmtpException
How can I make $SMTPClient.Send() work correctly?
Is Send-MailMessage not an option for you?
You could do the following:
$EmailFrom = "xxxxxx#gmail.com"
$EmailTo = "xxxxx#gmail.com"
$Subject = "Subject"
$Body = "Body"
$filenameAndPath = "C:\Desktop\EE.txt"
$SMTPServer = "smtp.gmail.com"
Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $Subject -body $Body -Attachments $filenameAndPath -SmtpServer $SMTPServer
Would this example work?
##############################################################################
$From = "YourEmail#gmail.com"
$To = "AnotherEmail#YourDomain.com"
$Cc = "YourBoss#YourDomain.com"
$Attachment = "C:\temp\Some random file.txt"
$Subject = "Email Subject"
$Body = "Insert body text here"
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
-Credential (Get-Credential) -Attachments $Attachment
##############################################################################
Notice that it asks for credentials and also specifies to UseSSL.
It's from https://www.pdq.com/blog/powershell-send-mailmessage-gmail/
I have found when using Powershell script to send emails in Gmail you have to first log into the Gmail account and allow the location/IP address of the sending PC to send Emails. From the same PC running the script log into Gmail and there should be a security email. Click allow and then test.
I am trying to send attachments in Send-MailMessage without saving to disk first by using the answer I found in this thread, which points to this URL.
It says to use:
$attachment = [System.Net.Mail.Attachment]::CreateAttachmentFromString($attachmenttext,"test.txt")
But when I try to do that with Send-MailMessage instead of the complicated way it shows, I get this error:
Send-MailMessage -From "email#email.com" -To "email#email.com" -Subject "Subject" -Body $body -SmtpServer "smtp.server.local" -Port 25 -BodyAsHtml -Attachments $attachment
Send-MailMessage : Could not find file 'C:\Windows\system32\System.Net.Mail.Attachment'.
At line:3 char:1
+ Send-MailMessage -From "email#email.com" -To "email#e ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Send-MailMessage], FileNotFoundException
+ FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.SendMailMessage
Is there anyway to continue using Send-MailMessage with this method? Or is there a different method that will allow me to attach files without first saving to disk while still using send-mailmessage?
Let me first go over whats wrong. [System.Net.Mail.Attachment]::CreateAttachmentFromString outputs type System.Net.Mail.Attachment while send-mailmessage -attachments is looking for an array of strings string[]. Thats why the example will fail.
I have written a simple function so you can use the attachment you have posted in the example which creates a MailMessage and looks for attachments of type [System.Net.Mail.Attachment].
Function Try-SendMail([string[]]$To, [string]$From, [string]$SmtpServer, [int]$Port = 25, [pscredential]$SmtpCredential, [string]$Subject, [string]$Body, [System.Net.Mail.Attachment[]]$attachment, [switch]$IsBodyHTML){
[System.Net.Mail.MailMessage]$Mail = new-object System.Net.Mail.MailMessage
$To | %{$Mail.To.Add($_)}
$Mail.From = $From
$Mail.IsBodyHtml = $IsBodyHTML
$Mail.Body = $Body
$Mail.Subject = $Subject
$Attachment | %{$mail.Attachments.Add($_)}
[System.Net.Mail.SmtpClient]$SMTP = new-object System.Net.Mail.SmtpClient
$SMTP.Host = $SmtpServer
$SMTP.Port = $Port
If($SmtpCredential){
$NetCredential = New-Object System.Net.NetworkCredential
$NetCredential.UserName = $SmtpCredential.GetNetworkCredential().UserName
$NetCredential.Password = $SmtpCredential.GetNetworkCredential().Password
$SMTP.Credentials = $NetCredential
}
try{
$SMTP.Send($Mail)
}catch{
$_ | select *
}
}
you can use it like
$To = #("Person1#Test.com","Person2#Test.com")
$From = "MainGuy#PErson.com"
$Server = "SMTPSERVER.NET"
$Port = 587
$Attachments = #(
$([System.Net.Mail.Attachment]::CreateAttachmentFromString("HELLO", "Test.txt")),
$([System.Net.Mail.Attachment]::CreateAttachmentFromString("HELLO2", "Test2.txt"))
)
$SmtpCredential = Get-Credential
Try-SendMail -to $To -From $From -Subject "Hello" -Body "World" -Attachment $Attachments -SmtpServer $Server -port $Port -SmtpCredential $SmtpCredential
*Edited forgot to make [System.Net.Mail.Attachment] to [System.Net.Mail.Attachment[]] in the function parameters
I am trying to download the latest artifact from a Nexus repository. If I give the exact zip file name, it is working fine. When I try to download using a generic URL (REST URI) its giving me 401 Unauthorized. I have tried Invoke-WebRequest, WebClient and Invoke-RestMethod as well.
$wc = New-Object System.Net.WebClient
$URL = "http://nexusrepo/nexus/service/local/artifact/maven/redirect?r=my-snapshot&g=my.group.id&a=my.artifact.id&v=1.10.0-SNAPSHOT&c=win32.win32.x86_64&p=zip"
$username = "nexus"
$password = "nexus"
$auth = $username + ":" + $password
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$Encoded = [System.Text.Encoding]::UTF8.GetBytes($auth)
$EncodedPassword = [System.Convert]::ToBase64String($Encoded)
$wc.Headers.Add("Accept-Encoding", "gzip,deflate")
$wc.Credentials = New-Object System.Net.NetworkCredential($username, $password)
$wc.Headers.Add("Authorization", "Basic " + $EncodedPassword)
$wc.UseDefaultCredentials = $false
$wc.DownloadFile($URL, "MyApp.zip")
Exception calling "DownloadString" with "1" argument(s): "The remote server
returned an error: (401) Unauthorized."
At C:\temp\NexusDownloadTest\Nexus-Download.ps1:39 char:1
+ $weburl = $wc.DownloadString($URL)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
Can someone please help me on this?
As an workaround, I went back to Invoke-WebRequest and first got the redirected URL by using MaximumRedirection 0 and then submitted that URL as a request. The below code works.
$URL = "http://nexusrepo/nexus/service/local/artifact/maven/redirect?r=my-snapshot&g=my.group.id&a=my.artifact.id&v=1.10.0-SNAPSHOT&c=win32.win32.x86_64&p=zip"
$username = "nexus"
$password = "nexus"
$auth=$username+":"+$password
$Encoded = [System.Text.Encoding]::UTF8.GetBytes($auth)
$EncodedPassword = [System.Convert]::ToBase64String($Encoded)
$latestArtifactURL = Invoke-WebRequest $url -Headers #{Authorization = "Basic $EncodedPassword"} -MaximumRedirection 0
$redirectedMessage = "$latestArtifactURL".IndexOf('http:')
$targetURL = "$latestArtifactURL".SubString("$redirectedMessage")
Invoke-WebRequest $targetURL -Headers #{Authorization = "Basic $EncodedPassword"} -OutFile "MyApp.zip"
I am trying to run a PowerShell script from windows server 2012 R2 (PowerShell version 4) that sends out an E-mail. I am able to telnet to smtp.gmail.com port 587.
However, The e-mail is not being sent out.
Below is the code snippet and the exception.
$RPT_PWD='userpass'
$Attachment = $args[2]
$EmailTo = $args[1]
$Subject = $args[0]
$EmailFrom = "usertest#gmail.com"
$SmtpServer = "smtp.gmail.com"
$SmtpPort = "587"
$SMTPClient = New-Object System.Net.Mail.SmtpClient($SmtpServer, $SmtpPort)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object
System.Net.NetworkCredential($EmailFrom,$RPT_PWD); # credentials to be used
$objMessage = New-Object System.Net.Mail.MailMessage
$Body = "Email Body"
$objMessage = New-Object System.Net.Mail.MailMessage –ArgumentList
$EmailFrom, $EmailTo, $Subject, $Body
$objMessage.Attachments.Add($Attachment)
$SMTPClient.Send($objMessage)
Exception calling "Send" with "1" argument(s): "Failure sending mail."
At E:\Scripts\DailySystemProcessCheck\EmailTest\mailx_ssl.ps1:133 char:2
+ $SMTPClient.Send($objMessage)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SmtpException
Also,I also tried using send-mailmessage and it didn't work either.
$Credentials = New-Object System.Management.Automation.PSCredential -
ArgumentList $EmailFrom, $($RPT_PWD | ConvertTo-SecureString -AsPlainText -Force)
send-mailmessage -from $EmailFrom -to $EmailTo -subject $Subject -body $Body
-Attachment $Attachment -smtpServer smtp.gmail.com -Credential $Credentials
-UseSsl -Port 587
~~~~
send-mailmessage : The client and server cannot communicate, because they do
not possess a common algorithm
At E:\Scripts\DailySystemProcessCheck\EmailTest\mailx_ssl.ps1:131 char:2
+ send-mailmessage -from $EmailFrom -to $EmailTo -subject $Subject -body
$Body -A ...
+
+ CategoryInfo : InvalidOperation:
(System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpExcept
ion
+ FullyQualifiedErrorId :
SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
I tried to run the script using "Powershell --Version 2.0" and now I don't see SMTP Exception, rather I see another one.
Send : Exception calling "Send" with "1" argument(s): "A from address must
be specified."
At E:\Scripts\DailySystemProcessCheck\EmailTest\mailx_ssl.ps1:124 char:18
+ $SMTPClient.Send <<<< ($objMessage)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Can anyone please advise?
Thanks in advance