shell - 使用 powershell {send-mailmessage} 检查 SMTP 服务器是否正常工作

标签 shell email powershell smtp monitoring

如何检查 SMTP 服务器是否正常工作。基本上我有:

SMTPserver= mail.my1stdomain.com, 
FromAddress=mymailbox@my1stdomain.com, 
ToAddress=Recipient@my1stdomain.com. 

我想检查是否可以发送电子邮件。如果可以的话没问题,但是如果 Powershell 脚本输出错误,那么我想使用我的第二个 SMTP 服务器来通知第一个 SMTP 服务器发送电子邮件失败。

我的第二组 SMTP 信息是:
SMTPserver=mail.my2nddomain.com, 
FromAddress=mymailbox@my2nddomain.com, 
ToAddress=Recipient@my2nddomain.com

我用这个脚本发送电子邮件:
Send-MailMessage -from mymailbox@my1stdomain.com -to Recipient@my1stdomain.com -Subject "Test OK" -Body "Test is OK" -SmtpServer mail.my1stdomain.com

当然要通过第二个 SMTP 服务器发送电子邮件,我必须这样做:
Send-MailMessage -from mymailbox@my2nddomain.com -to Recipient@my2nddomain.com -Subject "Test OK" -Body "Test is OK" -SmtpServer mail.my2nddomain.com

如果第一个命令失败,我不知道如何编写 IF 案例

最佳答案

你可以做一个简单的try catch:

try{
    Send-MailMessage -from mymailbox@my1stdomain.com -to Recipient@my1stdomain.com -Subject "Test OK" -Body "Test is OK" -SmtpServer mail.my1stdomain.com
   }
catch{
    Send-MailMessage -from mymailbox@my2nddomain.com -to Recipient@my2nddomain.com -Subject "Test OK" -Body "Test is OK" -SmtpServer mail.my2nddomain.com
   }

如果 try 中的代码块产生错误,它将运行 catch block 。

关于shell - 使用 powershell {send-mailmessage} 检查 SMTP 服务器是否正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39394948/

相关文章:

php - 如何在 PHP 中使用 SMTP 发送 HTML 电子邮件

powershell - 如何使用 PowerShell 打开然后关闭 "Devices and Printers"文件夹?

python - 从 Python 脚本执行 shell 命令

linux - 将行添加到文件末尾 - 使用 for 循环 - linux 脚本

Javascript - 从输入中删除焦点

php - 即时更改 mysql 列类型

powershell - New-ScheduledTaskTrigger -AtLogon重复

powershell - 如何运行计划的作业启动,使其具有交互性

java - 运行位于服务器上并映射为 U ://dirver on windows from windows in java application? 的 unix shell 脚本

linux - 在管道之后无法在源脚本中更改 bash 变量?