Send mail using proxy

Feb 12, 2015 at 10:23 AM
Edited Feb 13, 2015 at 5:03 AM
Hi,

First of all, great library.

I am trying to send email using HigLabo library using proxy. How can I configure that?
I need to set something like:
            client.Proxy = WebRequest.DefaultWebProxy;
            client.Credentials = System.Net.CredentialCache.DefaultCredentials; ;
            client.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
It needs to use the default settings setted at Windows level or as in Internet Explorer.

Thx. in advance.

PS> We have one client using Proxy and SSL for email connection.
We tried using Sokets, but it throws error: "The handshake failed due to an unexpected packet format."

This is the code we tried using sokets:
IWebProxy proxySett = WebRequest.GetSystemWebProxy();
                Uri proxyUri = proxySett.GetProxy(new Uri(smtpServer.StartsWith("http://") ? smtpServer : "http://" + smtpServer));
                string ProxyHost = proxyUri.Host;
                int ProxyPort = proxyUri.Port;               

                if (ProxyHost != "")
                {
                    try
                    {
                        Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                        socket.Connect(ProxyHost, ProxyPort);
                        if (socket.Connected)
                        {
                            client.Connect(socket);//This is where it throws error                       
                        }
                    }
                    catch (Exception e)
                    {

                        throw; 
                    }
                }
                else
                {
                    client.Connect();
                }
And this is the full method we are using to send emails:
     public static SendMailResult SendEmailMessage(string fromEmail, string toEmail, string cc, string bcc, string subject, string body, string htmlBody, IEnumerable<string> attachments, string userName, string smtpServer, int smtpPort, string password, bool ssl, bool tls, bool spa)
        {
            if (ssl && tls)
                throw new ArgumentException("ssl and tsl can not be true at the same time. Set ssl = true or tls = true or false to both");

            if (string.IsNullOrWhiteSpace(fromEmail))
                throw new ArgumentException("fromEmail cannot be null or empty", "fromEmail");

            if (string.IsNullOrWhiteSpace(toEmail))
                throw new ArgumentException("toEmail cannot be null or empty", "toEmail");

            if (string.IsNullOrWhiteSpace(subject) && string.IsNullOrWhiteSpace(body) && string.IsNullOrWhiteSpace(htmlBody))
                throw new ArgumentException("subject, body, htmlBody is empty");

            System.Net.ServicePointManager.Expect100Continue = false;
            System.Net.ServicePointManager.DefaultConnectionLimit = 1000;

            using (HigLabo.Net.Smtp.SmtpClient client = new HigLabo.Net.Smtp.SmtpClient())
            {
                client.ServerName = smtpServer;
                client.Port = smtpPort;
                client.UserName = userName;
                client.Password = password;

                if (tls)
                {
                    client.EncryptedCommunication = SmtpEncryptedCommunication.Tls;
                }
                else if (ssl)
                {
                    client.EncryptedCommunication = SmtpEncryptedCommunication.Ssl;
                }
                else
                {
                    client.EncryptedCommunication = SmtpEncryptedCommunication.None;
                }

                client.AuthenticateMode = spa ? SmtpAuthenticateMode.Login : SmtpAuthenticateMode.Auto;


                IWebProxy proxySett = WebRequest.GetSystemWebProxy();
                Uri proxyUri = proxySett.GetProxy(new Uri(smtpServer.StartsWith("http://") ? smtpServer : "http://" + smtpServer));
                string ProxyHost = proxyUri.Host;
                int ProxyPort = proxyUri.Port;               

                if (ProxyHost != "")
                {
                    try
                    {
                        Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                        socket.Connect(ProxyHost, ProxyPort);
                        if (socket.Connected)
                        {
                            client.Connect(socket);//This is where it throws error                       
                        }
                    }
                    catch (Exception e)
                    {

                        throw; 
                    }
                }
                else
                {
                    client.Connect();
                }
                
                //fromEmail = String.Format("{0} <{1}>", Context.ComapanieNume, fromEmail);
                //message.Organization = Context.ComapanieNume;
                var msg = new SmtpMessage(fromEmail, toEmail, cc ?? "", subject ?? "", body ?? "");
                if (!string.IsNullOrWhiteSpace(bcc))
                {
                    msg.Bcc.Add(new HigLabo.Mime.MailAddress(bcc));
                }

                htmlBody = AnalyzeAndAlterMessageForEmbeddedImages(htmlBody);
                if (string.IsNullOrEmpty(body) && !string.IsNullOrEmpty(htmlBody))
                {
                    msg.SetBodyAsHtmlContent(htmlBody);
                }
                else if (!string.IsNullOrEmpty(body) && !string.IsNullOrEmpty(htmlBody))
                {
                    msg.SetBodyAsAlternativeContent(body, htmlBody);
                }

                if (attachments != null)
                {
                    foreach (var attachment in attachments)
                    {
                        var attach = new SmtpContent();
                        attach.LoadFileData(attachment);
                        attach.ContentType = new ContentType("application/octet-stream");
                        msg.Contents.Add(attach);
                    }
                }

                if (_cidAttachedImages != null)
                {
                    foreach (var kvp in _cidAttachedImages)
                    {
                        SmtpContent ct = new SmtpContent();
                        ct.FileName = kvp.Key;
                        ct.Headers.Add(new SmtpMailHeader("Content-ID", kvp.Key));
                        //ct.ContentDisposition.Parameters.Add(new SmtpMailHeaderParameter("Content-ID", kvp.Key));
                        //ct.Name = kvp.Key;
                        ct.LoadFileData(kvp.Value);
                        ct.ContentTransferEncoding = HigLabo.Mime.TransferEncoding.Base64;
                        ct.ContentType = new ContentType("application/octet-stream");
                        ct.ContentType.Name = kvp.Key;
                        msg.Contents.Add(ct);
                    }
                }


                var res = client.SendMail(msg);

                // remove attached images using cid
                if (_cidAttachedImages != null)
                {
                    foreach (var kvp in _cidAttachedImages)
                    {
                        if (File.Exists(kvp.Value)) File.Delete(kvp.Value);
                    }
                }

                return res;
            }
        }
The error is thrown at client.Connect(socket);
I think maybe because the connection should be SSL and the socket is not, but I do not see any other way of using Higlabo smtp libray with proxy besides using soket.

Is there another way?
Coordinator
Feb 13, 2015 at 8:44 AM
Hi.

I don't do any test to proxy server.
It is not supported.

I want to add proxy feature support.
But I don't know how to implement this feature.
Is there any library or document what has a feature of proxy server?
I'll check it and implement with it.

regards.
Feb 18, 2015 at 4:30 AM
Hi.

It actually was not a proxy problem. It seems it was a port block and now that is resolved, but there is another problem now.
I have to authenticate to a MS Exchange server using user domain credentials and I get a Authentication error.
Do you have any ideas on that?