Esempio n. 1
0
        /// <summary>
        /// sample2.txt ファイルを使って Mail クラスのテストを行います。
        /// </summary>
        private static void ShowMailTest2()
        {
            Console.WriteLine("sample2.txt のテスト");

            // テストのために POP から取得する代わりにファイルから読み込みます。
            string mailtext = null;

            using (StreamReader sr = new StreamReader("sample2.txt")) {
                mailtext = sr.ReadToEnd();
            }

            // Mail クラスを作成します。
            Mail mail = new Mail(mailtext);

            // From、To、Subject を表示します。
            Console.WriteLine(mail.Header["From"][0]);
            Console.WriteLine(mail.Header["To"][0]);
            Console.WriteLine(mail.Header["Subject"][0]);
            Console.WriteLine("--");

            // デコードしたFrom、To、Subject を表示します。
            Console.WriteLine(MailHeader.Decode(mail.Header["From"][0]));
            Console.WriteLine(MailHeader.Decode(mail.Header["To"][0]));
            Console.WriteLine(MailHeader.Decode(mail.Header["Subject"][0]));
            Console.WriteLine("--");

            // Content-Type を表示します。
            Console.WriteLine(mail.Header["Content-Type"][0]);
            Console.WriteLine("--");

            // 1つ目のパートの Content-Type、メール本文を表示します。
            // 本来は Content-Type の charset を参照してデコードすべきですが
            // ここではサンプルとして iso-2022-jp 固定でデコードします。
            MailMultipart part1 = mail.Body.Multiparts[0];
            Console.WriteLine("パート1");
            Console.WriteLine(part1.Header["Content-Type"][0]);
            Console.WriteLine("--");

            byte[] bytes = Encoding.ASCII.GetBytes(part1.Body.Text);
            string mailbody = Encoding.GetEncoding("iso-2022-jp").GetString(bytes);

            Console.WriteLine("↓本文↓");
            Console.WriteLine(mailbody);
            Console.WriteLine("--");

            // 2つ目のパートの Content-Type を表示し、BASE64 をデコードしてファイルとして保存します。
            // 本来は Content-Transfer-Encoding が base64 であることを確認したり、
            // Content-Type の name を参照してファイル名を決めたりすべきですが、ここでは省略しています。
            MailMultipart part2 = mail.Body.Multiparts[1];
            Console.WriteLine("パート2");
            Console.WriteLine(part2.Header["Content-Type"][0]);
            Console.WriteLine("--");

            bytes = Convert.FromBase64String(part2.Body.Text);
            using (Stream stm = File.Open("sample2.gif", FileMode.Create))

            using (BinaryWriter bw = new BinaryWriter(stm)) {
                bw.Write(bytes);
            }

            Console.WriteLine("添付ファイルを保存しました。ファイル名は sample2.gif です。");
        }
Esempio n. 2
0
        /// <summary>
        /// ファイル化したメールを画面に表示します
        /// </summary>
        private static void ShowMailTest1()
        {
            Console.WriteLine("sample1.txt のテスト");

            // テストのために POP から取得する代わりにファイルから読み込みます。
            string mailtext = null;

            using (StreamReader sr = new StreamReader("sample1.txt")) {
                mailtext = sr.ReadToEnd();
            }

            // Mail クラスを作成します。
            Mail mail = new Mail(mailtext);

            // From、To、Subject を表示します。
            Console.WriteLine(mail.Header["From"][0]);
            Console.WriteLine(mail.Header["To"][0]);
            Console.WriteLine(mail.Header["Subject"][0]);
            Console.WriteLine("--");

            // デコードしたFrom、To、Subject を表示します。
            Console.WriteLine(MailHeader.Decode(mail.Header["From"][0]));
            Console.WriteLine(MailHeader.Decode(mail.Header["To"][0]));
            Console.WriteLine(MailHeader.Decode(mail.Header["Subject"][0]));
            Console.WriteLine("--");

            // Content-Type を表示します。
            Console.WriteLine(mail.Header["Content-Type"][0]);
            Console.WriteLine("--");

            // メール本文を表示します。
            // 本来は Content-Type の charset を参照してデコードすべきですが
            // ここではサンプルとして iso-2022-jp 固定でデコードします。
            byte[] bytes = Encoding.ASCII.GetBytes(mail.Body.Text);
            string mailbody = Encoding.GetEncoding("iso-2022-jp").GetString(bytes);

            Console.WriteLine("↓本文↓");
            Console.WriteLine(mailbody);
        }
Esempio n. 3
0
        //Принимает List с заголовками(Subject Date From) писем
        //Парсит только одно письмо
        //Через Line возвращает индекс начала заголовков следующего письма
        private void ParseHeaders(List <string> data, ref List <Mail> mails, ref int line)
        {
            string header  = string.Empty;
            string str     = string.Empty;
            bool   replace = false;
            Mail   mail    = new Mail();

            for (int i = line; i < data.Count; i++)
            {
                str = data[i];

                if (i == (data.Count - 2) && str.StartsWith("*"))
                {
                    line = data.Count;
                    return;
                }


                // ) - Конец заголовков одного письма
                Regex regex = new Regex(@"(UID )(?<uid>[0-9]*)(\))");
                Match match = regex.Match(str);
                if (str == ")" || match.Success)
                {
                    mails.Add(mail);
                    line = i + 1;

                    if (match.Success)
                    {
                        mail.Id = int.Parse(match.Groups["uid"].Value);
                    }

                    return;
                }

                if (str == "")
                {
                    continue;
                }

                // * - некоторая доп информация от сервера.
                //Отсюда необходим порядовый номер письма в ящике
                if (str.StartsWith("*"))
                {
                    regex = new Regex("(UID) (?<uid>[0-9]*) (BODY)");
                    match = regex.Match(str);
                    if (match.Success)
                    {
                        mail.Id = int.Parse(match.Groups["uid"].Value);
                    }
                    continue;
                }

                replace = false;
                if (str.StartsWith("Subject: "))
                {
                    header  = "Subject: ";
                    replace = true;
                }
                else if (str.StartsWith("Date: "))
                {
                    header  = "Date: ";
                    replace = true;

                    int index = str.IndexOf("(");
                    if (index != -1)
                    {
                        str = str.Substring(0, index);
                    }
                }
                else if (str.StartsWith("From: "))
                {
                    header  = "From: ";
                    replace = true;
                }

                if (replace)
                {
                    str = str.Replace(header, string.Empty);
                }

                str = Utils.DecodeEncodedLine(str);

                switch (header)
                {
                case "Subject: ": mail.Subject += str; break;

                case "Date: ": mail.Date = DateTime.Parse(str); break;

                case "From: ": mail.From += str; break;

                default:
                    continue;
                    //throw new ArgumentException("Unknown Header!");
                }
            }
        }