Skip to content

Instantly share code, notes, and snippets.

@yuxxxx
Forked from hijirichan/OpenMail.cs
Last active August 29, 2015 13:56
Show Gist options
  • Save yuxxxx/9170183 to your computer and use it in GitHub Desktop.
Save yuxxxx/9170183 to your computer and use it in GitHub Desktop.
string ReadEscapedHtml(string file, Header header)
{
using (var sr = new StreamReader(Application.StartupPath + @"\tmp\" + file, Encoding.Default)) {
string htmlBody = sr.ReadToEnd();
return Mail.HtmlToText(htmlBody, header);
}
}
string ReplaceLineBreak(string text)
{
if (!attach.Body.Contains("\n\n")) {
return text;
}
else {
return text.Replace("\n\n", "\r\n").Replace("\n", "\r\n");
}
}
void ListAttaches(string attaches)
{
string[] attachFileNameList = attaches.Split(',');
for (int i = 0; i < attachFileNameList.Length; i++) {
var attachFile = attachFileNameList[i];
if (File.Exists(attachFile)) {
appIcon = System.Drawing.Icon.ExtractAssociatedIcon(attachFile);
buttonAttachList.DropDownItems.Add(attachFile, appIcon.ToBitmap());
}
else {
buttonAttachList.DropDownItems.Add(attachFile + "は削除されています。");
buttonAttachList.DropDownItems[i].Enabled = false;
}
}
}
void ResetAttachMenu()
{
buttonAttachList.DropDownItems.Clear();
buttonAttachList.Visible = false;
attachMenuFlag = false;
attachMailReplay = false;
attachMailBody = "";
}
/// <summary>
/// 指定されたメールを開く
/// </summary>
/// <param name="mail">メール</param>
private void OpenMail(Mail mail)
{
Icon appIcon;
ResetAttachMenu();
var attach = new nMail.Attachment();
bool htmlMail = attach.GetHeaderField("Content-Type:", mail.header).Contains("text/html");
checkNotYetReadMail = mail.notReadYet;
attach.Path = Application.StartupPath + @"\tmp";
int id = attach.GetId(mail.header);
// 添付ファイルが存在する場合(存在しない場合は-1が返る)
// もしくは HTML メールの場合
if (id != nMail.Attachment.NoAttachmentFile || htmlMail) {
try {
// 旧バージョンからの変換データではないとき
if (mail.convert == "") {
Options.EnableDecodeBody();
}
else {
Options.DisableDecodeBodyText();
}
attach.Add(mail.header, mail.body);
attach.Save();
}
catch (Exception ex) {
labelMessage.Text = String.Format("エラー メッセージ:{0:s}", ex.Message);
return;
}
attachMailReplay = true;
// IE コンポーネントを使用かつ HTML パートを保存したファイルがある場合
if (AccountInfo.bodyIEShow && attach.HtmlFile != "") {
this.textBody.Visible = false;
this.browserBoday.Visible = true;
if (!htmlMail) {
attachMailBody = attach.Body;
}
else {
attachMailBody = ReadEscapedHtml(attach.HtmlFile, mail.header);
}
browserBody.AllowNavigation = true;
browserBody.Navigate(attach.Path + @"\" + attach.HtmlFile);
}
else {
this.browserBody.Visible = false;
this.textBody.Visible = true;
// IE コンポーネントを使用せず、HTML メールで HTML パートを保存したファイルがある場合
if (htmlMail && !AccountInfo.bodyIEShow && attach.HtmlFile != "") {
string escaped = ReadEscapedHtml(attach.HtmlFile, mail.header);
attachMailBody = escaped;
this.textBody.Text = escaped;
}
else if (attach.Body != "") {
this.textBody.Text = ReplaceLineBreak(attach.Body);
}
else {
this.textBody.Text = mail.body;
}
}
if (attach.FileNameList != null) {
if (!AccountInfo.bodyIEShow || attach.HtmlFile == "" || attach.FileNameList.Length > 1) {
buttonAttachList.Visible = true;
attachMenuFlag = true;
// メニューに添付ファイルの名前を追加する
// IE コンポーネントありで、添付ファイルが HTML パートを保存したファイルはメニューに表示しない
foreach (var attachFile in attach.FileNameList.Where(a => a != attach.HtmlFile || !Mail.bodyIEShow)) { // ここの条件がどうやらおかしい
appIcon = System.Drawing.Icon.ExtractAssociatedIcon(Application.StartupPath + @"\tmp\" + attachFile);
buttonAttachList.DropDownItems.Add(attachFile, appIcon.ToBitmap());
}
}
}
}
else {
// 添付ファイルが存在しない通常のメールまたは
// 送信済みメールのときは本文をテキストボックスに表示する
this.browserBody.Visible = false;
this.textBody.Visible = true;
if (mail.attach != "") {
buttonAttachList.Visible = true;
ListAttaches(mail.attach);
}
bool base64Mail = attach.GetDecodeHeaderField("Content-Transfer-Encoding:", mail.header).Contains("base64");
if (base64Mail) {
Options.EnableDecodeBody();
attach.Add(mail.header, mail.body);
attach.Save();
this.textBody.Text = ReplaceLineBreak(attach.Body);
}
else {
// ISO-2022-JPでかつquoted-printableがある場合(nMail.dllが対応するまでの暫定処理)
if (attach.GetHeaderField("Content-Type:", mail.header).ToLower().Contains("iso-2022-jp")
&& attach.GetHeaderField("Content-Transfer-Encoding:", mail.header).Contains("quoted-printable")) {
Options.EnableDecodeBody();
attach.Add(mail.header, mail.body);
attach.Save();
this.textBody.Text = ReplaceLineBreak(attach.Body);
}
else if (attach.GetHeaderField("X-NMAIL-BODY-UTF8:", mail.header).Contains("8bit")) {
// Unicode化されたUTF-8文字列をデコードする
var bs = mail.body.Select(c => (byte)c).ToArray();
attachMailBody = Encoding.UTF8.GetString(bs);
this.textBody.Text = attachMailBody;
}
else {
// テキストボックスに出力する文字コードをJISに変更する
byte[] b = Encoding.GetEncoding("iso-2022-jp").GetBytes(mail.body);
string strBody = Encoding.GetEncoding("iso-2022-jp").GetString(b);
// 本文をテキストとして表示する
this.textBody.Text = strBody;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment