好的,所以我尝试使用Powershell v2将文本格式为新行的名为emailtargets的文本文件发送给某些收件人。下面是sendmailer脚本。
$emailSmtpServer = "mail.xxxxxxxxx.com"
$emailSmtpServerPort = "587"
$SourceRCPT = "xxxxx@xxxxxxxxx.com"
$DestFile = "emailTargets.txt"
$BodyFile = "emailBody.txt"
$SubjectLine = "testing"
$Pass = "xxxxxxx"
# Start Loop for Email Sending
$file = Get-Content $DestFile
$body = Get-Content $BodyFile
foreach ($line in $file){
$DestinationRCPT = $line.Split(",")[0]
$firstName = $line.Split(",")[1]
$lastName = $line.Split(",")[2]
$body =(Get-Content $BodyFile) | foreach-object {$_ -replace '\[firstName\]',$firstName}| foreach-object {$_ -replace '\[lastName\]',$lastName}
$emailSmtpUser = "$SourceRCPT"
$emailSmtpPass = "$Pass"
$emailFrom = "$SourceRCPT"
$emailTo = "$DestinationRCPT"
$emailMessage = New-Object System.Net.Mail.MailMessage( $emailFrom , $emailTo )
$emailMessage.Subject = $SubjectLine
$emailMessage.IsBodyHtml = $true
$emailMessage.Body = $body
$SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort )
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser.Split("@")[0] , $emailSmtpPass );
$SMTPClient.Send( $emailMessage )
Write-Host "Sending Email to $DestinationRCPT"
现在的问题是,在使用Office 365进行测试时,我不断出现以下错误
在使用其他SMTP服务器进行测试时,出现以下错误
我在做什么错了?
答案 0 :(得分:0)
这是一个证书验证问题,如果您可以取消它,可以这样做
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
在发送消息之前。
答案 1 :(得分:0)
这里有一些示例,可以帮助您重新编写代码以发挥作用。
Windows PowerShell Windows中可用的Cmdlet概述 PowerShell 2.0 https://technet.microsoft.com/en-us/library/ff714569.aspx
使用此示例 https://gallery.technet.microsoft.com/scriptcenter/Send-MailMessage-3a920a6d
...
$params = @{
InlineAttachments = $images
Attachments = 'C:\temp\attachment1.txt', 'C:\temp\attachment2.txt'
Body = $body
BodyAsHtml = $true
Subject = 'Test email'
From = 'username@gmail.com'
To = 'recipient@domain.com'
Cc = 'recipient2@domain.com', 'recipient3@domain.com'
SmtpServer = 'smtp.gmail.com'
Port = 587
Credential = (Get-Credential -Credential 'username@gmail.com')
UseSsl = $true
}
Send-MailMessage @params
或 #使用PS内置/在线帮助文件。
Get-Command -Name Send-MailMessage
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Send-MailMessage
3.1.0.0 Microsoft.PowerShell.Utility
# get function / cmdlet details
(Get-Command -Name Send-MailMessage).Parameters.Keys
Attachments
Bcc
Body
BodyAsHtml
Encoding
Cc
DeliveryNotificationOption
From
SmtpServer
Priority
Subject
To
Credential
UseSsl
Port
Verbose
Debug
ErrorAction
WarningAction
InformationAction
ErrorVariable
WarningVariable
InformationVariable
OutVariable
OutBuffer
PipelineVariable
# Get the examples and modify per your use case, splatting used to make it easier to read on the forum.
Get-help -Name Send-MailMessage -Examples
# Example 1: Send an email from one user to another
$Params = @{
To = "User01 <user01@example.com>"
From = "User02 <user02@example.com>"
Subject = "Test mail"
Credential = (Get-Credential -Credential 'user@fabrikam.com')
SmtpServer = "smtp.fabrikam.com"
UseSsl = $true
Port = 587
}
Send-MailMessage @Params
# This command sends an email message from User01 to User02.
#
# The mail message has a subject, which is required, but it does not have a
# body, which is optional. Also, because the SmtpServer parameter is not
# specified,
#
# Send-MailMessage uses the value of the $PSEmailServer preference variable
# for the SMTP server.
# Example 2: Send an attachment
$Params = @{
From = "User01 <user01@example.com>"
To = "User02 <user02@example.com>", "User03 <user03@example.com>"
Subject = "Sending the Attachment"
Body = "Forgot to send the attachment. Sending now."
Attachments = "data.csv"
Priority = High
dno = 'onSuccess, onFailure'
Credential = (Get-Credential -Credential 'user@fabrikam.com')
SmtpServer = "smtp.fabrikam.com"
UseSsl = $true
Port = 587
}
Send-MailMessage @Params
# This command sends an email message with an attachment from User01 to two
# other users.
# It specifies a priority value of High and requests a delivery notification
# by email when the email messages are delivered or when they fail.
# Example 3: Send email to a mailing list
$Params = @{
To = "User01 <user01@example.com>"
From = "ITGroup <itdept@example.com>"
Cc = "User02 <user02@example.com>"
bcc = "ITMgr <itmgr@example.com>"
Subject = "Don't forget today's meeting!"
Credential = 'domain01\admin01'
UseSsl = $true
SmtpServer = "smtp.fabrikam.com"
Port = 587
}
Send-MailMessage @Params
# This command sends an email message from User01 to the ITGroup mailing list
# with a copy (Cc) to User02 and a blind carbon copy (Bcc) to the IT manager
# (ITMgr).
#
# The command uses the credentials of a domain administrator and the UseSsl
# parameter.
获取帮助-名称Send-MailMessage-完整
获取帮助-名称Send-MailMessage-在线