2013-07-10 98 views
0

我想在使用Windows 8和powersell的一行中從我的gmail ccoiunt發送一封電子郵件。這是我使用的代碼:使用憑據從powershell命令行發送電子郵件

Send-MailMessage -smtpServer 'smtp.gmail.com' -port 587 -from '[email protected]' -to '[email protected]' -subject 'Test' -body 'Body' –UseSsl 

但我不知道如何添加憑據。我怎樣才能將用戶名和密碼添加到這個單一的代碼行? (不需要加密密碼)。

感謝

+0

的可能重複[如何憑據傳遞到發送,命令MAILMESSAGE用於發送電子郵件(http://stackoverflow.com/questions/12460950/how-to-pass -createntials-to-the-send-mailmessage-command-for-sending-emails) –

+0

它不是重複的,因爲「-credential $ mycredentials」在單一代碼行中沒有證書。我不知道如何將用戶名和密碼添加到該「-credential」命令 – Ton

+0

嘗試閱讀答案,而不僅僅是問題。 –

回答

0
Send-MailMessage -smtpServer 'smtp.gmail.com' -port 587 -from '[email protected]' -to '[email protected]' -subject 'Test' -body 'Body' –UseSsl -Credential (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "myUsername" (ConvertTo-SecureString -String "myPassword" -AsPlainText -Force)) 
0
 $EmailTo = "[email protected]" // [email protected] 
    $EmailFrom = "[email protected]" //[email protected] 
    $Subject = "zx" //subject 
    $Body = "Test Body" //body of message 
    $SMTPServer = "smtp.gmail.com" 
    $filenameAndPath = "G:\abc.jpg" //attachment 
    $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("[email protected]", "xxxxxxxx"); // xxxxxx-password 
    $SMTPClient.Send($SMTPMessage) 
相關問題