public void TestGetMessageAsBytes()
		{
			const string welcomeMessage = "+OK";
			const string okUsername = "******";
			const string okPassword = "******";
			const string okMessageFetch = "+OK";
			const string messageHeaders = "Return-Path: <*****@*****.**>";
			const string messageHeaderToBodyDelimiter = "";
			const string messageBody = "\r\nTest\r\n";
			const string messageEnd = ".";

			const string message = messageHeaders + "\r\n" + messageHeaderToBodyDelimiter + "\r\n" + messageBody;

			const string serverResponses = welcomeMessage + "\r\n" + okUsername + "\r\n" + okPassword + "\r\n" + okMessageFetch + "\r\n" + message + "\r\n" + messageEnd + "\r\n";

			Stream inputStream = new MemoryStream(Encoding.ASCII.GetBytes(serverResponses));
			Stream outputStream = new MemoryStream();

			byte[] expectedBytes = Encoding.ASCII.GetBytes(message);

			Pop3Client client = new Pop3Client();
			client.Connect(new CombinedStream(inputStream, outputStream));
			client.Authenticate("something", "else");

			byte[] messageBytes = client.GetMessageAsBytes(132);

			Assert.AreEqual(expectedBytes, messageBytes);
		}
		public void TestGetMessageAsBytesWithDotDot()
		{
			const string welcomeMessage = "+OK";
			const string okUsername = "******";
			const string okPassword = "******";
			const string okMessageFetch = "+OK";
			const string messageEnd = ".";

			const string message		 = "Return-Path: <*****@*****.**>" + "\r\n" + "" + "\r\n" + "..";
			const string expectedMessage = "Return-Path: <*****@*****.**>" + "\r\n" + "" + "\r\n" + ".";
			
			const string serverResponses = welcomeMessage + "\r\n" + okUsername + "\r\n" + okPassword + "\r\n" + okMessageFetch + "\r\n" + message + "\r\n" + messageEnd + "\r\n";

			Stream inputStream = new MemoryStream(Encoding.ASCII.GetBytes(serverResponses));
			Stream outputStream = new MemoryStream();

			// When a .. is expected as the first character of a line, it really means that it is only a .
			byte[] expectedBytes = Encoding.ASCII.GetBytes(expectedMessage);

			Pop3Client client = new Pop3Client();
			client.Connect(new CombinedStream(inputStream, outputStream));
			client.Authenticate("something", "else");

			byte[] messageBytes = client.GetMessageAsBytes(139);

			Assert.AreEqual(expectedBytes, messageBytes);
		}
Exemple #3
0
        private void GetMails(MailboxInfo info)
        {
            try
            {
                using (var client = new Pop3Client())
                {
                    //Get Zabbix metrics
                    var stopwatch = new Stopwatch();
                    stopwatch.Start();
                    //Get mail count
                    client.Connect(info.Hostname, info.Port, false);
                    client.Authenticate(info.User, info.Password);
                    stopwatch.Stop();

                    //Send it to Zabbix
                    Functions.Zabbix.SendData(new ZabbixItem { Host = _config.HostKey, Key = info.Type + _config.TimingKey, Value = stopwatch.ElapsedMilliseconds.ToString() });
                    Functions.Log.Debug("Send [{0}] timing to Zabbix: connected to '{1}' as '{2}', timing {3}ms", info.Type, info.Hostname, info.User, stopwatch.ElapsedMilliseconds);

                    var count = client.GetMessageCount();
                    if (count == 0)
                        return;

                    Functions.Log.Debug("We've got new {0} messages in '{1}'", count, info.User);
                    //Send messages to sorting block
                    for (var i = 0; i < count; i++)
                    {
                        try
                        {
                            var mailInfo = new MessageInfo
                            {
                                IsSpam = false,
                                Mail = client.GetMessage(i + 1),
                                Type = MessageType.UNKNOWN,
                                Subtype = null,
                                Recipient = null,
                                Mailbox = info
                            };
                            Functions.Log.Debug("Download message from '{0}'. Size: {1}b", info.User, mailInfo.Mail.RawMessage.Length);
                            while (!_sortMailDataBlock.Post(mailInfo))
                                Thread.Sleep(500);

                            //Save every mail to archive
                            Functions.Log.Debug("Archive message");
                            Functions.Archive.Info(Functions.MessageToString(mailInfo.Mail));
                        }
                        catch (Exception ex)
                        {
                            Functions.Log.Error("Parse email error: {0}", ex.Message);
                            Functions.ErrorsCounters[info.Type].Increment();

                            //Archive mail anyway
                            Functions.Log.Debug("Archive message");
                            Functions.Archive.Info(Encoding.Default.GetString(client.GetMessageAsBytes(i + 1)));
                        }

                        if (_config.DeleteMail)
                            client.DeleteMessage(i + 1);

                        if (_stopPipeline)
                            break;
                    }
                    Functions.Log.Debug("Done with '{0}'", info.User);
                }
            }
            catch (Exception ex)
            {
                Functions.Log.Error("General error - type: {0}, message: {1}", ex, ex.Message);
                Functions.ErrorsCounters[info.Type].Increment();
            }
        }