简体   繁体   中英

Sending Email Using SMTP4DEV in Powershell

I plan to do a powershell script for sending a email to end user via SMTP4DEV. Before i implement my script, i started to execute the Send-MailMessage command in powershell CMD for test out the flow first. But i am getting the following error message

 Send-MailMessage : The operation has timed out.
At line:1 char:1
+ Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -S ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpExcept
   ion
    + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage



I tested telnet to my SMTP4DEV , there's no anything blocking. I believed it's not required any credential for authentication because when i want to check my email, i am not required to login and just key in the url for checking the email.The online doc for SMTP4DEV also didnt shown any authentication required. My SMTP4DEV is running in HTTP

PS C:\Windows\system32> $From = "sender@domain.com"
PS C:\Windows\system32> $To = "support@domain.com"
PS C:\Windows\system32> $Subject = "test"
PS C:\Windows\system32> $Body = "test"
PS C:\Windows\system32> $SMTPServer = "x.x.x.x"
PS C:\Windows\system32> $SMTPPort = "2222"
PS C:\Windows\system32> Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port
 $SMTPPort



The sender and recipient are dummy user

Is your SMTP really configured on port 2222?

The standard SMTP ports are 25 (default SMTP port), 465, & 587. Also smtp4dev has default port as 25 mentioned in their README.md

refer to this blogs for more references which smpt port should I use and smtp port to use

I found the solution. The powershell script is working fine for me

$smtp = New-Object System.Net.Mail.SmtpClient
$to = New-Object System.Net.Mail.MailAddress($contractorEmail)
$from = New-Object System.Net.Mail.MailAddress("accessmgmt@xxx.com")

$msg = New-Object System.Net.Mail.MailMessage($from, $to)
$msg.subject = "xxx Account Extension"
$msg.body = "Dear xxx <br>"  
$msg.body += "This is the email content and body <br>"
$msg.body += "Best Regards <br>"
$msg.body += "Team B" 
$smtp.host = "x.x.x.x"

$smtp.send($msg)



i am not sure what is the different between send() and Send-MailMessage

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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