/// <summary> /// 重要度取得 /// </summary> /// <param name="header">ヘッダ</param> /// <returns>重要度(urgent/normal/non-urgent)</returns> public static string Parse(string header) { string _priority = "normal"; string priority = ""; var pop = new Pop3(); // ヘッダにX-Priorityがあるとき if (header.Contains("X-Priority:")) { priority = pop.GetHeaderField("X-Priority:", header); if (priority == "1" || priority == "2") { _priority = Urgent; } else if (priority == "3") { _priority = Normal; } else if (priority == "4" || priority == "5") { _priority = NonUrgent; } } else if (header.Contains("X-MsMail-Priotiry:")) { priority = pop.GetHeaderField("X-MsMail-Priotiry:", header); if (priority.ToLower() == "High") { _priority = Urgent; } else if (priority.ToLower() == "Normal") { _priority = Normal; } else if (priority.ToLower() == "low") { _priority = NonUrgent; } } else if (header.Contains("Importance:")) { priority = pop.GetHeaderField("Importance:", header); if (priority.ToLower() == "high") { _priority = Urgent; } else if (priority.ToLower() == "normal") { _priority = Normal; } else if (priority.ToLower() == "low") { _priority = NonUrgent; } } else if (header.Contains("Priority:")) { priority = pop.GetHeaderField("Priority:", header); // 重要度の文字列の長さが0以上のときは取得した重要度を入れる if (priority.Length > 0) { _priority = priority; } } return _priority; }
/// <summary> /// 文字コードを取得する /// </summary> /// <param name="mailHeader">メールヘッダ</param> /// <returns>文字コード</returns> public static string ParseEncoding(string mailHeader) { Pop3 pop = new Pop3(); // メールヘッダから文字コード文字列を抜き出す string codeName = pop.GetHeaderField("Content-Type:", mailHeader); codeName = codeName.Replace("\"", ""); string[] arrayName = codeName.Split('='); codeName = arrayName[1]; return codeName; }
// メールデータの変換を行う(1.01→1.20) public int DataConvertA(string file_name) { // ファイルのバックアップ string file_name_bak = file_name.Substring(0, file_name.Length - 3) + "bak"; try { File.Copy(file_name, file_name_bak); } catch (Exception) { MessageBox.Show(file_name_bak + "が既に存在しています。\n変換済みか何らかのエラーで作られた可能性があります。", "変換エラー", MessageBoxButtons.OK, MessageBoxIcon.Stop); return 0; } // ファイルストリームを作成する FileStream stream_r = new FileStream(file_name, FileMode.Open); // ファイルストリームをストリームリーダに関連付ける StreamReader reader = new StreamReader(stream_r, Encoding.Default); // GetHederFieldとHeaderプロパティを使うためPop3クラスを作成する nMail.Pop3 pop = new nMail.Pop3(); // データを読み出す for (int i = 0; i < collectionMail.Length; i++) { // メールの件数を読み出す int n = Int32.Parse(reader.ReadLine()); // メールを取得する for (int j = 0; j < n; j++) { // 送信メールのみ必要な項目 string address = reader.ReadLine(); // アドレスがx-akane-convert-mailまたは空値のとき if (address.Contains("x-akane-convert-mail") == true) { reader.Close(); stream_r.Close(); File.Delete(file_name_bak); MessageBox.Show("このメールデータは1.10のメールデータに変換済みです。\n1.10から1.20の形式に変換するを選択して実行してください。", "注意", MessageBoxButtons.OK, MessageBoxIcon.Stop); return 0; } string subject = reader.ReadLine(); // ヘッダを取得する string header = ""; string hd = reader.ReadLine(); // ヘッダがurgent、normal、non-urgentのとき if (hd.Contains("urgent") == true || hd.Contains("normal") == true || hd.Contains("non-urgent") == true) { reader.Close(); stream_r.Close(); File.Delete(file_name_bak); MessageBox.Show("このメールデータは変換済みです。", "注意", MessageBoxButtons.OK, MessageBoxIcon.Stop); return 0; } // 区切り文字が来るまで文字列を連結する while (hd != "\x03") { header = header + hd + "\r\n"; hd = reader.ReadLine(); } // ヘッダのサイズが1バイト以上の場合 if (header.Length > 0) { // ヘッダープロパティにファイルから取得したヘッダを格納する pop.Header = header; // アドレスを取得する pop.GetHeaderField("From:"); if (pop.Field != null) { address = pop.Field; } // 件名を取得する pop.GetHeaderField("Subject:"); if (pop.Field != null) { subject = pop.Field; } } // 本文を取得する 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 = body + b + "\r\n"; // 区切り文字が検出されたときは区切り文字を取り除いてループから抜ける if (err_parse == true) { break; } b = reader.ReadLine(); } // 受信・送信日時を取得する string date = reader.ReadLine(); // sizeの値が空値(送信メール)のとき if (date == "未送信") { // 文字列に現在時刻を格納する date = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString(); } // メールサイズを取得する(送信メールは0を格納する) string size = reader.ReadLine(); // sizeの値が空値(送信メール)のとき if (size == "") { // 文字列で0を格納する size = "0"; } // UIDLを取得する(送信メールは無視) string uidl = reader.ReadLine(); // 添付ファイル名を取得する(受信メールは無視) string attach = reader.ReadLine(); // 既読・未読フラグを取得する bool notReadYet = (reader.ReadLine() == "True"); // メール格納配列に格納する Mail mail = new Mail(address, header, subject, body, attach, date, size, uidl, notReadYet, "x-akane-convert-mail"); collectionMail[i].Add(mail); } } // ストリームリーダとファイルストリームを閉じる reader.Close(); stream_r.Close(); // データファイルを削除する File.Delete(file_name); // ファイルストリームを作成する FileStream stream_w = new FileStream(file_name, FileMode.Create); // ファイルストリームをストリームライタに関連付ける StreamWriter writer = new StreamWriter(stream_w, Encoding.UTF8); // メールの件数とデータを書き込む for (int i = 0; i < collectionMail.Length; i++) { writer.WriteLine(collectionMail[i].Count.ToString()); foreach (Mail mail in collectionMail[i]) { writer.WriteLine(mail.address); writer.WriteLine(mail.subject); writer.Write(mail.header); writer.WriteLine("\x03"); writer.Write(mail.body); writer.WriteLine("\x03"); writer.WriteLine(mail.date); writer.WriteLine(mail.size); writer.WriteLine(mail.uidl); writer.WriteLine(mail.attach); writer.WriteLine(mail.notReadYet.ToString()); // CCアドレスを取得する string cc = ""; // ヘッダが存在するとき if (mail.header.Length > 0) { // ヘッダープロパティにファイルから取得したヘッダを格納する pop.Header = mail.header; // ヘッダからCCアドレスを取得する pop.GetHeaderField("Cc:"); if (pop.Field != null) { cc = pop.Field; } } // CCアドレスを書き込む writer.WriteLine(cc); // BCCを設定する(受信メールは無視) string bcc = ""; // CCアドレスを書き込む writer.WriteLine(bcc); // 重要度を取得する string priority = "normal"; // ヘッダが存在するとき if (mail.header.Length > 0) { // ヘッダから重要度を取得する priority = GetPriority(mail.header); } // 重要度を書き込む writer.WriteLine(priority); // コンバート済みのステータスを書き込む writer.WriteLine(mail.convert); } } // ストリームライタとファイルストリームを閉じる writer.Close(); stream_w.Close(); MessageBox.Show("変換が完了しました。", "完了", MessageBoxButtons.OK, MessageBoxIcon.Information); return 0; }
/// <summary> /// メールを送信する /// </summary> private void SendMail() { ProgressMailInitDlg progressMailInit = ProgressMailInit; ProgressMailUpdateDlg progressMailUpdate = ProgressMailUpdate; UpdateViewDlg updateView = UpdateView; EnableButtonDlg enableButton = EnableButton; int max_no = 0; int send_no = 0; // 送信可能なメールの数を確認する max_no = collectionMail[SEND].Count(m => m.notReadYet); // 送信可能なメールが存在しないとき if (max_no == 0) { // メール送信・受信のメニューとツールボタンを有効化する Invoke(enableButton, 1); return; } try { // ステータスバーに状況表示する labelMessage.Text = "メール送信中"; // プログレスバーを表示して最大値を未送信メール件数に設定する Invoke(progressMailInit, max_no); // POP before SMTPが有効の場合 if (AccountInfo.popBeforeSMTP) { try { // 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); } } 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; } } // SMTPのセッションを作成する using (var smtp = new nMail.Smtp(AccountInfo.smtpServer)) { smtp.Port = AccountInfo.smtpPortNumber; // SMTP認証フラグが有効の時はSMTP認証を行う if (AccountInfo.smtpAuth) { // SMTPサーバに接続 smtp.Connect(); // SMTP認証を行う smtp.Authenticate(AccountInfo.userName, AccountInfo.passWord, Smtp.AuthPlain | Smtp.AuthCramMd5); } foreach (var mail in collectionMail[SEND]) { if (mail.notReadYet) { // CCが存在するとき if (mail.cc != "") { // CCの宛先を設定する smtp.Cc = mail.cc; } // BCCが存在するとき if (mail.bcc != "") { // BCCの宛先を設定する smtp.Bcc = mail.bcc; } // 添付ファイルを指定している場合 if (mail.attach != "") { smtp.FileName = mail.attach; } // 追加ヘッダをつける smtp.Header = "\r\nPriority: " + mail.priority + "\r\nX-Mailer: Akane Mail Version " + Application.ProductVersion; // 差出人のアドレスを編集する string fromAddress = AccountInfo.FromAddress; // 送信する smtp.SendMail(mail.address, fromAddress, mail.subject, mail.body); // 送信日時を設定する mail.date = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString(); // 送信済みに変更する mail.notReadYet = false; // メールの送信件数を更新する send_no++; Invoke(progressMailUpdate, send_no); // スレッドを1秒間待機させる System.Threading.Thread.Sleep(1000); } } } // プログレスバーを非表示に戻す Invoke(new ProgressMailDisableDlg(ProgressMailDisable)); // ボタンとメニューを有効化する Invoke(enableButton, 1); // ステータスバーに状況表示する labelMessage.Text = "メール送信完了"; } 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, 1); }
/// <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> /// <param name="address">宛先</param> /// <param name="cc">CCのアドレス</param> /// <param name="bcc">BCCのアドレス</param> /// <param name="subject">件名</param> /// <param name="body">本文</param> /// <param name="attach">添付メールリスト</param> /// <param name="priority">重要度</param> /// <returns>なし</returns> public void DirectSendMail(string address, string cc, string bcc, string subject, string body, string attach, string priority) { try { // ステータスバーに状況表示する labelMessage.Text = "メール送信中"; // POP before SMTPが有効の場合 if (AccountInfo.popBeforeSMTP) { try { // POP3のセッションを作成する using (var pop = new Pop3()) { // POP3への接続タイムアウト設定をする Options.EnableConnectTimeout(); // APOPを使用するときに使うフラグ pop.APop = AccountInfo.apopFlag; // POP3 over SSL/TLSフラグが有効のときはSSLを使用する if (AccountInfo.popOverSSL) { pop.SSL = Pop3.SSL3; pop.Connect(AccountInfo.popServer, AccountInfo.popPortNumber); } else { // POP3へ接続する pop.Connect(AccountInfo.popServer, AccountInfo.popPortNumber); } // POP3への認証処理を行う pop.Authenticate(AccountInfo.userName, AccountInfo.passWord); } } catch (nMail.nMailException nex) { labelMessage.Text = "エラーNo:" + nex.ErrorCode + " エラーメッセージ:" + nex.Message; return; } catch (Exception exp) { // ステータスバーに状況表示する labelMessage.Text = "エラーメッセージ:" + exp.Message; return; } } // SMTPのセッションを作成する using (var smtp = new Smtp(AccountInfo.smtpServer)) { smtp.Port = AccountInfo.smtpPortNumber; // SMTP認証フラグが有効の時はSMTP認証を行う if (AccountInfo.smtpAuth) { // SMTPサーバに接続 smtp.Connect(); // SMTP認証を行う smtp.Authenticate(AccountInfo.userName, AccountInfo.passWord, Smtp.AuthPlain | Smtp.AuthCramMd5); } // CCが存在するとき if (cc != "") { // CCの宛先を設定する smtp.Cc = cc; } // BCCが存在するとき if (bcc != "") { // BCCの宛先を設定する smtp.Bcc = bcc; } // 添付ファイルを指定している場合 if (attach != "") { smtp.FileName = attach; } // 追加ヘッダをつける smtp.Header = "\r\nPriority: " + priority + "\r\nX-Mailer: Akane Mail Version " + Application.ProductVersion; // 差出人のアドレスを編集する string fromAddress = AccountInfo.FromAddress; // 送信する smtp.SendMail(address, fromAddress, subject, body); } // ステータスバーに状況表示する labelMessage.Text = "メール送信完了"; } catch (nMail.nMailException nex) { labelMessage.Text = "エラーNo:" + nex.ErrorCode + " エラーメッセージ:" + nex.Message; return; } catch (Exception exp) { labelMessage.Text = "エラーメッセージ:" + exp.Message; return; } }
/// <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); } } } }
// メールデータの変換を行う(1.10→1.20) public int DataConvertB(string file_name) { // ファイルのバックアップ string file_name_bak = file_name.Substring(0, file_name.Length - 3) + "bak"; try { File.Copy(file_name, file_name_bak); } catch (Exception) { MessageBox.Show(file_name_bak + "が既に存在しています。\n変換済みか何らかのエラーで作られた可能性があります。", "変換エラー", MessageBoxButtons.OK, MessageBoxIcon.Stop); return(0); } // ファイルストリームを作成する FileStream stream_r = new FileStream(file_name, FileMode.Open); // ファイルストリームをストリームリーダに関連付ける StreamReader reader = new StreamReader(stream_r, Encoding.Default); // GetHederFieldとHeaderプロパティを使うためPop3クラスを作成する nMail.Pop3 pop = new nMail.Pop3(); // データを読み出す for (int i = 0; i < collectionMail.Length; i++) { // メールの件数を読み出す int n = Int32.Parse(reader.ReadLine()); // メールを取得する for (int j = 0; j < n; j++) { // 送信メールのみ必要な項目 string address = reader.ReadLine(); string subject = reader.ReadLine(); // ヘッダがurgent、normal、non-urgentのとき if (subject.Contains("urgent") == true || subject.Contains("normal") == true || subject.Contains("non-urgent") == true) { reader.Close(); stream_r.Close(); File.Delete(file_name_bak); MessageBox.Show("このメールデータは変換済みです。", "注意", MessageBoxButtons.OK, MessageBoxIcon.Stop); return(0); } // ヘッダを取得する string header = ""; string hd = reader.ReadLine(); // 区切り文字が来るまで文字列を連結する while (hd != "\x03") { header = header + hd + "\r\n"; hd = reader.ReadLine(); } // ヘッダのサイズが1バイト以上の場合 if (header.Length > 0) { // ヘッダープロパティにファイルから取得したヘッダを格納する pop.Header = header; // アドレスを取得する pop.GetHeaderField("From:"); if (pop.Field != null) { address = pop.Field; } // 件名を取得する pop.GetHeaderField("Subject:"); if (pop.Field != null) { subject = pop.Field; } } // 本文を取得する 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 = body + b + "\r\n"; // 区切り文字が検出されたときは区切り文字を取り除いてループから抜ける if (err_parse == true) { break; } b = reader.ReadLine(); } // 受信・送信日時を取得する string date = reader.ReadLine(); // sizeの値が空値(送信メール)のとき if (date == "未送信") { // 文字列に現在時刻を格納する date = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString(); } // メールサイズを取得する(送信メールは0を格納する) string size = reader.ReadLine(); // sizeの値が空値(送信メール)のとき if (size == "") { // 文字列で0を格納する size = "0"; } // UIDLを取得する(送信メールは無視) string uidl = reader.ReadLine(); // 添付ファイル名を取得する(受信メールは無視) string attach = reader.ReadLine(); // 既読・未読フラグを取得する bool notReadYet = (reader.ReadLine() == "True"); // コンバートフラグを取得する string convert = reader.ReadLine(); // メール格納配列に格納する Mail mail = new Mail(address, header, subject, body, attach, date, size, uidl, notReadYet, convert); collectionMail[i].Add(mail); } } // ストリームリーダとファイルストリームを閉じる reader.Close(); stream_r.Close(); // データファイルを削除する File.Delete(file_name); // ファイルストリームを作成する FileStream stream_w = new FileStream(file_name, FileMode.Create); // ファイルストリームをストリームライタに関連付ける StreamWriter writer = new StreamWriter(stream_w, Encoding.UTF8); // メールの件数とデータを書き込む for (int i = 0; i < collectionMail.Length; i++) { writer.WriteLine(collectionMail[i].Count.ToString()); foreach (Mail mail in collectionMail[i]) { writer.WriteLine(mail.address); writer.WriteLine(mail.subject); writer.Write(mail.header); writer.WriteLine("\x03"); writer.Write(mail.body); writer.WriteLine("\x03"); writer.WriteLine(mail.date); writer.WriteLine(mail.size); writer.WriteLine(mail.uidl); writer.WriteLine(mail.attach); writer.WriteLine(mail.notReadYet.ToString()); // CCアドレスを設定する string cc = ""; // ヘッダが存在するとき if (mail.header.Length > 0) { // ヘッダープロパティにファイルから取得したヘッダを格納する pop.Header = mail.header; // ヘッダからCCアドレスを取得する pop.GetHeaderField("Cc:"); if (pop.Field != null) { cc = pop.Field; } } // CCアドレスを書き込む writer.WriteLine(cc); // BCCを設定する(受信メールは無視) string bcc = ""; // CCアドレスを書き込む writer.WriteLine(bcc); // 重要度を取得する string priority = "normal"; // ヘッダが存在するとき if (mail.header.Length > 0) { // ヘッダから重要度を取得する priority = GetPriority(mail.header); } // 重要度を書き込む writer.WriteLine(priority); // コンバート済みのステータスを書き込む writer.WriteLine(mail.convert); } } // ストリームライタとファイルストリームを閉じる writer.Close(); stream_w.Close(); MessageBox.Show("変換が完了しました。", "完了", MessageBoxButtons.OK, MessageBoxIcon.Information); return(0); }