0

So Ive been banging my head for days and trying as many solutions as I can find... All of which get me close, however in the end I run into the same road blocks. ( all of which I think are associated which same root issue )

I'm attempting to rewrite a process that will do a few things however the part I'm running into a problem with is Sending and Receiving email via MS Exchange. I'm using the standard SmtpClient to send and have read the exchange requires a SSL and connects to the default port 25 for Explicit SSL that starts un-encrypted then issues a STARTDLS then switches to an Encrypted connection: RFC 2228), but doesn't support Implicit SSL. so where does that leave me? Ive unsuccessful tried some of the Port relays like stunnel which is based off openssl however those don't seem to work either ( though I'm certain not getting past the proxy int that case...

So.. what Ive done thus far: When I'm NOT on the network ( ie home ) I can send mail via my Gmail with no problem. However when on the network (in office or VPN) I'm no longer able to send mail and get am error of : An attempt was made to access a socket in a way forbidden by its access permissions xxx.xxx.xxx.xxx:587(Ive tried other ports all of which produce the same error or a time out error)

When I attempt to send to our MS Exchange server (obviously while on the network) I get the following error: No connection could be made because the target machine actively refused it xxx.xxx.xxx.xxx:587 (Ive tried other ports all of which produce the same error or a time out error)

Couple things bout my environment (There is a internet proxy present that went connected to the VPN or on the network all traffic goes 8080 and 80 through ( I'm sure there's more ports too ) With that I haven't found a way to connect and authenticate with the proxy and use its connection state to send or receive mail) My current mail client (Outlook) worked just fine over the VPN or on Network and in looking at the account config its set up to use the proxy as seen here:

Proxy enabled for HTTP

Proxy Settings

Here is what I have so far for testing:

Any suggestion would be amazing!

    static void Main(string[] args)
    {

        string server = "smtp.gmail.com";       //-- > or exchange server "mxserver@mydomain.com"
        int port = 25;                          // --> tried 25, 443, 587 110...ect
        string mailbox = "user@gmail.com";
        string password = "mypassword";
        string recipient = "user@gmail.com";


        try
        {
            Console.WriteLine("Step 1: Attempting to Connect to server: {0} over port: {1}", server, port);
            using (var client = new TcpClient(server, port))
            {
                using (var stream = client.GetStream())
                using (var clearTextReader = new StreamReader(stream))
                using (var clearTextWriter = new StreamWriter(stream) { AutoFlush = true })
                using (var sslStream = new SslStream(stream))
                {
                    var helloResponse = clearTextReader.ReadLine();
                }

            }


        }
        catch (Exception ex)
        {

            Console.WriteLine(ex.Message);
            Console.WriteLine("The Process has ended... Press any key to exit.");
            Console.ReadLine();
            return;
        }
        Console.WriteLine("Result: Success! Connected to server:{0}.", server);

        try
        {
            Console.WriteLine("Step 2: Attempting Send Mail...", server);
            using (SmtpClient smtpClient = new SmtpClient(server, port))
            {
                NetworkCredential creds = new NetworkCredential(mailbox, password);
                smtpClient.EnableSsl = true;
                ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
                smtpClient.Credentials = creds;
                MailMessage msg = new MailMessage(mailbox, recipient, "Test", "This is a test");
                smtpClient.Send(msg);
                Console.WriteLine("Result: Success! Mail sent...");

            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        Console.WriteLine("The Process has ended... Press any key to exit.");
        Console.ReadLine();
    }

Note: This code works for sending mail through gmail ( off the network ie VPN or office network) however does NOT when on the VPN or in the office) which tells me its a issue with the process not able to get passed the network proxy which if you look at the exchange account settings is required in order to send or received mail. Maybe Im missing something here but even when implementing the exact code others say works for them unfortunately doesn't for me.

Update:

Ran a trace on the packet data via wireshark and found this.

307 38.853709000    xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx HTTP    354 HTTP/1.1 407 Proxy Authentication Required ( Forefront TMG requires authorization to fulfill the request. Access to the Web Proxy filter is denied.  )  (text/html)

308 38.937293000    xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx HTTP    637 HTTP/1.1 407 Proxy Authentication Required ( Access is denied.  ) , NTLMSSP_CHALLENGE

So with that; the returned error conducive to the not being able to connect through the proxy ... and now faced with how do i programmatically do so?

Eric
  • 9
  • 3
  • why are you making this process so difficult have you looked at Stackoverflow for other post's in regards to using `SSL smtp` to send emails.. – MethodMan Oct 08 '15 at 00:40
  • HI @MethodMan, thank you for replying so quickly, I wish it was that easy and I was hoping I was just missing something. You're right the process isn't all that difficult and Ive looked at the post your reference and unfortunately ( aside form doing the TcpListener check which I just put in place as a test to make sure that I can connect to the server it was intended to be there is was attempting to figure where in the TCP stack it was failing. As far as the other post referencing the SSL smtp is seem others have a similar issue when dealing with exchange every time referencing the EnableSSL, – Eric Oct 08 '15 at 03:52
  • ...( which if I understand only establishes the secure connection after the initial connection is authenticated) option which doesn't help if the Proxy doesn't let the none secure request through regardless of the port. I don't think its the code rather the inability to establish a connection to the server outside of ICMP.. Im still assuming that this is a proxy issue and I was hoping someone had found a way to either establish a through a proxy IE client --> proxy authentication --> exchange or knows what else might be going on. Also I eventually want to make a call to receive mail as well, – Eric Oct 08 '15 at 03:52

0 Answers0