Beispiel #1
0
        public Boolean MsgValid(MsgSummary MsgtoValid, MsgAtt Attachment, String SenderEmail, String Subject, String FileType)
        {
            String strValid;

            strValid = "";
            Boolean bValid      = false;
            bool    fromAddress = false;
            bool    subject     = false;

            // Cannot have both Subject AND email empty. However can have empty email address.
            if (string.IsNullOrEmpty(SenderEmail) && !string.IsNullOrEmpty(Subject))
            {
                fromAddress = true;
            }
            else
            // If email address contains Sender OR it matches completely
            if (MsgtoValid.MsgFrom.ToLower().Contains(SenderEmail.ToLower()) || MsgtoValid.MsgFrom.ToLower() == SenderEmail.ToLower())
            {
                fromAddress = true;
            }

            // If the Email address is valid and Subject is blank
            if (string.IsNullOrEmpty(Subject) && fromAddress)
            {
                subject = true;
            }
            else
            // If Subject contains the Subject OR subject matches completely
            if (MsgtoValid.MsgSubject.ToLower().Contains(Subject.ToLower()) || MsgtoValid.MsgSubject.ToLower() == Subject.ToLower())
            {
                subject = true;
            }

            if (fromAddress && subject)
            {
                bValid = true;
            }
            else
            {
                bValid = false;
                Program.AddRTBText(rtbStatus, string.Format("{0:g} ", DateTime.Now) + " Unable to match Receive profile:  Sender:" + SenderEmail + " Subject:" + Subject, Color.Red);
            }
            return(bValid);
        }
Beispiel #2
0
        public static List <MsgSummary> GetMail(string hostname, int port, string useSsl, string username, string password)
        {
            int    messageCount = 0;
            String fromMail     = "";
            String subject      = "";
            bool   ssl          = false;

            switch (useSsl)
            {
            case "none":
                ssl = false;
                break;

            case "SSL":
                ssl = true;
                break;

            case "TLS":
                ssl = true;
                break;
            }
            List <MsgSummary> allMessages = new List <MsgSummary>();
            Pop3Client        client      = new Pop3Client();

            try
            {
                client.Connect(hostname, port, ssl);
                if (client.Connected)
                {
                    client.Authenticate(username, password);
                }
                messageCount = client.GetMessageCount();
                if (messageCount > 0)
                {
                    for (int i = messageCount; i > 0; i--)
                    {
                        if (client.GetMessage(i) != null)
                        {
                            OpenPop.Mime.Message msg     = client.GetMessage(i);
                            MsgSummary           newSumm = new MsgSummary();
                            try
                            {
                                if (msg.Headers.From.MailAddress.Address != null && msg.Headers.From.MailAddress.Address != "")
                                {
                                    fromMail = msg.Headers.From.MailAddress.Address;
                                }
                                if (msg.Headers.Subject != null && msg.Headers.Subject != "")
                                {
                                    subject = msg.Headers.Subject;
                                }
                                List <MsgAtt> att = new List <MsgAtt>();
                                foreach (OpenPop.Mime.MessagePart attach in msg.FindAllAttachments())
                                {
                                    string file_name_attach = attach.FileName;
                                    if (attach.FileName != "(no name)")
                                    {
                                        FileInfo fi    = new FileInfo(Path.Combine(Globals.TempPath, file_name_attach));
                                        int      ij    = 0;
                                        string   sName = fi.Name;
                                        string   sExt  = fi.Extension;

                                        while (File.Exists(sName))
                                        {
                                            sName = fi.Name + ij.ToString() + sExt;
                                            ij++;
                                        }
                                        fi = new FileInfo(Path.Combine(Globals.TempPath, sName));
                                        att.Add(new MsgAtt()
                                        {
                                            AttFilename = fi
                                        });

                                        attach.Save(fi);
                                    }
                                }
                                newSumm.MsgFile    = att;
                                newSumm.MsgFrom    = fromMail;
                                newSumm.MsgSubject = subject;
                                newSumm.MsgStatus  = "Ok";
                                newSumm.MsgId      = i;
                            }
                            catch (Exception exAtt)
                            {
                                // Program.AddRTBText(rtbStatus, string.Format("{0:g} ", DateTime.Now) + " Error Retrieving Email message " + i.ToString() + " of " + messageCount.ToString()+ " Error was: " + exAtt.Message + Environment.NewLine, Color.Red);
                                newSumm.MsgStatus = exAtt.Message;
                            }
                            finally
                            {
                                allMessages.Add(newSumm);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MsgSummary newSumm = new MsgSummary();
                newSumm.MsgStatus = ex.Message;
                allMessages.Add(newSumm);
            }
            finally
            {
                try
                {
                    client.Disconnect();
                }
                catch (Exception ex)
                {
                }
            }
            return(allMessages);
        }