コード例 #1
0
ファイル: IMAP.cs プロジェクト: gahadzikwa/GAPP
        /// <summary>
        /// Retrieves the header information from the server and populates the IMAPMessage objects properties
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="partID"></param>
        /// <returns></returns>
        public bool ProcessMessageHeader(IMAPMessage msg, int partID)
        {
            Dictionary<string, string> headerData = new Dictionary<string, string>();
            string cmd = "UID FETCH {0} BODY[{1}]\r\n";

            cmd = String.Format(cmd, msg.Uid, partID > 0 ? partID + ".MIME" : "HEADER");

            ArrayList result = new ArrayList();
            ArrayList lineToProcess = new ArrayList();
            SendAndReceive(cmd, ref result);
            string temp = "";
            for (int i = 0; i < result.Count; i++)
            {
                // start of response, just skip it
                if (result[i].ToString().StartsWith("*") || result[i].ToString() == String.Empty || result[i].ToString().StartsWith(" ") || result[i].ToString().StartsWith("IMAP"))
                    continue;
                
                temp = result[i].ToString();

                // check each line after this line, looking for a tab or space. this indicates that those lines
                // are associated with the line in temp and should be appended. The loop ends when neither a tab or a space
                // is found, and the loop should not even be entered if one of those characters are not found.

                string currentLine = (i + 1 < result.Count - 1) ? result[i + 1].ToString() : "";

                while (currentLine.StartsWith("\t") || currentLine.StartsWith(" "))
                {
                    if (String.IsNullOrEmpty(currentLine))
                        break;

                    if (currentLine.StartsWith(" "))
                        temp += currentLine.TrimEnd();
                    else
                        temp += currentLine.Trim();

                    i++;
                    currentLine = (i + 1 < result.Count - 1) ? result[i + 1].ToString() : "";
                }
                                
                lineToProcess.Add(temp);
            }

            // now we process each data line into its name and value
            foreach (string line in lineToProcess)
            {
                int idx = line.IndexOf(":");
                if (idx == -1) continue;

                string name = line.Substring(0, idx).Replace("-","");
                int len = line.Length - idx;
                string value = line.Substring(idx + 1, line.Length - idx-1);
                // if a certain data item is already there then we just append the additional data.
                // this usually occurs with the Received: field
                if (headerData.ContainsKey(name))
                {
                    headerData[name] += value;
                }
                else
                {
                    headerData.Add(name, value);
                }
            }

            foreach (string name in headerData.Keys)
            {
                // process special cases first
                if (name.ToLower().Equals("to"))
                {
                    msg.SetPropValue(name,ParseAddresses(headerData[name].ToString()));
                    continue;
                }

                if (name.ToLower().Equals("from"))
                {
                    msg.SetPropValue(name, ParseAddresses(headerData[name].ToString()));
                    continue;
                }

                if (name.ToLower().Equals("cc"))
                {
                    msg.SetPropValue(name, ParseAddresses(headerData[name].ToString()));
                    continue;
                }

                if (name.ToLower().Equals("bcc"))
                {
                    msg.SetPropValue(name, ParseAddresses(headerData[name].ToString()));
                    continue;
                }

                if (name.ToLower().Equals("date"))
                {
                    string date = headerData[name].ToString();
                    
                    // special processing needed for gmail. the format they send the date in does not automatically convert                    
                    if (date.IndexOf(" (") > 0)
                    {
                        date = date.Substring(0, headerData[name].IndexOf("("));
                    }

                    DateTime dt = new DateTime();
                    DateTime.TryParse(date, out dt);
                    msg.SetPropValue(name, dt);
                    continue;
                }
                
                if (!msg.SetPropValue(name, headerData[name].ToString().Trim()))
                {
                    //if (!name.StartsWith("X"))
                    //    Log(LogTypeEnum.WARN, String.Format("IMAPMessage does not contain property for {0}", name));
                }
            }

            msg.HeaderLoaded = true;

            if (msg.ContentType == null)
                msg.ContentType = "text/plain";

            

            return true;
        }