/// <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);
}