2016-04-01 30 views
7

ile e-posta göndermek .. razı olmama powershell e-posta göndermek ister bu yüzden bu komutu kullanmaknasıl ben buraya biraz tavsiyeye ihtiyaç 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) 

Bu komut o yahoo posta ve görünüm açısından hiç bir çalışma mail, ama gmailim için çalışıyor. Yaptığım bir yanlışlık var mı?

+3

Herhangi bir neden [? Send-MailMessage' 'kullanmak] değil (http://stackoverflow.com/a/36343788/712649) –

+0

ben sadece PS öğreniyorum, bu komutu bilmiyordum – Jessica

+1

Aradığınızı unutmayın. Örneğin: [http://www.google.com/search?&q=send+email+from+powershell](http://www.google.com/search?&q=send+email+from+powershell]. –

cevap

11

ardından kod parçacığı gerçekten benim için çalışıyor:

$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

Bu arada, gmail hesabı ayarlarınızda daha az güvenli uygulamalar için erişime izin vermeniz gerekir. En azından gmail ile çalışır. – IgrDi

+0

Çözüm için çok teşekkür ederim, PS'de yeniyim, bu gerçekten çok faydalı .. – Jessica

+0

Her zaman yardımcı olmaktan mutluluk duyuyorum ... – IgrDi