Powershell thunderbird在不打开窗口的情况下发送电子邮件
我试图通过power shell命令向thunder bird发送电子邮件,但我面临一个问题,所有的研究都没有找到解决方案 我的问题是,当我运行以下命令时,power shell会添加提供的信息,但随后它会打开窗口,我必须手动发送,但该发送也应由powershell auto完成。我无法执行此操作Powershell thunderbird在不打开窗口的情况下发送电子邮件,shell,powershell,thunderbird,Shell,Powershell,Thunderbird,我试图通过power shell命令向thunder bird发送电子邮件,但我面临一个问题,所有的研究都没有找到解决方案 我的问题是,当我运行以下命令时,power shell会添加提供的信息,但随后它会打开窗口,我必须手动发送,但该发送也应由powershell auto完成。我无法执行此操作 param( [string]$attachment='', [string]$to='test@outlook.com', # used to specify the email o
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"
如果没有使用雷鸟的实际要求,则雷鸟可能是使用雷鸟的替代品 使用您的基线,它可能如下所示:
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
大多数字段都接受字符串数组,以防有多个抄送收件人或类似收件人
请注意,您必须告诉Send MailMessage如何连接到邮件服务器,因为它不会神奇地读取您的Thunderbird配置。因此,您可能必须包括-From,-SmtpServer,甚至可能包括-Credential和-Port,这取决于邮件服务器的配置方式
根据您的需求,您可能需要进一步挖掘,并直接使用Net.Mail.SmtpClient。还有其他问题和示例,例如。您没有使用Send MailMessage,因为…?@Bill\u Stewart感谢您的回复我是powershell的新手,对此我不确定是否需要帮助运行此命令:help Send MailMessage。