示例#1
0
        /// <summary>Retrieves a message header by its internal number (1..<see cref="ImapMailboxInfo.Exist" />).</summary>
        /// <param name="number">The internal message number (1..<see cref="ImapMailboxInfo.Exist" />).</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentOutOfRangeException"></exception>
        /// <exception cref="System.FormatException"></exception>
        public Rfc822Message GetMessageHeader(int number)
        {
            if (number < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(number));
            }

            while (true)
            {
                var answer = SendCommand("FETCH " + number + " BODY[HEADER]");
                if (answer.Data.Length == 0)
                {
                    continue;
                }

                var streamReader = answer.GetStreamReader(0);
                var header       = streamReader.ReadLine();
                var size         = int.Parse(header.Substring(header.LastIndexOf('{')).Unbox("{", "}", true));
                var dataReader   = answer.GetDataReader(header.Length + 2);
                var messageData  = dataReader.ReadBytes(size);
                if (messageData.Length != size)
                {
                    throw new FormatException();
                }

                var message = Rfc822Message.FromBinary(messageData);
                return(message);
            }
        }
示例#2
0
        public static string saveEmail(Rfc822Message msg, long issueId, long acctId, string uid)
        {
            string filename = "";

            if ((GlobalShared.mailInDir != null) & !string.IsNullOrEmpty(GlobalShared.mailInDir.Trim()))
            {
                filename = string.Format(GlobalShared.popFilename, GlobalShared.mailInDir, acctId, issueId, DateTime.Now, uid, "txt");
                System.IO.StreamWriter sw = new System.IO.StreamWriter(filename, false);
                sw.Write(messageFormat(msg));
                sw.Flush();
                sw.Close();
            }
            return(filename);
        }
示例#3
0
        /// <summary>Retrieves a message by its internal number (1..<see cref="ImapMailboxInfo.Exist" />).</summary>
        /// <param name="number">The internal message number (1..<see cref="ImapMailboxInfo.Exist" />).</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentOutOfRangeException"></exception>
        public Rfc822Message GetMessage(int number)
        {
            if (number < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(number));
            }

            var answer  = SendCommand("FETCH " + number + " BODY[]");
            var start   = Array.IndexOf(answer.Data, (byte)'\n') + 1;
            var end     = Array.LastIndexOf(answer.Data, (byte)')') - 2;
            var message = new byte[end - start];

            Array.Copy(answer.Data, start, message, 0, end - start);
            return(Rfc822Message.FromBinary(message));
        }
示例#4
0
        public static string messageFormat(Rfc822Message msg)
        {
            string outString = "";

            outString  = "From: " + msg.From.Address + Microsoft.VisualBasic.Constants.vbCrLf;
            outString += "To:      ";
            foreach (Email.Net.Common.EmailAddress addr in msg.To)
            {
                outString += addr.Address + ", \"";
            }
            outString += Microsoft.VisualBasic.Constants.vbCrLf;
            bool cc = false;

            foreach (Email.Net.Common.EmailAddress addr in msg.CarbonCopies)
            {
                if (!cc)
                {
                    outString += "Bcc:   ";
                    cc         = true;
                }
                outString += addr.Address + ", ";
            }
            foreach (Email.Net.Common.EmailAddress addr in msg.BlindedCarbonCopies)
            {
                if (!cc)
                {
                    outString += "Bcc:   ";
                    cc         = true;
                }
                outString += addr.Address + ", ";
            }
            outString += "Date:    " + msg.Date + Microsoft.VisualBasic.Constants.vbCrLf;
            outString += "Subject: " + msg.Subject + Microsoft.VisualBasic.Constants.vbCrLf;
            outString += "Message: " + Microsoft.VisualBasic.Constants.vbCrLf + msg.Text + Microsoft.VisualBasic.Constants.vbCrLf;
            return(outString);
        }
示例#5
0
        public static string findIssueTag(Rfc822Message msg)
        {
            string tag   = "";
            bool   found = false;

            if (GlobalShared.regx.IsMatch(msg.Subject))
            {
                found = true;
                return(parseIssueTag(msg.Subject));
            }
            if (msg.Header.ExtraHeaders.Count > 0)
            {
                foreach (System.Collections.Generic.KeyValuePair <string, string> hdr in msg.Header.ExtraHeaders)
                {
                    if (hdr.Key.Equals(GlobalShared.ISSUE_HEADER))
                    {
                        found = true;
                        tag   = hdr.Value;
                        break;                         // TODO: might not be correct. Was : Exit For
                    }
                }
            }
            return(tag);
        }
示例#6
0
        private SortedList <string, Rfc822Message> getSmtpMessages()
        {
            Pop3Client target = new Pop3Client();

            target.Host = Host;
            //TCP port for connection
            target.Port = Convert.ToUInt16(Port);
            //Username to login to the POP3 server
            target.Username = Username;
            //Password to login to the POP3 server
            target.Password = Password;
            // SSL Interaction type
            Email.Net.Common.Configurations.EInteractionType interaction = default(Email.Net.Common.Configurations.EInteractionType);
            if (TSL)
            {
                interaction = EInteractionType.StartTLS;
            }
            else if (SSL)
            {
                interaction = EInteractionType.SSLPort;
            }
            else
            {
                interaction = EInteractionType.Plain;
            }
            target.SSLInteractionType = EInteractionType.SSLPort;
            target.AuthenticationType = EAuthenticationType.Login;
            if (authenticationType == (int)EAuthenticationType.None)
            {
                Password = "";
                Username = "";
            }

            System.Collections.Generic.SortedList <string, Rfc822Message> messages = new System.Collections.Generic.SortedList <string, Rfc822Message>();
            Rfc822Message msg = null;

            // Login to POP server
            try {
                Pop3Response response = target.Login();
                if (response.Type == EPop3ResponseType.OK)
                {
                    // Retrieve Unique IDs for all messages
                    Pop3MessageUIDInfoCollection messageUIDs = target.GetAllUIDMessages();
                    //Check if messages already received
                    foreach (Pop3MessageUIDInfo uidInfo in messageUIDs)
                    {
                        try {
                            msg = target.GetMessage(uidInfo.SerialNumber);
                        } catch (Email.Net.Common.Exceptions.ConnectionException ex) {
                        } catch (Email.Net.Common.Exceptions.AuthenticationMethodNotSupportedException ex) {
                        } catch (System.Exception ex) {
                        }
                        if ((msg != null))
                        {
                            if (!string.IsNullOrEmpty(UtilFunctions.findIssueTag(msg).Trim()))
                            {
                                if (!UtilFunctions.searchUID(uidInfo.UniqueNumber))
                                {
                                    // No.  Add to list
                                    messages.Add(uidInfo.UniqueNumber, target.GetMessage(uidInfo.SerialNumber));
                                }
                            }
                        }
                        if (!runFlag | !isEnabled)
                        {
                            break;                             // TODO: might not be correct. Was : Exit For
                        }
                    }
                    string cnt = (messages.Count.ToString());
                    cnt += " SMTP messages were found";
                    GlobalShared.Log.Log((int)LogClass.logType.Info, cnt, false);
                }
                //Logout from the server
                target.Logout();
            } catch (IOException ex) {
                GlobalShared.Log.Log((int)LogClass.logType.ErrorCondition, ex.Message, false);
            } catch (Email.Net.Common.Exceptions.ConnectionException ex) {
                GlobalShared.Log.Log((int)LogClass.logType.ErrorCondition, ex.Message, false);
            } catch (System.Exception ex) {
                GlobalShared.Log.Log((int)LogClass.logType.ErrorCondition, ex.Message, false);
            }
            return(messages);
        }