powershell script using Net.Mail.SmtpClient not working for my email vendors server

Answered powershell script using Net.Mail.SmtpClient not working for my email vendors server

  • Thursday, February 21, 2013 6:18 PM
     
      Has Code

    I can use some other tools to send emails (like http://glob.com.au/sendmail/ for example), but for some reason I can't send emails with Net.Mail.SmtpClient.

    I am using the same settings as I have configured in my email client (outlook) as well. The vendors server (which I have no access to) uses a different outgoing port (465) and SSL. Currently just trying to send emails to myself.

    $CredUser = "me@mydom.com"
    $CredPassword = "mypass!"
    
    $EmailFrom = $CredUser
    $EmailTo = $CredUser 
    $Subject = "Test mail Subject"
    $Body = "Test Email Body" 
    $SMTPServer = "www.myemailvendor.biz"
    
    $msg = new-object Net.Mail.MailMessage
    $msg.From = $EmailFrom
    $msg.to.Add($EmailTo)
    $msg.Subject = $Subject
    
    $msg.Body = $Body
    
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 465) 
    $SMTPClient.EnableSsl = $true 
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($CredUser, $CredPassword) 
    
    $SMTPClient.Send($msg)

    The error I get is: Exception calling "Send" with "1" argument(s): "The operation has timed out."

    How can I troubleshoot this further


    • Edited by red888 Thursday, February 21, 2013 6:19 PM
    •  

All Replies

  • Thursday, February 21, 2013 6:31 PM
     
     

    Use Send-MailMessage.  It's free and works better with PowerShell.

    You need to unblock your firewall.


    ¯\_(ツ)_/¯

  • Thursday, February 21, 2013 9:12 PM
     
      Has Code

    Thanks for the reply, but I had tried send-mailmessage before and got the same timeout problem. The only reason I was using the class directly was because I read some people were having issues with the -port parameter not being used (not sure if this is a real problem or not)

    When using send-mailmessage:

    $CredUser = "me@mydom.com" $CredPassword = "mypass!" $EmailFrom = $CredUser $EmailTo = $CredUser $Subject = "Test mail Subject" $Body = "Test Email Body" $SMTPServer = "www.myemailvendor.biz" $emailCreds = New-Object System.Management.Automation.PSCredential($CredUser,(ConvertTo-SecureString -String $CredPassword -AsPlainText -Force)) Send-MailMessage -from $CredUser -to $CredUser -Subject $Subject -Body $Body -Credential $emailCreds -UseSsl -SmtpServer $SMTPServer -Port 465

    Send-MailMessage : The operation has timed out.

    Like I said I can send emails with other tools using the exact same settings which makes me question if this is an issue with my firewalls. Do you think its something with my email server?. Is there a way to get back a more detailed error message?

    Like I said I have zero access to this server which makes troubleshooting extra difficult. I'm trying to figure out why sending emails this way does not work, but other tools do.

  • Thursday, February 21, 2013 9:41 PM
     
     

    What port are you tryihng to use.  SMTP uses either port 25 or port 587.  Gmail has some odd port (465).  In all cases the firewall needs to be popened to allow outbound smtp traffic.  Most corporate networks lock down all smtp ports.

    If you are using PowerShell V2 then you need to use the mail client method if you are not using SMTP port 25.

    Timeout indicates that teh path to or from the server was blocked.

    You need to contact the network admin or teh ISP/vendor for more assistance.

    Most corporations explicitly block gmail port 465 for security reasons.  Your network admin would be able to verify this.


    ¯\_(ツ)_/¯

  • Thursday, February 21, 2013 10:21 PM
     
     
    But I have my mail client also using this port and it never has any issues sending emails. The outgoing port is 465 and the incoming is 995
  • Thursday, February 21, 2013 10:31 PM
    Moderator
     
     

    It sounds to me like you don't have a scripting question. I would start by asking your network administrator for assistance.

    Bill

  • Thursday, February 21, 2013 11:07 PM
     
     
    But I have my mail client also using this port and it never has any issues sending emails. The outgoing port is 465 and the incoming is 995

    Have you checked you AV software.  It may be set to allow cleints but not to allow scripts.  The Net client knows nothing about incoming ports.  Do you meant the POP3 port?  POP3 is not incoming. It is the POP3 protocol mail management port and has nothing to do with SMTP.  Outgoing mail is sent via SMTP.  Incoming mail is received via POP3.  Both protocols use a TCP port to esatblish a session and a randomly assigned return port to read the responses.  Nothing is sent in. Everything is read from the host using TCP/IP protocol.

    Programs are monitored by most AV scanners and third party firewalls. They block all traffic except that which has been designated.

    YOur Nework administrator would be the best person to help you figure this out.

    This question/problem has nothing to do with scripting.  Assuming your information about the ports and server name are correct then the scritp is correct.  The problem is external.

    Also note that RBL lists will block you if you probe constantly from an unregistered address.  When you secure from aan SMTP client program (POP3) you usi=ually authenticate on POP3 first.  If you do not and attemtp to authenticate on SMTP first teh server may well assign you as a spam sopurce and block your IP.  This will also cause a timeout. YOU may have to wait 24 hours or more to see the block removed.  If the IP you are sourceing from is blacklisted your Gmail client will work but no other direct SMTP methods will work due to RBL listing.

    Your IP will be the external IP of the corporate environment and not your local workstation IP.

    Sorry but there is no simple answer to your dilemma.


    ¯\_(ツ)_/¯

  • Friday, February 22, 2013 12:22 PM
    Moderator
     
     Answered

    I'd throw Network Monitor onto your system - and look for the outgoing SMTP connection. That may help diagnose your issue.

    In general, time outs are due to the IP address not really existing, the port not existing (or being blocked) and firewalls. You shoudl take a look at your firewall logs and event logs, and double check the port for your SMTP server.


    Thomas Lee <DoctorDNS@Gmail.Com>

  • Friday, February 22, 2013 2:53 PM
     
     

    Now that I know this is probably a network issue, can I move this thread to the correct forum, just to preserve the history of this thread for reference, or should I just close this thread and open a new thread?

    ------------

    Continuing to troubleshoot this:

    Using wireshark to start a capture and then running the script I see a complete tcp\ip handshake made to the vendor's smtp server.

    Then nothing happens for about a minute.

    Then my computer sends a FIN, ACK packet to the smtp server and immediately after the smtp server sends back a RST and that's it.

    When running a capture with the email tool that works I can see my computer sending pop packets with my email account name and password in plain text when setting up the connection and after that a bunch of TLS packets being exchanged.

  • Friday, February 22, 2013 3:27 PM
    Moderator
     
     

    Hi,

    Go ahead and start a new thread in the forum of your choice. You can give this thread's URL as a reference if needed.

    Bill

  • Friday, February 22, 2013 3:49 PM
     
     
    I'll close this thread, but I think figured the issue out. I think the server requires "pop before smtp".
  • Friday, February 22, 2013 4:08 PM
     
     
    I'll close this thread, but I think figured the issue out. I think the server requires "pop before smtp".

    If gmail servers then you are wrong.  I post to gmail using smtp all of the time with no issues.  It looks more like your connection Is being rejected because of a protocol violation which is likely caused by your local firewall blocking the return packets. Your client (Net) is not receving the response packet hence the timeout.

    Authentication violations do not cause a timeout.  They cause a rejection with cause.


    ¯\_(ツ)_/¯