/// <summary> /// POP3サーバからメールを受信する /// </summary> private void RecieveMail() { int mailCount = 0; // 未受信メール件数 ProgressMailInitDlg progressMailInit = ProgressMailInit; ProgressMailUpdateDlg progressMailUpdate = ProgressMailUpdate; UpdateViewDlg updateView = UpdateView; FlashWindowOnDlg flashWindow = FlashWindowOn; EnableButtonDlg enableButton = EnableButton; try { // ステータスバーに状況表示する labelMessage.Text = "メール受信中"; // POP3のセッションを作成する using (var pop = new nMail.Pop3()) { // POP3への接続タイムアウト設定をする Options.EnableConnectTimeout(); // APOPを使用するときに使うフラグ pop.APop = AccountInfo.apopFlag; // POP3 over SSL/TLSフラグが有効のときはSSLを使用する if (AccountInfo.popOverSSL) { pop.SSL = nMail.Pop3.SSL3; pop.Connect(AccountInfo.popServer, AccountInfo.popPortNumber); } else { // POP3へ接続する pop.Connect(AccountInfo.popServer, AccountInfo.popPortNumber); } // POP3への認証処理を行う pop.Authenticate(AccountInfo.userName, AccountInfo.passWord); // 未受信のメールが何件あるかチェックする var countMail = new Task<int>(() => { var uidls = Enumerable.Range(1, pop.Count).Select(i => { pop.GetUidl(i); return pop.Uidl; }); var locals = collectionMail[RECEIVE].Union(collectionMail[DELETE]); var unreadMails = from u in uidls join l in locals on u equals l.uidl select l; return unreadMails.Count(); }); countMail.Start(); // POP3サーバ上に1件以上のメールが存在するとき if (pop.Count > 0) { // ステータスバーに状況表示する labelMessage.Text = pop.Count + "件のメッセージがサーバ上にあります。"; } else { // ステータスバーに状況表示する labelMessage.Text = "新着のメッセージはありませんでした。"; // メール受信のメニューとツールボタンを有効化する Invoke(enableButton, 1); return; } var receivedCount = countMail.Result; // 受信済みメールカウントがPOP3サーバ上にあるメール件数と同じとき if (receivedCount == pop.Count) { // ステータスバーに状況表示する labelMessage.Text = "新着のメッセージはありませんでした。"; // プログレスバーを非表示に戻す Invoke(new ProgressMailDisableDlg(ProgressMailDisable)); // メール受信のメニューとツールボタンを有効化する Invoke(enableButton, 1); return; } // プログレスバーを表示して最大値を未受信メール件数に設定する int mailCountMax = pop.Count - receivedCount; Invoke(progressMailInit, mailCountMax); // 未受信のメールを取得するためカウントを1増加させる receivedCount++; // 取得したメールをコレクションに追加する for (int no = receivedCount; no <= pop.Count; no++) { // 受信中件数を表示 labelMessage.Text = no + "件目のメールを受信しています。"; // メールのUIDLを取得する pop.GetUidl(no); // HTML/Base64のデコードを無効にする Options.DisableDecodeBodyText(); // メールの情報を取得する pop.GetMail(no); // メールの情報を格納する Mail mail = new Mail(pop.From, pop.Header, pop.Subject, pop.Body, pop.FileName, pop.DateString, pop.Size.ToString(), pop.Uidl, true, "", pop.GetDecodeHeaderField("Cc:"), "", Mail.ParsePriority(pop.Header)); collectionMail[RECEIVE].Add(mail); // 受信メールの数を増加する mailCount++; // メール受信時にPOP3サーバ上のメール削除のチェックがある時はPOP3サーバからメールを削除する if (AccountInfo.deleteMail) { pop.Delete(no); } // メールの受信件数を更新する Invoke(progressMailUpdate, mailCount); // スレッドを1秒間待機させる System.Threading.Thread.Sleep(1000); } } // プログレスバーを非表示に戻す Invoke(new ProgressMailDisableDlg(ProgressMailDisable)); // メール受信のメニューとツールボタンを有効化する Invoke(enableButton, 1); // 未受信メールが1件以上の場合 if (mailCount >= 1) { // メール着信音の設定をしている場合 if (AccountInfo.popSoundFlag && AccountInfo.popSoundName != "") { SoundPlayer sndPlay = new SoundPlayer(AccountInfo.popSoundName); sndPlay.Play(); } // ウィンドウが最小化でタスクトレイに格納されていて何分間隔かで受信をするとき if (this.WindowState == FormWindowState.Minimized && AccountInfo.minimizeTaskTray && AccountInfo.autoMailFlag) { notifyIcon1.BalloonTipIcon = ToolTipIcon.Info; notifyIcon1.BalloonTipTitle = "新着メール"; notifyIcon1.BalloonTipText = mailCount + "件の新着メールを受信しました。"; notifyIcon1.ShowBalloonTip(300); } else { // 画面をフラッシュさせる Invoke(flashWindow); // ステータスバーに状況表示する labelMessage.Text = mailCount + "件の新着メールを受信しました。"; } // データ変更フラグをtrueにする dataDirtyFlag = true; } else { // ステータスバーに状況表示する labelMessage.Text = "新着のメッセージはありませんでした。"; // メール受信のメニューとツールボタンを有効化する Invoke(enableButton, 1); return; } } catch (nMail.nMailException nex) { // ステータスバーに状況表示する labelMessage.Text = "エラーNo:" + nex.ErrorCode + " エラーメッセージ:" + nex.Message; // メール受信のメニューとツールボタンを有効化する Invoke(enableButton, 1); return; } catch (Exception exp) { // ステータスバーに状況表示する labelMessage.Text = "エラーメッセージ:" + exp.Message; // メール受信のメニューとツールボタンを有効化する Invoke(enableButton, 1); return; } // TreeViewとListViewの更新を行う Invoke(updateView, 0); }
/// <summary> /// メールデータの読み込み /// </summary> private void MailDataLoad() { // 予期せぬエラーの時にメールの本文が分かるようにするための変数 string expSubject = ""; int n = 0; // スレッドのロックをかける lock (lockobj) { if (File.Exists(Application.StartupPath + @"\Mail.dat")) { try { // ファイルストリームをストリームリーダに関連付ける using (var reader = new StreamReader(Application.StartupPath + @"\Mail.dat", Encoding.UTF8)) { // GetHederFieldとHeaderプロパティを使うためPop3クラスを作成する using (var pop = new Pop3()) { // データを読み出す foreach (var mailList in collectionMail) { try { // メールの件数を読み出す n = Int32.Parse(reader.ReadLine()); } catch (Exception) { // エラーフラグをtrueに変更する errorFlag = true; MessageBox.Show("メール件数とメールデータの数が一致していません。\n件数またはデータレコードをテキストエディタで修正してください。", "Akane Mail", MessageBoxButtons.OK, MessageBoxIcon.Stop); return; } // メールを取得する for (int j = 0; j < n; j++) { // 送信メールのみ必要な項目 string address = reader.ReadLine(); string subject = reader.ReadLine(); // 予期せぬエラーの時にメッセージボックスに表示する件名 expSubject = subject; // ヘッダを取得する string header = ""; string hd = reader.ReadLine(); // 区切り文字が来るまで文字列を連結する while (hd != "\x03") { header += hd + "\r\n"; hd = reader.ReadLine(); } // 本文を取得する string body = ""; string b = reader.ReadLine(); // エラー文字区切りの時対策 bool err_parse = false; // 区切り文字が来るまで文字列を連結する while (b != "\x03") { // 区切り文字が本文の後ろについてしまったとき if (b.Contains("\x03") && b != "\x03") { // 区切り文字を取り除く err_parse = true; b = b.Replace("\x03", ""); } body += b + "\r\n"; // 区切り文字が検出されたときは区切り文字を取り除いてループから抜ける if (err_parse) { break; } b = reader.ReadLine(); } // 受信・送信日時を取得する string date = reader.ReadLine(); // メールサイズを取得する(送信メールは0byte扱い) string size = reader.ReadLine(); // UIDLを取得する(送信メールは無視) string uidl = reader.ReadLine(); // 添付ファイル名を取得する(受信メールは無視) string attach = reader.ReadLine(); // 既読・未読フラグを取得する bool notReadYet = (reader.ReadLine() == "True"); // CCのアドレスを取得する string cc = reader.ReadLine(); // BCCを取得する(受信メールは無視) string bcc = reader.ReadLine(); // 重要度を取得する string priority = reader.ReadLine(); // 旧ファイルを読み込んでいるとき if (priority != "urgent" && priority != "normal" && priority != "non-urgent") { // エラーフラグをtrueに変更する errorFlag = true; MessageBox.Show("Version 1.10以下のファイルを読み込もうとしています。\nメールデータ変換ツールで変換してから読み込んでください。", "Akane Mail", MessageBoxButtons.OK, MessageBoxIcon.Stop); return; } // 変換フラグを取得する(旧バージョンからのデータ移行) string convert = reader.ReadLine(); // ヘッダーがあった場合はそちらを優先する if (header.Length > 0) { // ヘッダープロパティにファイルから取得したヘッダを格納する pop.Header = header; // アドレスを取得する pop.GetDecodeHeaderField("From:"); address = pop.Field ?? address; // 件名を取得する pop.GetDecodeHeaderField("Subject:"); subject = pop.Field ?? subject; // ヘッダからCCアドレスを取得する pop.GetDecodeHeaderField("Cc:"); cc = pop.Field ?? cc; // ヘッダから重要度を取得する priority = Mail.ParsePriority(header); } // メール格納配列に格納する var mail = new Mail(address, header, subject, body, attach, date, size, uidl, notReadYet, convert, cc, bcc, priority); mailList.Add(mail); } } } } } catch (Exception exp) { MessageBox.Show("予期しないエラーが発生しました。\n" + "件名:" + expSubject + "\n" + "エラー詳細 : \n" + exp.Message, "Akane Mail", MessageBoxButtons.OK, MessageBoxIcon.Stop); } } } }