locked
SMTP from different domain RRS feed

  • Question

  • I am trying to send an email using an SMTP server using my powershell script and I receive this.
    The smtp account I used to send is user01@abc.com . SMTP server is at smtp.abc.com
    From my script I am sending to a different domain user01@xyz.com 
    Note: I use SSL.
    But I receive this error with proper syntax 
    Exception calling "Send" with "1" argument(s): "The SMTP server requires a secure connection or the client was not auth
    enticated. The server response was: 5.7.1 Client was not authenticated"
    At C:\Users\user01\Desktop\smtp.ps1:25 char:17
    + $SMTPClient.Send <<<< ($emailMessage)
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : DotNetMethodException
    Please advice where I am doing a mistake.
    Script:
    $EmailFrom = 'user01@abc.com'
    $EmailTo = 'user01@xyz.com'
    $Subject = 'TEST' 
    $Body = 'HELLO WORLD' 
    $SMTPServer = "smtp.abc.com" 
    #$filename = “c:\test\test.txt” 
    $user = 'user01@abc.com'
    $pass = 'password'
    $emailMessage = New-Object System.Net.Mail.MailMessage
    $SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer, 587) 
    $SMTPClient.EnableSSL = $true 
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)
    $emailMessage.From = $EmailFrom
    $emailMessage.To.Add($EmailTo)
    $emailMessage.Subject = $Subject
    $emailMessage.Body = $Body
    #$emailMessage.Attachments.Add("C:\test\Test.txt")
    $SMTPClient.Send($emailMessage)


    • Edited by windlover Sunday, January 15, 2012 3:31 PM
    Sunday, January 15, 2012 3:29 PM

Answers

  •  

    Add "$SMTPClient.UseDefaultCredentials = false;" immediately after the line setting the credentials, otherwise the credentials you're setting will be ignored and the SMTP client will keep using the default credentials; see here for details.


    • Edited by ObiWan Monday, January 16, 2012 5:11 PM
    • Marked as answer by Evan Liu Sunday, February 5, 2012 12:51 PM
    Monday, January 16, 2012 5:10 PM

All replies

  • See if this example helps you...I am not sure what platform your on but this is for Exchange 2007 using powershell

     

    New-SendConnector -Name Test -AddressSpaces contoso.com,microsoft.com
    $CredentialObject = Get-Credential
    New-SendConnector -Name "Secure E-Mail to Contoso.com" -AddressSpaces contoso.com -AuthenticationCredential $CredentialObject -SmartHostAuthMechanism BasicAuth

    The first example shows the command that creates a new Send connector that sends e-mail messages over the Internet. The example shows the minimum parameters that are required to create a Send connector. The address space is restricted to send messages only to the Contoso.com domain and the Microsoft.com domain.

    The second example shows the commands that create a new Send connector that sends e-mail messages to Contoso.com. The Contoso.com e-mail server requires that the local Exchange 2007 server authenticate with it to submit e-mail messages. To configure the Send connector to use authentication, you must first run the Get-Credential command and assign it to a variable. When you run the Get-Credential command, the command asks for the username and password of the account that is used during authentication with the Contoso.com e-mail server.

    In the second example, the Get-Credential cmdlet is assigned to the $CredentialObject variable. After the authentication credentials are assigned to the $CredentialObject variable, the new Send connector can be created by using the New-SendConnector cmdlet. The $CredentialObject variable is passed to the AuthenticationCredential parameter. In addition to setting the credentials, the SmartHostAuthMechanism parameter must be set to BasicAuth to instruct the Send connector to authenticate with the Contoso.com e-mail server by using the supplied credentials.

     

    The link for more information about this is here:

    http://technet.microsoft.com/en-us/library/bb629547(EXCHG.80).aspx

     

    JazzyJ72


    Jason Segale - Owner Western Tech Solutions jason@westerntechsolutions.com
    Sunday, January 15, 2012 7:31 PM
  •  

    Add "$SMTPClient.UseDefaultCredentials = false;" immediately after the line setting the credentials, otherwise the credentials you're setting will be ignored and the SMTP client will keep using the default credentials; see here for details.


    • Edited by ObiWan Monday, January 16, 2012 5:11 PM
    • Marked as answer by Evan Liu Sunday, February 5, 2012 12:51 PM
    Monday, January 16, 2012 5:10 PM