public Message[] Receive(ConnectionObject con) { HostConfigObject pop3config = SettingsObject.ListPOP3[con.Pop3ID]; POPClient popClient = new POPClient(); // Set timeouts to 5 seconds popClient.ReceiveTimeOut = 5000; popClient.SendTimeOut = 5000; bool connected = false; // Port is set to automatic, try SSL first, then STANDARD if (pop3config.Port == 0) { Console.WriteLine("Automatic port is activated for POP3 host."); Console.WriteLine("Trying to connect with SSL."); connected = Connect(popClient,pop3config.Host, SSL); if (!connected) { Console.WriteLine("Connection denied."); Console.WriteLine("Trying to connect at standard port [" + STANDARD + "]."); connected = Connect(popClient, pop3config.Host, STANDARD); if (!connected) { Console.WriteLine("Connection denied."); } else { Console.WriteLine("Connection granted."); } } else { Console.WriteLine("Connection granted."); } } else { Console.WriteLine("Currently activated port: " + pop3config.Port); Console.WriteLine("Trying to connect at this port."); connected = Connect(popClient,pop3config.Host, pop3config.Port); if (!connected) { Console.WriteLine("Connection denied."); } else { Console.WriteLine("Connection granted."); } } bool errorOccured = false; // Contacting the server and login Message[] msgArray = null; if (connected){ try { Console.WriteLine("Starting authentication."); AuthenticationMethod auth = AuthenticationMethod.TRYBOTH; popClient.Authenticate(pop3config.Username, pop3config.Password, auth); Logger.sendMessage("Login successful.", Logger.MessageTag.INFO); Console.WriteLine("login successful."); int msgCount = popClient.GetMessageCount(); Logger.sendMessage("Account statistics loaded. [" + msgCount + "] messages on server.", Logger.MessageTag.INFO); Console.WriteLine("Account statistics loaded. [" + msgCount + "] messages on server."); List<Message> msgs = new List<Message>(); //System.Windows.Forms.MessageBox.Show("Fetching first 3 messages only (Bugfixing)"); // Mailbox entries always start with "1" for (int i = 1; i <= 3; i++) // msgCount { if (running) { // Receive UID of mail string uid = popClient.GetMessageUID(i); List<string> cache = SettingsObject.CacheUIDs[con.ConnectionID]; if (cache == null) { cache = new List<string>(); } // Receive complete email Message msgObj = popClient.GetMessage(i, false); if (msgObj != null) { msgs.Add(msgObj); } // Add UID to list of cached mail UIDs SettingsObject.CacheUIDs.Add(con.ConnectionID, cache); } } msgArray = (Message[])msgs.ToArray(); } catch (Exception) { errorOccured = true; Logger.sendMessage("Problem while receiving message/s.", Logger.MessageTag.ERROR); } finally { popClient.Disconnect(); } } if (!errorOccured) { Logger.sendMessage("Received " + (msgArray != null ? msgArray.Length.ToString() : "no") + " mails from " + pop3config.Description + ".", Logger.MessageTag.INFO); } return msgArray; }
public static void readConfigFile() { SettingsObject.ListConnections = new List<ConnectionObject>(); SettingsObject.ListPOP3 = new List<HostConfigObject>(); SettingsObject.ListSMTP = new List<HostConfigObject>(); SettingsObject.ListAddress = new List<AddressObject>(); try { XmlDocument doc = new XmlDocument(); doc.Load(Settings.Default.ConfigFile); XmlNode root = doc.SelectSingleNode("config"); // Read connections XmlNode connections = root.SelectSingleNode("connections"); if (connections != null) { XmlNodeList dataNodes = connections.ChildNodes; // Iterates through data nodes foreach (XmlNode connect in dataNodes) { ConnectionObject item = new ConnectionObject(); item.Pop3ID = getIntValue(connect, "pop3id"); item.SmtpID = getIntValue(connect, "smtpid"); item.AddressID = Convert.ToInt32(getValue(connect, "emailid")); item.Active = getBoolValue(connect, "active"); item.ContinousMode = getBoolValue(connect, "continousmode"); // Repetition Times for cycling XmlNode timecycle = connect.ChildNodes[0]; int hours = getIntValue(timecycle, "hours"); int minutes = getIntValue(timecycle, "minutes"); int seconds = getIntValue(timecycle, "seconds"); TimeSpan time = new TimeSpan(hours, minutes, seconds); item.WaitTime = time; SettingsObject.ListConnections.Add(item); } } // Read POP3 Hosts XmlNode pops = root.SelectSingleNode("pop3hosts"); if (pops != null) { XmlNodeList dataNodes = pops.ChildNodes; // Iterates through data nodes foreach (XmlNode pop3 in dataNodes) { HostConfigObject item = new HostConfigObject(); item.Description = getValue(pop3, "description"); item.Host = getValue(pop3, "host"); int port = 0; int.TryParse(getValue(pop3, "port"),out port); item.Port = port; item.Username = getValue(pop3, "username"); item.Password = getValue(pop3, "password"); item.Active = getBoolValue(pop3, "active"); SettingsObject.ListPOP3.Add(item); } } // Read SMTP Hosts XmlNode smtps = root.SelectSingleNode("smtphosts"); if (smtps != null) { XmlNodeList dataNodes = smtps.ChildNodes; // Iterates through data nodes foreach (XmlNode smtp in dataNodes) { HostConfigObject item = new HostConfigObject(); item.Description = getValue(smtp, "description"); item.EMail = getValue(smtp, "email"); item.Host = getValue(smtp, "host"); int port = 0; int.TryParse(getValue(smtp, "port"), out port); item.Port = port; item.Username = getValue(smtp, "username"); item.Password = getValue(smtp, "password"); item.Active = getBoolValue(smtp, "active"); SettingsObject.ListSMTP.Add(item); } } // Read mail addresses XmlNode addresses = root.SelectSingleNode("mailaddresses"); if (addresses != null) { XmlNodeList dataNodes = addresses.ChildNodes; // Iterates through data nodes foreach (XmlNode add in dataNodes) { AddressObject item = new AddressObject(); item.AddressName = getValue(add, "name"); item.AddressEMail = getValue(add, "email"); item.Active = getBoolValue(add, "active"); SettingsObject.ListAddress.Add(item); } } } catch (Exception) { Logger.sendMessage("Cannot load config!", Logger.MessageTag.ERROR); } }