2016-04-01 20 views
7

ho bisogno di qualche consiglio in qui .. I'd piace per inviare e-mail da PowerShell così io uso questo comandoCome inviare e-mail con PowerShell

$EmailFrom = “[email protected]” 
$EmailTo = “[email protected]” 
$Subject = “today date” 
$Body = “TODAY SYSTEM DATE=01/04/2016 SYSTEM TIME=11:32:05.50” 
$SMTPServer = "smtp.mail.yahoo.com” 
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) 
$SMTPClient.EnableSsl = $true  
$SMTPClient.Credentials = New-Object 
System.Net.NetworkCredential(“[email protected]”, “password”)  
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body) 

Questo comando non ha funzionato per la posta Yahoo e prospettive mail, ma funziona per la mia gmail. c'è qualcosa di sbagliato che ho fatto?

+3

qualsiasi ragione non [? Usare 'Send-MailMessage'] a (http://stackoverflow.com/a/36343788/712649) –

+0

I sto solo imparando PS, non sapevo che il comando esiste – Jessica

+1

Non dimenticare che puoi cercare. Ad esempio: [http://www.google.com/search?&q=send+email+da+powershell](http://www.google.com/search?&q=send+email+da+powershell). –

risposta

11

seguente frammento di codice funziona davvero per me:

$Username = "MyUserName"; 
$Password = "MyPassword"; 
$path = "C:\attachment.txt"; 

function Send-ToEmail([string]$email, [string]$attachmentpath){ 

    $message = new-object Net.Mail.MailMessage; 
    $message.From = "[email protected]"; 
    $message.To.Add($email); 
    $message.Subject = "subject text here..."; 
    $message.Body = "body text here..."; 
    $attachment = New-Object Net.Mail.Attachment($attachmentpath); 
    $message.Attachments.Add($attachment); 

    $smtp = new-object Net.Mail.SmtpClient("smtp.gmail.com", "587"); 
    $smtp.EnableSSL = $true; 
    $smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password); 
    $smtp.send($message); 
    write-host "Mail Sent" ; 
    $attachment.Dispose(); 
} 
Send-ToEmail -email "[email protected]" -attachmentpath $path; 
+0

A proposito, devi consentire l'accesso per le app meno sicure nelle impostazioni dell'account Gmail. Funziona almeno con Gmail. – IgrDi

+0

Grazie mille per la soluzione, sono nuovo in PS questo è davvero utile .. – Jessica

+0

Sono sempre felice di aiutare ... – IgrDi

Problemi correlati