none
Help with sending mail

    Question

  • I have three Hyper-V servers (2008R2 SP1) and are woring on a script that shall email the result.
    While testing with "send-mailmessage" cmdlet, I find it strange that it works on two of the servers but not on the third.

    The command is:
    send-mailmessage -to "User1 <user1@dom.tld>" -from "User2 <user2@dom.tld>" -subject "Test mail" -smtpserver mailserver.localdom.local

    On two of the servers it works perfectly on the third server i get this error:

    Send-MailMessage : Smtp server returned an invalid response.
    At line:1 char:17
    + send-mailmessage <<<<  -to "User1 <user1@dom.tld>" -from "User2 <user2@dom.tld>" -subject "Test mail" -smtpserver mailserver.localdom.local
        + CategoryInfo          : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException
        + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage

     

    I have also tried Smtp.Send, it works on the same two servers and fail on the third server, same as Send-MailMessage fail on.

     

    Any ideas ?

     

    - Duelund

     

     


    Best regards,
    Thursday, September 01, 2011 6:34 PM

All replies

  • It looks like your smtp server isn't accepting a connection from that third server. 

    I'd check the settings on that mail server and/or investigate the smtp logs.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    • Proposed as answer by Rich Prescott Friday, September 02, 2011 12:16 AM
    Thursday, September 01, 2011 6:42 PM
  • firewall/AV?
     
    try this from cmd (telnet client needs to be installed from windows
    features)
     
     

    Justin Rich
    http://jrich523.wordpress.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, September 01, 2011 6:45 PM
  • firewall/AV?
     
    try this from cmd (telnet client needs to be installed from windows
    features)
     
     

    Justin Rich
    http://jrich523.wordpress.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    No AV, No FW.

    The TELNET worked, I received mail.

     

    Nothing in the SMTP log, when trying with PS from the faulty server.

     

    Additional info:
    With Smtp.Send I get this error:

    Exception calling "Send" with "1" argument(s): "Failure sending mail."
    At line:1 char:11
    + $smtp.Send <<<< ($msg)
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : DotNetMethodException

     

    - Duelund


    Best regards,


    • Edited by Duelund Friday, September 02, 2011 9:48 AM
    Friday, September 02, 2011 8:15 AM
  • weird, have you tried using the .net mail namespace?

    $smtp = new-object system.net.mail.smtpclient "smtp.domain.com
    $msg=new-object system.net.mail.mailmessage "from@domain.com", "to@domain.com"
    $msg.subject = "test email"
    $msg.body = "this is a test"
    $smtp.send($msg)
    


    this might give more details as to the error, but, maybe not..


    Justin Rich
    http://jrich523.wordpress.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Friday, September 02, 2011 11:49 AM
  • weird, have you tried using the .net mail namespace?

    $smtp = new-object system.net.mail.smtpclient "smtp.domain.com
    $msg=new-object system.net.mail.mailmessage "from@domain.com", "to@domain.com"
    $msg.subject = "test email"
    $msg.body = "this is a test"
    $smtp.send($msg)
    


    this might give more details as to the error, but, maybe not..


    Justin Rich
    http://jrich523.wordpress.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    With $Smtp.Send I get this error:

    Exception calling "Send" with "1" argument(s): "Failure sending mail."
    At line:1 char:11
    + $smtp.Send <<<< ($msg)
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException


    Best regards,
    Friday, September 02, 2011 11:56 AM
  • so, that seems like it’s a problem with .net perhaps?
     
    do you know c#? maybe write a small app that does the same thing to see if
    it’s a configuration/restriction of powershell?
     
    because the telnet worked, that means that communication is fine (no port
    blocking) but since the .net method doesn’t work it makes me wonder if there
    is an issue with the .net install itself.
     
    outside of writing a C# app to test this id say use wireshark to see what
    data is actually being sent comparing powershell to a telnet session.
     
    the only other thing that comes to mind (which I don’t even know what to do
    with it) is that powershell uses unicode and a cmd prompt uses ascii,
    perhaps the mail server cant handle it? but then the question is, why wouldn’t
    the other two servers that work not be using unicode...
     
    the next step I would take is to use wireshark I guess.
     
     

    Justin Rich
    http://jrich523.wordpress.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Friday, September 02, 2011 1:37 PM
  • Hi Duelund,

     

    Thanks for posting here.

     

    So did you also get any error that regarding mail operation form Application log?

    You might also try to specify a user Credential by attaching “-Credential” parameter and see how is going:

     

    Send-MailMessage

    http://technet.microsoft.com/en-us/library/dd347693.aspx

     

    Regards,

     

    Tiger Li

     

    TechNet Subscriber Support in forum

    If you have any feedback on our support, please contact  tnmff@microsoft.com.


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Monday, September 05, 2011 2:28 AM
  • Hi Duelund,

    If there is any update on this issue, please feel free to let us know.

    Regards,

    Tiger Li

    TechNet Subscriber Support in forum
    If you have any feedback on our support, please contact  tnmff@microsoft.com.


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Tuesday, September 06, 2011 6:12 AM
  • Here is an update.

    Nothing in Application log when trying to send.

    No change using Send-MailMessage with "-Credential".

    Unfortunately, i am not familiar with C# or Wireshark.

     

    Is it possible to reinstall .Net ?

     

    -Duelund


    Best regards,
    Tuesday, September 06, 2011 6:38 AM
  • One other diagnostic and/or temporary workaround might be to revert to the old-school methods, and use blat.exe (or some equivalent) and see if that works.

    http://sourceforge.net/projects/blat/files/


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Tuesday, September 06, 2011 11:18 AM
  • on a 2k8 server, I don’t think so. not really sure. you could probably find
    some simple .net mail programs that would let you know if its from the
    .net.mail classes or not...
     
    that’s a step by step on how to make a mail tester in C#, I suspect it will
    be the same outcome, which would point at a .net problem, but, if it works,
    then the problem is else where.
     
     

    Justin Rich
    http://jrich523.wordpress.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Tuesday, September 06, 2011 7:04 PM
  • One other diagnostic and/or temporary workaround might be to revert to the old-school methods, and use blat.exe (or some equivalent) and see if that works.

    http://sourceforge.net/projects/blat/files/


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    Should Blat work on 64bit OS ?

    - Duelund


    Best regards,
    Wednesday, September 07, 2011 6:59 AM
  • For the error to  be more descriptive, can you please expand it after you have this:

    Exception calling "Send" with "1" argument(s): "Failure sending mail."
    At line:1 char:11
    + $smtp.Send <<<< ($msg)
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

    PS > $error[0].Exception | fl * -force

    post the output


    Ketan Thakkar | Microsoft Online Community Support
    Wednesday, September 07, 2011 8:18 AM
  • For the error to  be more descriptive, can you please expand it after you have this:

    Exception calling "Send" with "1" argument(s): "Failure sending mail."
    At line:1 char:11
    + $smtp.Send <<<< ($msg)
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

    PS > $error[0].Exception | fl * -force

    post the output


    Ketan Thakkar | Microsoft Online Community Support

    ErrorRecord                 : Exception calling "Send" with "1" argument(s): "Failure sending mail."
    StackTrace                  :    at System.Management.Automation.DotNetAdapter.AuxiliaryMethodInvoke(Object target, Obj
                                  ect[] arguments, MethodInformation methodInformation, Object[] originalArguments)
                                     at System.Management.Automation.DotNetAdapter.MethodInvokeDotNet(String methodName, Ob
                                  ject target, MethodInformation[] methodInformation, Object[] arguments)
                                     at System.Management.Automation.Adapter.BaseMethodInvoke(PSMethod method, Object[] arg
                                  uments)
                                     at System.Management.Automation.ParserOps.CallMethod(Token token, Object target, Strin
                                  g methodName, Object[] paramArray, Boolean callStatic, Object valueToSet)
                                     at System.Management.Automation.MethodCallNode.InvokeMethod(Object target, Object[] ar
                                  guments, Object value)
                                     at System.Management.Automation.MethodCallNode.Execute(Array input, Pipe outputPipe, E
                                  xecutionContext context)
                                     at System.Management.Automation.ParseTreeNode.Execute(Array input, Pipe outputPipe, Ar
                                  rayList& resultList, ExecutionContext context)
                                     at System.Management.Automation.StatementListNode.ExecuteStatement(ParseTreeNode state
                                  ment, Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)
    WasThrownFromThrowStatement : False
    Message                     : Exception calling "Send" with "1" argument(s): "Failure sending mail."
    Data                        : {}
    InnerException              : System.Net.Mail.SmtpException: Failure sending mail. ---> System.FormatException: Smtp se
                                  rver returned an invalid response.
                                     at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int
                                  32 read, Boolean readLine)
                                     at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean on
                                  eLine)
                                     at System.Net.Mail.ReadLinesCommand.Send(SmtpConnection conn)
                                     at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
                                     at System.Net.Mail.SmtpClient.Send(MailMessage message)
                                     --- End of inner exception stack trace ---
                                     at System.Net.Mail.SmtpClient.Send(MailMessage message)
                                     at Send(Object , Object[] )
                                     at System.Management.Automation.DotNetAdapter.AuxiliaryMethodInvoke(Object target, Obj
                                  ect[] arguments, MethodInformation methodInformation, Object[] originalArguments)
    TargetSite                  : System.Object AuxiliaryMethodInvoke(System.Object, System.Object[], System.Management.Aut
                                  omation.MethodInformation, System.Object[])
    HelpLink                    :
    Source                      : System.Management.Automation
    Best regards,
    Wednesday, September 07, 2011 9:53 AM
  • Thanks!

    Can you also get this?

    $error[0].Exception.InnerException | fl * -force

    if the output has more innerexception, bring that as well. ($error[0].Exception.InnerException.InnerException)


    Ketan Thakkar | Microsoft Online Community Support
    Wednesday, September 07, 2011 10:33 AM
  • Thanks!

    Can you also get this?

    $error[0].Exception.InnerException | fl * -force

    if the output has more innerexception, bring that as well. ($error[0].Exception.InnerException.InnerException)


    Ketan Thakkar | Microsoft Online Community Support

     

    $error[0].Exception.InnerException | fl * -force

    StatusCode     : GeneralFailure
    Message        : Failure sending mail.
    Data           : {}
    InnerException : System.FormatException: Smtp server returned an invalid response.
                        at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Bool
                     ean readLine)
                        at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine)
                        at System.Net.Mail.ReadLinesCommand.Send(SmtpConnection conn)
                        at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
                        at System.Net.Mail.SmtpClient.Send(MailMessage message)
    TargetSite     : Void Send(System.Net.Mail.MailMessage)
    StackTrace     :    at System.Net.Mail.SmtpClient.Send(MailMessage message)
                        at Send(Object , Object[] )
                        at System.Management.Automation.DotNetAdapter.AuxiliaryMethodInvoke(Object target, Object[] argumen
                     ts, MethodInformation methodInformation, Object[] originalArguments)
    HelpLink       :
    Source         : System

     

    $error[0].Exception.InnerException.InnerException | fl * -force

    Message        : Smtp server returned an invalid response.
    Data           : {}
    InnerException :
    TargetSite     : Int32 ProcessRead(Byte[], Int32, Int32, Boolean)
    StackTrace     :    at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Bool
                     ean readLine)
                        at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine)
                        at System.Net.Mail.ReadLinesCommand.Send(SmtpConnection conn)
                        at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
                        at System.Net.Mail.SmtpClient.Send(MailMessage message)
    HelpLink       :
    Source         : System

     

     


    Best regards,
    Wednesday, September 07, 2011 11:36 AM
  • I think so.  It shouldn't take but a few minutes to find out.  I can test it when I get to work.
    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Wednesday, September 07, 2011 11:43 AM
  • I think so.  It shouldn't take but a few minutes to find out.  I can test it when I get to work.
    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    But it Won't help me. I prefer to have the 'real' Send-mail to work.

    I have started testing a VM backuprogram, it has the same problem sending mail, only form this host.


    Best regards,
    Wednesday, September 07, 2011 12:32 PM
  • Okay.  If it has to be the .net method then you'll need to concentrate on getting the .net assemblies repaired on that server.
    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Wednesday, September 07, 2011 12:59 PM
  • I hope Ketan can see something usefull in the error description.
    Best regards,
    Thursday, September 08, 2011 11:44 AM
  • well im still seeing the same problem, the SMTP server turned something
    weird that the client (.net) couldn’t handle.
     
    would it be possible to see the email and server addresses you are using as
    well as the exact telnet commands you ran that worked to send an email?
     
     

    Justin Rich
    http://jrich523.wordpress.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, September 08, 2011 12:12 PM
  • I Kind of agree to Justin, as telnet could send mail through the same server. It's odd.

    Can you create a little exe out of it and debug the compiled code and verify that 'to' and 'from' or 'subject' all are in valid format?

    I'll check internally if its reported/known as I can't repro it on my test boxes.


    Ketan Thakkar | Microsoft Online Community Support
    Thursday, September 08, 2011 12:49 PM
  • well im still seeing the same problem, the SMTP server turned something
    weird that the client (.net) couldn’t handle.
     
    would it be possible to see the email and server addresses you are using as
    well as the exact telnet commands you ran that worked to send an email?
     
     

    Justin Rich
    http://jrich523.wordpress.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    telnet srvdk006.cbit.local 25

    220 SRVDK006.cbit.local Microsoft ESMTP MAIL Service ready at Thu, 8 Sep 2011 15
    :38:03 +0200

    ehlo cbit.dk


    mail from: test@cbit.dk
    250 2.1.0 Sender OK
    rcpt to: bdn@cbit.dk
    250 2.1.5 Recipient OK
    data
    354 Start mail input; end with <CRLF>.<CRLF>
    subject: Test mail
    This is a test

    .
    250 2.6.0 <2b5147f2-2ce7-4c38-8083-a220e545b603@SRVDK006.cbit.local> [InternalId
    =2125335] Queued mail for delivery

    And I received the mail.

     

    This is not working in PowerShell on this server (works on other servers):

    send-mailmessage -to "Duelund <bdn@cbit.dk>" -from "Test <test@cbit.dk>" -subject "Test mail" -smtpserver srvdk006.cbit.local


    Best regards,
    • Edited by Duelund Thursday, September 08, 2011 1:48 PM
    Thursday, September 08, 2011 1:47 PM
  • try this
     send-mailmessage -to "bdn@cbit.dk" -from "test@cbit.dk" -subject "Test
    mail" -body "Test mail"  -smtpserver srvdk006.cbit.local
     
     

    Justin Rich
    http://jrich523.wordpress.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, September 08, 2011 1:51 PM
  • I Kind of agree to Justin, as telnet could send mail through the same server. It's odd.

    Can you create a little exe out of it and debug the compiled code and verify that 'to' and 'from' or 'subject' all are in valid format?

    I'll check internally if its reported/known as I can't repro it on my test boxes.


    Ketan Thakkar | Microsoft Online Community Support

    I am not sure about this - I am not a programmer/developer - I only manage cmd, vbs and PS scripts.
    It is more than 15 years ago I have compiled anything (asm, C++, Pascal, Fortran).
    Best regards,
    Thursday, September 08, 2011 1:52 PM
  • Hi,

    Can you get a Netmon trace when you are running that piece of code? I'd like to see the response from SMTP server. You can either use wireshark or netmon.

    Compress it and send it to blrforum-at-microsoft-com (replace '-')


    Ketan Thakkar | Microsoft Online Community Support
    Friday, September 09, 2011 8:13 AM
  • try this
     send-mailmessage -to "bdn@cbit.dk" -from "test@cbit.dk" -subject "Test
    mail" -body "Test mail"  -smtpserver srvdk006.cbit.local
     
     

    Justin Rich
    http://jrich523.wordpress.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Same error.
    Best regards,
    Monday, September 12, 2011 6:17 AM
  • Hi,

    Can you get a Netmon trace when you are running that piece of code? I'd like to see the response from SMTP server. You can either use wireshark or netmon.

    Compress it and send it to blrforum-at-microsoft-com (replace '-')


    Ketan Thakkar | Microsoft Online Community Support

    Will it requires a reboot to install/uninstall Netmon ? It's a Hyper-V server in production.
    Best regards,
    Monday, September 12, 2011 6:22 AM
  • It should not require reboot.
    Ketan Thakkar | Microsoft Online Community Support
    Monday, September 12, 2011 7:30 AM
  • wireshark shouldn’t require a reboot either, and here is a walkthrough on
    capturing SMTP traffic.
     
     
    its basically the same for netmon. you'll just want to capture traffic on
    port 25.
     
     

    Justin Rich
    http://jrich523.wordpress.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, September 12, 2011 1:05 PM
  • try test this function:

     

    function SendMail([string] $smtpServer, [string] $smtpUser, [string] $smtpPassword, [string] $to, [string] $title, [string] $body) 

        $smtp = new-object System.Net.Mail.SmtpClient; 

        $smtp.Host = $smtpServer; 

        $smtp.Credentials = New-Object System.Net.NetworkCredential($smtpUser, $smtpPassword); 

        $message = New-Object System.Net.Mail.MailMessage; 

        $message.IsBodyhtml = $true; 

        $message.Body = $body; 

        $message.Subject = $title; 

        $message.From = New-Object System.Net.Mail.MailAddress $smtpUser; 

        $toItems = $to.Split(";"); 

     

        foreach($toItem in $toItems) 

        { 

            # -> Adiciona os e-mails válidos de destino. 

            if($toItem -ne $null -and $toItem.Trim() -ne "") 

            { 

                $toAdress = New-Object System.Net.Mail.MailAddress $toItem; 

                $message.To.Add($toAdress); 

            } 

        } 

        $smtp.Send($message); 

    }

    Monday, September 12, 2011 1:33 PM
  • I have tried $Smtp.Send earlier. It also fails.
    Best regards,
    Wednesday, September 14, 2011 7:05 AM
  • Hi,

    Can you get a Netmon trace when you are running that piece of code? I'd like to see the response from SMTP server. You can either use wireshark or netmon.

    Compress it and send it to blrforum-at-microsoft-com (replace '-')


    Ketan Thakkar | Microsoft Online Community Support

    A WireShark capture is on way.
    Best regards,
    Wednesday, September 14, 2011 7:31 AM
  • you can also send that this way if you like.
     
    jrich523 at gmail
     

    Justin Rich
    http://jrich523.wordpress.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Wednesday, September 14, 2011 11:52 AM
  • I has been a while since I had time to look at this.

    Kethan looked at some Netmon and Wireshark dumps, without finding any clues.

     

    We have now introduced more Hyper-V servers in our cluster, which make it easier for us to mover servers around.

    Today I discovered that the send-mail error only fails on this specific Hyper-V server if it hosts the virtual Exchange server itself.
    If another Hyper-V server hosts the Exchange server it works. And this another host, can send mail to the virtual Exchange it host by itself.

    Seems like there is a problem with the "faulty" Hyper-v server.


    Best regards,
    Tuesday, November 22, 2011 9:28 AM
  • Hi,

    As per our last conversation, you were going to reinstall .NET and I suggested doing that in front of a Support engineer on call.

    In any case, this case has gone well beyond the forum support boundaries, please get a phone call support.

     

    Thanks!


    Ketan Thakkar | Microsoft Online Community Support
    Tuesday, November 22, 2011 11:06 AM