简体   繁体   English

从Powershell命令行发送带有凭据的电子邮件

[英]Send email from powershell command line with credentials

I am trying to send an email from my gmail ccoiunt in just one single line using windows 8 and powersell. 我正在尝试使用Windows 8和powersell仅通过一行发送来自gmail ccoiunt的电子邮件。 This is the code I use: 这是我使用的代码:

Send-MailMessage -smtpServer 'smtp.gmail.com' -port 587 -from 'myself@gmail.com' -to 'myself@gmail.com' -subject 'Test' -body 'Body' –UseSsl

But I don't know how to add the credentials. 但是我不知道如何添加凭据。 How can I add username and password to this single code line? 如何在此单一代码行中添加用户名和密码? (there is no need to encrypt the password). (无需加密密码)。

Thanks 谢谢

Send-MailMessage -smtpServer 'smtp.gmail.com' -port 587 -from 'myself@gmail.com' -to 'myself@gmail.com' -subject 'Test' -body 'Body' –UseSsl -Credential (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "myUsername", (ConvertTo-SecureString -String "myPassword" -AsPlainText -Force))
     $EmailTo = "udit043.ur@gmail.com"  // abc@domain.com
     $EmailFrom = "udit821@gmail.com"  //xyz@gmail.com
     $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("udit821@gmail.com", "xxxxxxxx");    // xxxxxx-password
     $SMTPClient.Send($SMTPMessage)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM