简体   繁体   English

使用Powershell发送电子邮件

[英]Sending a email with powershell

I want to send a email with powershell. 我想发送带有Powershell的电子邮件。 The script works fine if I type my credential in manualy. 如果我手动输入凭据,脚本可以正常工作。 But I want to give the credential parameters within the script. 但是我想在脚本中提供凭据参数。 My script looks like this: 我的脚本如下所示:

$From = "test@yahoo.de"
$To = "test2@yahoo.de"
$Cc = "test@yahoo.de"
$Attachment = "C:\Users\test\test\test.ini"
$Subject = "Email Subject"
$Body = "Insert body text here"
$SMTPServer = "smtp.mail.yahoo.com"
$SMTPPort = "587"
Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
-Credential ( 
$MyClearTextUsername=’test@yahoo.de’
$MyClearTextPassword=’test123’

$SecurePassword=Convertto-SecureString –String $MyClearTextPassword –AsPlainText –force

$MyCredentials=New-object System.Management.Automation.PSCredential $MyClearTextPassword,$SecurePassword) -Attachments $Attachment

Here's how you can create a credentials object: 这是创建凭证对象的方法:

$cred = ([pscredential]::new('test@yahoo.de',(ConvertTo-SecureString -String 'test123' -AsPlainText -Force)))

so in your case use: 所以在您的情况下使用:

Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject `
  -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
  -Credential $cred -Attachments $Attachment

I see no point in trying to fit that into the Send-MailMessage , just create it before and reference it. 我认为尝试将其放入Send-MailMessage没有任何意义,只需在之前创建它并对其进行引用即可。 easier to read. 更容易阅读。

If you're using Office365 to send email, you might want to try this: 如果您使用Office365发送电子邮件,则可能需要尝试以下操作:

# Sending an email from PowerShell 5.1 script through outlook.office365.com
#
# 1. Create an encrypted password file
#   PS > Read-Host -AsSecureString | ConvertFrom-SecureString | Out-File -FilePath <passwordfile>
#   This will prompt you for a password, encrypt and save in <passwordfile>
# 2. Obtain Outlook Office365 SMTP server name.
#   Go to your ISP and find the value of the MX record. For example <yourdomain>.mail.protection.outlook.com
# 3. If after running the script you get this error:
#   Send-MailMessage : Mailbox unavailable. The server response was: 5.7.606 Access denied, banned sending IP [X.X.X.X].
#   You will need to delist your IP by going here: https://sender.office.com/
#   Note:  Removing you IP from the block list could take up to 30 minutes.
#
$User = "<SMPT loging username>"
$PasswordFile = "<passwordfile>"
$SMTPCredentials=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $PasswordFile | ConvertTo-SecureString)
$EmailTo = "<to email address>"
$EmailFrom = "<from email address>"
$Subject = "<email subject>" 
$Body = "<email body>" 
$SMTPServer = "<Outlook STMP Server from MX record>"
Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $Subject -Body $Body -SmtpServer $SMTPServer -Port 25 -Credential $SMTPCredentials -UseSsl

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

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