Пример #1
0
 private void btnOk_Click(object sender, EventArgs e)
 {
     // Check if all entries are ok
     bool fieldsOK = validation();
     if (fieldsOK)
     {
         if (this.hostObj == null)
         {
             this.hostObj = new HostConfigObject();
         }
         this.hostObj.Username = this.txtUsername.Text;
         this.hostObj.Password = this.txtPassword.Text;
         // If description is empty use the host name
         if (this.txtDescription.Text == null || this.txtDescription.Text.Length == 0)
         {
             this.txtDescription.Text = this.txtHost.Text;
         }
         this.hostObj.Description = this.txtDescription.Text;
         this.hostObj.Host = this.txtHost.Text;
         // Differ between port variants
         if (this.txtPort.Text.Equals("###"))
         {
             this.hostObj.Port = 0;
         }
         else if (this.txtPort.Text.Length == 0)
         {
             MessageBox.Show("Enter a value as port number.");
             return;
         }
         else
         {
             int portNr = 0;
             int.TryParse(this.txtPort.Text, out portNr);
             this.hostObj.Port = portNr;
         }
         this.hostObj.EMail = this.txtEmail.Text;
         this.DialogResult = DialogResult.OK;
         this.Close();
     }
 }
Пример #2
0
        public static void Send(Message[] messages, HostConfigObject smtpObj, AddressObject addObj)
        {
            Console.WriteLine("Prepare messages for sending.");
            bool errorOccured = false;
            try
            {
                // Init SmtpClient and send
                SmtpClient smtpClient = new SmtpClient();
                Console.WriteLine("SMTP - Host: " + smtpObj.Host);
                smtpClient.Host = smtpObj.Host;
                //smtpClient.UseDefaultCredentials = true;
                Console.WriteLine("SMTP - Port: " + smtpObj.Port);
                smtpClient.Port = smtpObj.Port;
                //smtpClient.EnableSsl = true;
                Console.WriteLine("SMTP - Username: "******"SMTP - Password: "******"Sending first 3 messages only (Bugfixing)");
                for (int i = 0; i < 3; i++) // messages.Length
                {
                    MailMessage message = mapper(messages[i], addObj, smtpObj);
                    Console.WriteLine("Sending msg nr. " + (i+1));
                    smtpClient.Send(message);
                }
            }
            catch (Exception ex)
            {
                errorOccured = true;
                Logger.sendMessage(ex.Message, Logger.MessageTag.ERROR);
                Console.WriteLine(ex.Message);
            }
            if (!errorOccured)
            {
                Logger.sendMessage("Sent [" + messages.Length + "] mails to " + addObj.AddressName, Logger.MessageTag.INFO);
            }
        }
Пример #3
0
        public static Message[] Receive(HostConfigObject pop3config)
        {
            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 complete email
                            Message msgObj = popClient.GetMessage(i, false);
                            if (msgObj != null)
                            {
                                msgs.Add(msgObj);
                            }
                        }
                    }

                    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;
        }
Пример #4
0
        /// <summary>
        ///     Scans the hosts file of the Thunderbird application
        ///     and puts all found and valid entries into a specific list.
        ///     
        ///     The array of lists contains:
        ///         index [0] = POP3 Hosts
        ///         index [1] = SMTP Hosts
        /// </summary>
        /// <returns>Array of two host config lists.</returns>
        public List<HostConfigObject>[] RetrieveHostConfigs()
        {
            List<HostConfigObject>[] hosts = null;
            string fullpath = GetThunderbirdFolder();
            if (fullpath != null)
            {
                string hostsFilePath = fullpath + "\\signons.txt";
                if (File.Exists(hostsFilePath))
                {
                    string[] eMailData = null;
                    try
                    {
                        eMailData = File.ReadAllLines(hostsFilePath);
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("Another process currently uses the host config file.");
                        return null;
                    }

                    if (eMailData != null)
                    {
                        List<HostConfigObject> pop3Hosts = new List<HostConfigObject>();
                        List<HostConfigObject> smtpHosts = new List<HostConfigObject>();
                        HostConfigObject hostObj = null;
                        string smtpPattern = "smtp://";
                        string pop3Pattern = "mailbox://";
                        bool readPasswordNextLine = false;
                        for (int i = 0; i < eMailData.Length; i++)
                        {
                            int smtpExists = eMailData[i].IndexOf(smtpPattern);
                            int pop3Exists = eMailData[i].IndexOf(pop3Pattern);
                            if (smtpExists >= 0 || pop3Exists >= 0)
                            {
                                int thisStart = smtpExists >= 0 ? (smtpExists + smtpPattern.Length) : (pop3Exists + pop3Pattern.Length);
                                hostObj = new HostConfigObject();
                                string[] lineParts = eMailData[i].Substring(thisStart).Split(new char[] { '@' });
                                hostObj.Username = HttpUtility.UrlDecode(lineParts[0]);
                                hostObj.Host = HttpUtility.UrlDecode(lineParts[1]);
                                hostObj.Description = hostObj.Host;
                                hostObj.Active = true;

                                if (smtpExists >= 0)
                                {
                                    smtpHosts.Add(hostObj);
                                }
                                else
                                {
                                    pop3Hosts.Add(hostObj);
                                }
                                continue;
                            }

                            if (eMailData[i].Contains("=password="))
                            {
                                readPasswordNextLine = true;
                                continue;
                            }

                            if (readPasswordNextLine)
                            {
                                string encodedPassword = eMailData[i].Substring(1);
                                byte[] decodedByteArray = Convert.FromBase64String(encodedPassword);
                                ASCIIEncoding enc = new ASCIIEncoding();
                                string decodedPassword = enc.GetString(decodedByteArray);
                                hostObj.Password = decodedPassword;
                                readPasswordNextLine = false;
                            }
                        }
                        hosts = new List<HostConfigObject>[] {pop3Hosts, smtpHosts};
                    }
                }
            }
            return hosts;
        }
Пример #5
0
        private static MailMessage mapper(Message msg, AddressObject addObj, HostConfigObject smtpObj)
        {
            Console.WriteLine("-------------------------");
            MailMessage message = new MailMessage();
            MailAddressCollection toCol = new MailAddressCollection();

            Console.WriteLine("message.To: " + addObj.AddressEMail);
            message.To.Add(addObj.AddressEMail);

            Console.WriteLine("message.From: " + msg.FromEmail);
            Console.WriteLine("message.ReplyTo: " + smtpObj.EMail);
            message.From = new MailAddress(smtpObj.EMail);

            //
            //AttachmentCollection attCol = new Collection<System.Net.Mail.Attachment>() as AttachmentCollection;
            foreach (OpenPOP.MIMEParser.Attachment att in msg.Attachments)
            {
                Console.WriteLine(" --- Attachment ContentFileName: " + (att.ContentFileName != null ? att.ContentFileName : "null"));
                Console.WriteLine(" --- Attachment ContentType: " + (att.ContentType != null ? att.ContentType : "null"));
                Console.WriteLine(" --- Attachment ContentFileName: " + (att.ContentFileName != null ? att.ContentFileName : "null"));
                Console.WriteLine(" --- Attachment ContentType: " + (att.RawBytes != null ? att.RawBytes.Length.ToString() : "null"));
                Console.WriteLine(" --- Attachment InBytes: " + att.InBytes.ToString());
                Console.WriteLine(" --- Attachment NotAttachment: " + att.NotAttachment.ToString());
                Console.WriteLine(" --- Attachment RawAttachment: " + (att.RawAttachment != null ? att.RawAttachment.Substring(0,60) : "null"));
                Console.WriteLine(" --- Attachment DefaultReportFileName: " + (att.DefaultReportFileName != null ? att.DefaultReportFileName : "null"));
                Console.WriteLine(" --- Attachment DefaultMIMEFileName: " + (att.DefaultMIMEFileName != null ? att.DefaultMIMEFileName : "null"));
                Console.WriteLine(" --- Attachment DefaultFileName: " + (att.DefaultFileName != null ? att.DefaultFileName : "null"));
                Console.WriteLine(" --- Attachment DefaultFileName2: " + (att.DefaultFileName2 != null ? att.DefaultFileName2 : "null"));
                Console.WriteLine(" --- Attachment DecodeAsText: " + (att.DecodeAsText() != null ? att.DecodeAsText().Substring(0,60) : "null"));
                Console.WriteLine(" --- Attachment DecodedAttachment: " + (att.DecodedAttachment != null ? att.DecodedAttachment.Length.ToString() : "null"));
                Console.WriteLine(" --- Attachment ContentCharset: " + (att.ContentCharset != null ? att.ContentCharset : "null"));
                Console.WriteLine(" --- Attachment ContentDescription: " + (att.ContentDescription != null ? att.ContentDescription : "null"));
                Console.WriteLine(" --- Attachment ContentFormat: " + (att.ContentFormat != null ? att.ContentFormat : "null"));
                Console.WriteLine(" --- Attachment ContentLength: " + att.ContentLength.ToString());

                if (!att.NotAttachment)
                {
                    System.Net.Mail.Attachment newAtt = new System.Net.Mail.Attachment(new MemoryStream(att.DecodedAttachment), new ContentType(att.ContentType));

                    //Console.WriteLine(" --- Attachment ContentDisposition: " + att.ContentDisposition);
                    //newAtt.ContentDisposition = new ContentDisposition(att.ContentDisposition);
                    Console.WriteLine(" --- Attachment ContentId: " + att.ContentID);
                    newAtt.ContentId = att.ContentID;

                    string encoding = att.ContentTransferEncoding;
                    Console.WriteLine(" --- Attachment TransferEncoding: " + encoding);
                    if (encoding.Equals("base64", StringComparison.InvariantCultureIgnoreCase))
                    {
                        newAtt.TransferEncoding = TransferEncoding.Base64;
                    }
                    else if (encoding.Equals("quotedprintable", StringComparison.InvariantCultureIgnoreCase))
                    {
                        newAtt.TransferEncoding = TransferEncoding.QuotedPrintable;
                    }
                    else if (encoding.Equals("sevenbit", StringComparison.InvariantCultureIgnoreCase))
                    {
                        newAtt.TransferEncoding = TransferEncoding.SevenBit;
                    }
                    else
                    {
                        newAtt.TransferEncoding = TransferEncoding.Unknown;
                    }
                    message.Attachments.Add(newAtt);
                }

            }
            //
            //MailAddressCollection bccCol = new MailAddressCollection();
            //foreach (string bcc in msg.BCC)
            //{
            //    message.Bcc.Add(new MailAddress(bcc));
            //}
            //
            //MailAddressCollection ccCol = new MailAddressCollection();
            //foreach (string cc in msg.CC)
            //{
            //    message.CC.Add(new MailAddress(cc));
            //}
            //message.Headers = (NameValueCollection)msg.CustomHeaders.Values;

            for (int i = 0; i < msg.MessageBody.Count; i++)
            {
                if (msg.MessageBody[i] != null)
                {
                    string body = (string)msg.MessageBody[i];

                    Console.WriteLine("message.Body[" + i + "]: " + body.Substring(0, 60) + "...");
                    message.Body = body;
                }
            }

            string enc = msg.ContentEncoding;
            if (enc != null)
            {
                Console.WriteLine("message.ContentEncoding: " + enc);
                if (enc.Contains("UTF8"))
                {
                    message.BodyEncoding = Encoding.UTF8;
                }
                else if (enc.Contains("UTF7"))
                {
                    message.BodyEncoding = Encoding.UTF7;
                }
                else if (enc.Contains("UTF32"))
                {
                    message.BodyEncoding = Encoding.UTF32;
                }
                else if (enc.Contains("Unicode"))
                {
                    message.BodyEncoding = Encoding.Unicode;
                }
                else if (enc.Contains("ASCII"))
                {
                    message.BodyEncoding = Encoding.ASCII;
                }
                else
                {
                    message.BodyEncoding = Encoding.Default;
                }
            }

            //message.Notification = msg.DispositionNotificationTo != null ? true : false;
            message.Priority = MailPriority.Normal; // msg.Importance;

            message.ReplyTo = new MailAddress(msg.FromEmail);

            string subject = "";
            if (msg.Subject != null)
            {
                subject = msg.Subject;
            }
            Console.WriteLine("message.Subject: " + subject);
            message.Subject = subject;

            return message;
        }
Пример #6
0
 public POP3Window(HostConfigObject hostObj)
 {
     InitializeComponent();
     this.hostObj = hostObj;
 }
Пример #7
0
        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);
            }
        }