Ejemplo n.º 1
0
        static void ParseMultiMix(string s, EmaillMessage m)
        {
            multiHandled = true;
            string[] postParse = null;
            string   boundary  = null;

            foreach (string headerCheck in SplitToLines(ReturnHeader(s)))
            {
                if (headerCheck.StartsWith("\tboundary=\""))
                {
                    boundary  = "--" + headerCheck.Substring("boundary=".Length + 2, headerCheck.Length - "boundary=\"".Length - 2);
                    postParse = s.Split(new[] { boundary }, StringSplitOptions.None);
                    Console.WriteLine();
                }
            }
            try
            {
                m.Body = m.Body.Replace(boundary, "");
                for (int x = 1; x < postParse.Length; x++)
                {
                    AssignMime("", m, postParse[x]);
                }
            }
            catch { }
        }
Ejemplo n.º 2
0
        private static void AssignMime(string s, EmaillMessage m, string optionalMimeSubPart)        //not using newer optional param support because delegates
        {
            mimeHandled = true;
            Dictionary <string, Delegate> d = new Dictionary <string, Delegate>();

            Del plainTextHandler = ParsePlainText;
            Del htmlHandler      = ParseHtmlText;
            Del multiAltHandler  = ParseMultiMix;
            Del multiMixHandler  = ParseMultiMix;

            d.Add("multipart/alternative;", multiAltHandler);
            d.Add("multipart/mixed;", multiMixHandler);
            d.Add("text/plain;", plainTextHandler);
            d.Add("text/html;", htmlHandler);


            string[] keywords = new string[] { "Content-Type: ", "\tcharset=" };
            foreach (string headerCheck in SplitToLines(ReturnHeader(optionalMimeSubPart)))
            {
                foreach (string keyword in keywords)
                {
                    if (headerCheck.StartsWith(keyword))
                    {
                        Console.WriteLine(keyword + " noted in " + headerCheck);
                        foreach (KeyValuePair <string, Delegate> k in d)
                        {
                            if (headerCheck.Substring(keyword.Length) == k.Key)
                            {
                                k.Value.DynamicInvoke(optionalMimeSubPart, m);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        static string DecodeTransferEncoding(string s, EmaillMessage m)
        {
            Dictionary <string, Delegate> d = new Dictionary <string, Delegate>();
            Del2 quotedPrintableHandler     = DecodeQuotedPrintable;
            Del2 base64Handler = DecodeBase64;

            d.Add("base64", base64Handler);
            d.Add("quoted-printable", quotedPrintableHandler);

            string[] keywords = new string[] { "Content-Type: ", "\tcharset=", "Content-Transfer-Encoding: " };

            foreach (string headerCheck in SplitToLines(ReturnHeader(s)))
            {
                foreach (string keyword in keywords)
                {
                    if (headerCheck.StartsWith(keyword))
                    {
                        foreach (KeyValuePair <string, Delegate> k in d)
                        {
                            if (headerCheck.Substring(keyword.Length) == k.Key)
                            {
                                return((string)k.Value.DynamicInvoke(s, m));
                            }
                        }
                    }
                }
            }
            return(s);
        }
Ejemplo n.º 4
0
        public static void ParseHtmlText(string s, EmaillMessage m)
        {
            string originalS = s;

            s = DecodeTransferEncoding(s, m);

            s      = RemoveHeader(s);
            m.Body = m.Body.Replace(originalS, s);
        }
Ejemplo n.º 5
0
        public static void ParsePlainText(string s, EmaillMessage m)
        {
            string originalS = s;

            s      = DecodeTransferEncoding(s, m);
            s      = RemoveHeader(s);
            s      = s.Replace(Environment.NewLine, "<br>");
            s      = s.Replace("\r\n", "<br>");
            s      = s.Replace("\r", "<br>");
            s      = s.Replace("\n", "<br>");
            m.Body = m.Body.Replace(originalS, s);
        }
Ejemplo n.º 6
0
 public static string DecodeBase64(string input, EmaillMessage message)
 {
     try
     {
         string originalS = input;
         input = message.Encoding.GetString(Convert.FromBase64String(input));
         return(input);
     }
     catch
     {
         return(input);
     }
 }
Ejemplo n.º 7
0
 public EmaillMessage[] GetMail()
 {
     lock (this)
     {
         try
         {
             bool b;
             Connect(Host, Port);
             //Connect("localhost", 110);
             b = POPReceive();
             Send("USER " + User + "\n");
             b = POPReceive();
             Send("PASS " + Password + "\n");
             b = POPReceive();
             Send("STAT\n");
             int             mLength = Int32.Parse("" + Receive()[4]);
             EmaillMessage[] m       = new EmaillMessage[mLength];
             for (int x = mLength; x > 0; x--)
             {
                 Send("RETR " + x + "\n");
                 b = POPReceive();                         //known possible bug?
                 bool   loopTester  = true;
                 string receivedMsg = "";
                 do
                 {
                     string currentReception = Receive();
                     receivedMsg = receivedMsg + currentReception;
                     string[] s = Parser.SplitToLines(currentReception);
                     foreach (string S in s)
                     {
                         if (S == ".")
                         {
                             loopTester = false;
                         }
                         else
                         {
                         }
                     }
                 }while (loopTester);
                 m[x - 1] = Parser.Parse(receivedMsg);
             }
             Send("QUIT\n");
             return(m);
         }
         catch
         {
             return(null);
         }
     }
 }
Ejemplo n.º 8
0
        public static EmaillMessage Parse(string s)
        {
            Del subjectHandler   = AssignSubject;
            Del senderHandler    = AssignSender;
            Del recipientHandler = AssignRecipent;
            Del dateHandler      = AssignDate;
            Del mimeHandler      = AssignMime;
            Del encodingHandler  = AssignEncoding;

            Dictionary <string, Delegate> d = new Dictionary <string, Delegate>();

            d.Add("Date: ", dateHandler);
            d.Add("Mime-Version: ", mimeHandler);            //most parsing occurs therein
            d.Add("Subject: ", subjectHandler);
            d.Add("From: ", senderHandler);
            d.Add("To: ", recipientHandler);
            d.Add("charset=", encodingHandler);

            mimeHandled  = false;
            multiHandled = false;
            EmaillMessage m = new EmaillMessage("", s, "", "");

            foreach (string headerCheck in SplitToLines(ReturnHeader(s)))
            {
                foreach (KeyValuePair <string, Delegate> k in d)
                {
                    if (headerCheck.StartsWith(k.Key))
                    {
                        k.Value.DynamicInvoke(headerCheck.Substring(k.Key.Length), m);
                    }
                }
            }
            if (!mimeHandled)
            {
                ParsePlainText(m.Body, m);
            }
            if (multiHandled)
            {
                m.Body = RemoveHeader(m.Body);
            }
            Console.WriteLine("returning m");
            return(m);
        }
Ejemplo n.º 9
0
        public void SendMail(EmaillMessage m)
        {
            lock (this)
            {
                try
                {
                    Connect(Host, Port);
                    string result = Receive();
                    Send($"HELO {Host}\n");
                    result = Receive();
                    Send("AUTH LOGIN\n");
                    result = Receive();
                    Send(Convert.ToBase64String(Encoding.UTF8.GetBytes(User)) + "\n");
                    result = Receive();
                    Send(Convert.ToBase64String(Encoding.UTF8.GetBytes(Password)) + "\n");
                    result = Receive();
                    Send($"MAIL FROM: <{(string.IsNullOrEmpty(m.Sender) ? User : m.Sender)}>\n");
                    result = Receive();
                    Send($"RCPT TO: <{m.Recipient}>\n");
                    result = Receive();
                    Send("DATA\n");
                    result = Receive();
                    Send(String.Format(@"From: {0}
To: {1}
Date: {2}
Subject: {3}
Content-Type: {4};

{5}
.
", (string.IsNullOrEmpty(m.Sender) ? User : m.Sender), m.Recipient, m.Timestamp, m.Subject, m.IsHtml ? "text/html" : "text/plain", m.Body));
                    result = Receive();
                    Send("QUIT\n");

                    result = Receive();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
        }
Ejemplo n.º 10
0
        public static string DecodeQuotedPrintable(string input, EmaillMessage message)
        {
            Console.WriteLine("decoding QP");
            string originalS  = input;
            var    occurences = new Regex(@"(=[0-9A-Z][0-9A-Z])+", RegexOptions.Multiline);
            var    matches    = occurences.Matches(input);

            foreach (Match m in matches)
            {
                byte[] bytes = new byte[m.Value.Length / 3];
                for (int i = 0; i < bytes.Length; i++)
                {
                    string hex  = m.Value.Substring(i * 3 + 1, 2);
                    int    iHex = Convert.ToInt32(hex, 16);
                    bytes[i] = Convert.ToByte(iHex);
                }
                input = input.Replace(m.Value, message.Encoding.GetString(bytes));
            }
            input = input.Replace("=\r\n", "\r\n");
            input = input.Replace("=\n", "\n");
            input = input.Replace("=\r", "\r");
            return(input);
        }
Ejemplo n.º 11
0
 public static EmaillMessage UnDotstuff(EmaillMessage m)
 {
     return m;
 }
Ejemplo n.º 12
0
 public static void ParsePlainText(string s, EmaillMessage m)
 {
     string originalS = s;
     s = DecodeTransferEncoding(s, m);
     s = RemoveHeader(s);
     s = s.Replace(Environment.NewLine, "<br>");
     s = s.Replace("\r\n", "<br>");
     s = s.Replace("\r", "<br>");
     s = s.Replace("\n", "<br>");
     m.Body = m.Body.Replace(originalS, s);
 }
Ejemplo n.º 13
0
        public static void ParseHtmlText(string s, EmaillMessage m)
        {
            string originalS = s;
            s = DecodeTransferEncoding(s, m);

            s = RemoveHeader(s);
            m.Body = m.Body.Replace(originalS, s);
        }
Ejemplo n.º 14
0
 private static void AssignSender(string s, EmaillMessage m)
 {
     m.Sender = s;
 }
Ejemplo n.º 15
0
 private static void AssignSubject(string s, EmaillMessage m)
 {
     m.Subject = s;
 }
Ejemplo n.º 16
0
 private static void AssignRecipent(string s, EmaillMessage m)
 {
     m.Recipient = s;
 }
Ejemplo n.º 17
0
        //not using newer optional param support because delegates
        private static void AssignMime(string s, EmaillMessage m, string optionalMimeSubPart)
        {
            mimeHandled = true;
            Dictionary<string, Delegate> d = new Dictionary<string, Delegate>();

            Del plainTextHandler = ParsePlainText;
            Del htmlHandler = ParseHtmlText;
            Del multiAltHandler = ParseMultiMix;
            Del multiMixHandler = ParseMultiMix;

            d.Add("multipart/alternative;", multiAltHandler);
            d.Add("multipart/mixed;", multiMixHandler);
            d.Add("text/plain;", plainTextHandler);
            d.Add("text/html;", htmlHandler);

            string[] keywords = new string[] { "Content-Type: ", "\tcharset=" };
            foreach (string headerCheck in SplitToLines(ReturnHeader(optionalMimeSubPart)))
            {
                foreach (string keyword in keywords)
                {
                    if (headerCheck.StartsWith(keyword))
                    {
                        Console.WriteLine(keyword + " noted in " + headerCheck);
                        foreach (KeyValuePair<string, Delegate> k in d)
                        {
                            if (headerCheck.Substring(keyword.Length) == k.Key)
                            {
                                k.Value.DynamicInvoke(optionalMimeSubPart, m);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 18
0
 static void ParseMultiMix(string s, EmaillMessage m)
 {
     multiHandled = true;
     string[] postParse = null;
     string boundary = null;
     foreach (string headerCheck in SplitToLines(ReturnHeader(s)))
     {
         if (headerCheck.StartsWith("\tboundary=\""))
         {
             boundary = "--" + headerCheck.Substring("boundary=".Length + 2, headerCheck.Length - "boundary=\"".Length - 2);
             postParse = s.Split(new[] { boundary }, StringSplitOptions.None);
             Console.WriteLine();
         }
     }
     try
     {
         m.Body = m.Body.Replace(boundary, "");
         for (int x = 1; x < postParse.Length; x++)
         {
             AssignMime("", m, postParse[x]);
         }
     }
     catch { }
 }
Ejemplo n.º 19
0
 public static EmaillMessage UnDotstuff(EmaillMessage m)
 {
     return(m);
 }
Ejemplo n.º 20
0
 private static void AssignEncoding(string s, EmaillMessage m)
 {
     m.Encoding = Encoding.GetEncoding(s);
 }
Ejemplo n.º 21
0
 private static void AssignDate(string s, EmaillMessage m)
 {
     m.Timestamp = s;
 }
Ejemplo n.º 22
0
 private static void AssignMime(string s, EmaillMessage m)
 {
     AssignMime(s, m, m.Body);
 }
Ejemplo n.º 23
0
 private static void AssignRecipent(string s, EmaillMessage m)
 {
     m.Recipient = s;
 }
Ejemplo n.º 24
0
 private static void AssignDate(string s, EmaillMessage m)
 {
     m.Timestamp = s;
 }
Ejemplo n.º 25
0
 private static void AssignEncoding(string s, EmaillMessage m)
 {
     m.Encoding = Encoding.GetEncoding(s);
 }
Ejemplo n.º 26
0
 public static string DecodeBase64(string input, EmaillMessage message)
 {
     try
     {
         string originalS = input;
         input = message.Encoding.GetString(Convert.FromBase64String(input));
         return input;
     }
     catch
     {
         return input;
     }
 }
Ejemplo n.º 27
0
 private static void AssignMime(string s, EmaillMessage m)
 {
     AssignMime(s, m, m.Body);
 }
Ejemplo n.º 28
0
 public static string DecodeQuotedPrintable(string input, EmaillMessage message)
 {
     Console.WriteLine("decoding QP");
     string originalS = input;
     var occurences = new Regex(@"(=[0-9A-Z][0-9A-Z])+", RegexOptions.Multiline);
     var matches = occurences.Matches(input);
     foreach (Match m in matches)
     {
         byte[] bytes = new byte[m.Value.Length / 3];
         for (int i = 0; i < bytes.Length; i++)
         {
             string hex = m.Value.Substring(i * 3 + 1, 2);
             int iHex = Convert.ToInt32(hex, 16);
             bytes[i] = Convert.ToByte(iHex);
         }
         input = input.Replace(m.Value, message.Encoding.GetString(bytes));
     }
     input = input.Replace("=\r\n", "\r\n");
     input = input.Replace("=\n", "\n");
     input = input.Replace("=\r", "\r");
     return input;
 }
Ejemplo n.º 29
0
 private static void AssignSender(string s, EmaillMessage m)
 {
     m.Sender = s;
 }
Ejemplo n.º 30
0
        public static EmaillMessage Parse(string s)
        {
            Del subjectHandler = AssignSubject;
            Del senderHandler = AssignSender;
            Del recipientHandler = AssignRecipent;
            Del dateHandler = AssignDate;
            Del mimeHandler = AssignMime;
            Del encodingHandler = AssignEncoding;

            Dictionary<string, Delegate> d = new Dictionary<string, Delegate>();
            d.Add("Date: ", dateHandler);
            d.Add("Mime-Version: ", mimeHandler);//most parsing occurs therein
            d.Add("Subject: ", subjectHandler);
            d.Add("From: ", senderHandler);
            d.Add("To: ", recipientHandler);
            d.Add("charset=", encodingHandler);

            mimeHandled = false;
            multiHandled = false;
            EmaillMessage m = new EmaillMessage("", s, "", "");

            foreach (string headerCheck in SplitToLines(ReturnHeader(s)))
            {
                foreach (KeyValuePair<string, Delegate> k in d)
                {
                    if (headerCheck.StartsWith(k.Key))
                    {
                        k.Value.DynamicInvoke(headerCheck.Substring(k.Key.Length), m);
                    }
                }
            }
            if (!mimeHandled) { ParsePlainText(m.Body, m); }
            if (multiHandled) { m.Body = RemoveHeader(m.Body); }
            Console.WriteLine("returning m");
            return m;
        }
Ejemplo n.º 31
0
        static string DecodeTransferEncoding(string s, EmaillMessage m)
        {
            Dictionary<string, Delegate> d = new Dictionary<string, Delegate>();
            Del2 quotedPrintableHandler = DecodeQuotedPrintable;
            Del2 base64Handler = DecodeBase64;
            d.Add("base64", base64Handler);
            d.Add("quoted-printable", quotedPrintableHandler);

            string[] keywords = new string[] { "Content-Type: ", "\tcharset=", "Content-Transfer-Encoding: " };

            foreach (string headerCheck in SplitToLines(ReturnHeader(s)))
            {
                foreach (string keyword in keywords)
                {
                    if (headerCheck.StartsWith(keyword))
                    {
                        foreach (KeyValuePair<string, Delegate> k in d)
                        {
                            if (headerCheck.Substring(keyword.Length) == k.Key)
                            {
                                return (string)k.Value.DynamicInvoke(s, m);
                            }
                        }
                    }
                }
            }
            return s;
        }
Ejemplo n.º 32
0
 private static void AssignSubject(string s, EmaillMessage m)
 {
     m.Subject = s;
 }
Ejemplo n.º 33
0
        public EmaillMessage[] GetMail()
        {
            lock (this)
            {
                try
                {
                    bool b;
                    Connect(Host, Port);
                    //Connect("localhost", 110);
                    b = POPReceive();
                    Send("USER " + User + "\n");
                    b = POPReceive();
                    Send("PASS " + Password + "\n");
                    b = POPReceive();
                    Send("STAT\n");
                    int mLength = Int32.Parse("" + Receive()[4]);
                    EmaillMessage[] m = new EmaillMessage[mLength];
                    for (int x = mLength; x > 0; x--)
                    {
                        Send("RETR " + x + "\n");
                        b = POPReceive(); //known possible bug?
                        bool loopTester = true;
                        string receivedMsg = "";
                        do
                        {
                            string currentReception = Receive();
                            receivedMsg = receivedMsg + currentReception;
                            string[] s = Parser.SplitToLines(currentReception);
                            foreach (string S in s)
                            {
                                if (S == ".")
                                {
                                    loopTester = false;
                                }
                                else
                                {

                                }
                            }
                        }
                        while (loopTester);
                        m[x - 1] = Parser.Parse(receivedMsg);
                    }
                    Send("QUIT\n");
                    return m;
                }
                catch
                {
                    return null;
                }
            }
        }
Ejemplo n.º 34
0
        public void SendMail(EmaillMessage m)
        {
            lock (this)
            {
                try
                {
                    Connect(Host, Port);
                    string result = Receive();
                    Send($"HELO {Host}\n");
                    result = Receive();
                    Send("AUTH LOGIN\n");
                    result = Receive();
                    Send(Convert.ToBase64String(Encoding.UTF8.GetBytes(User)) + "\n");
                    result = Receive();
                    Send(Convert.ToBase64String(Encoding.UTF8.GetBytes(Password)) + "\n");
                    result = Receive();
                    Send($"MAIL FROM: <{(string.IsNullOrEmpty(m.Sender) ? User : m.Sender)}>\n");
                    result = Receive();
                    Send($"RCPT TO: <{m.Recipient}>\n");
                    result = Receive();
                    Send("DATA\n");
                    result = Receive();
                    Send(String.Format(@"From: {0}
            To: {1}
            Date: {2}
            Subject: {3}
            Content-Type: {4};

            {5}
            .
            ", (string.IsNullOrEmpty(m.Sender) ? User : m.Sender), m.Recipient, m.Timestamp, m.Subject, m.IsHtml ? "text/html" : "text/plain", m.Body));
                    result = Receive();
                    Send("QUIT\n");

                    result = Receive();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
        }