我正在尝试通过power shell命令向雷鸟发送电子邮件,但我遇到了问题并且所有研究工作都找到了解决方案
我的问题是,当我运行以下命令时,电源外壳添加提供的信息,但随后它打开窗口,我必须手动发送,但发送也应该由powershell自动完成无法做到
param(
[string]$attachment='',
[string]$to='test@outlook.com', # used to specify the email of the recipient
[string]$cc='', # used to specify the email of the recipient of a copy of the mail
[string]$bcc='', # used to specify the email of the recipient of a blind copy of the mail*
[string]$subject='test@outlook.com', # subject of the mail
[string]$body='test@outlook.com', # body of the mail
[string]$exec= '"C:\Program Files `(x86`)\Mozilla Thunderbird\thunderbird.exe"' # mail client, if not default type path to Thunderbird installation, eg. C:\Program Files (x86)\Mozilla\Thunderbird\thunderbird.exe
)
# ------ Get default mail client -------
if ($exec) {
$MailClient = $exec + "-compose `"%1`""
}
else {
$node = Get-ItemProperty HKCU:\Software\Classes\mailto\shell\open\command
if (!$node) {
$node = Get-ItemProperty HKLM:\Software\Classes\mailto\shell\open\command
}
$MailClient = $node.'(default)'
}
# TODO check if default client is compatible
# ------ Read email fields -------------
$cmdParams = New-Object Collections.Generic.List[string]
$emailFields = @{"to"=$to; "cc"=$cc; "bcc"=$bcc; "subject"= $subject; "body" = $body}
$emailFields.Keys | % { $value = $emailFields[$_]
if ($value) {
$cmdParams.Add("$_=`'$value`'");
}
}
# Assign ATTACHMENTS
if ($attachment) {
if (Test-Path($attachment)) {
$fullPath = (Get-Item $attachment).FullName;
$cmdParams.Add("attachment=`'$fullPath`'");
} else {
echo "The path `"$file`" does not exists."
}
} elseif (@($input).Count -gt 0) {
# Try to assign ATTACHMENTS parameter from pipeline
$input.reset()
$attachParam = "attachment=`'"
foreach ($filename in $input) {
$fullPath = $filename.FullName + ","
$attachParam += $fullPath
}
$attachParam += "`'"
$cmdParams.Add($attachParam)
}
# ----- Build & run command -------------
$command = ''
if ($cmdParams.Count -gt 0) {
foreach ($param in $cmdParams) {
$command += $param + ","
}
}
$command = $MailClient -replace "`%1", $command
Invoke-Expression "& $command"
答案 0 :(得分:1)
如果没有实际要求使用Thunderbird Send-MailMessage
可能是另一种选择。
使用您的基线,它可能如下所示:
param(
[string]$attachment='C:\Temp\file.txt',
[string]$to='test@outlook.com', # used to specify the email of the recipient
[string]$cc='', # used to specify the email of the recipient of a copy of the mail
[string]$bcc='', # used to specify the email of the recipient of a blind copy of the mail*
[string]$subject='test@outlook.com', # subject of the mail
[string]$body='test@outlook.com', # body of the mail
)
# ------ Get default mail client -------
Send-MailMessage -To $to -Cc $cc -Bcc $bcc -Subject $subject -Body $body -Attachment $attachment -From powershell@example.com -SmtpServer mail.example.com
如果您有多个CC收件人或simiar,大多数字段都接受字符串数组。
请注意,您必须告诉Send-MailMessage
如何连接到邮件服务器,因为它不会神奇地读取您的Thunderbird配置。因此,您可能需要包含-From
,-SmtpServer
甚至-Credential
和-Port
,具体取决于您的邮件服务器的配置方式。
根据您的要求,您可能需要深入挖掘并直接使用Net.Mail.SmtpClient
。还有其他一些问题,例如Send mail via gmail with PowerShell V2's Send-MailMessage。