webmail and embedded image


http://wp.me/ph3BR-ft

 

Review :

去年, 2010年5月份, 花了兩天看文章, 實驗學習登入 Telnet, 手動連結 SMTP 伺服器, 實驗如何發EMAIL. 目的是為了收到 FAX 後, 自動發出去 EMAIL 給特定的收件人, 還半抄半寫了來兩個小工具程式.  然後忙其他事停下來了.

PCCW, email telnet test for smtp.netvigator.com
http://hk.myblog.yahoo.com/xiao-laba/article?mid=2649

YAHOO email telnet test for smtp.mail.yahoo.com
http://hk.myblog.yahoo.com/xiao-laba/article?mid=2643

Base64 encoder, 小工具
http://hk.myblog.yahoo.com/xiao-laba/article?mid=2663

Win32ASM 自己抄寫的 32位元 XP 小工具, Auto Send email
http://hk.myblog.yahoo.com/xiao-laba/article?mid=2638

在以下的論壇問了以下問題, 後來有人很熱心幫忙用 C# 寫了一個框架, 演示如何用 C# 的 fileSystemWatcher 達到相同的目的, 雖然包裝不同, 但是估計底層應是一樣的 Win32 API.

當時實驗了兩個小時, 基本上建好了, 修修改後, 終於能夠在收到 FAX 後自動發出去了, 這是 C# 寫的. 但是有一個很嚴重的 BUG, 不到半小時就發現了, 收完第一份 FAX, 緊接著的下一份就無法發出去了. 差不多放下兩個月後, 突然有一天, 靈感來了, 就試試看, 意外地, 這個阻礙實際應用的 BUG 解決了. 可惜,  以下的論壇問的以下問題和回應現在看不到了.

[問題] WIN32 API 判斷有新的檔案加入 [Newest]
http://delphi.ktop.com.tw/board.php?cid=30&fid=72&tid=100897


// copy from here http://delphi.ktop.com.tw/board.php?cid=30&fid=72&tid=100897

// code and copyright retain to original author

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;            //  fileSystemWatcher1
using System.Net.Mail;      //  MailMessage

namespace System_FileSystemWatcher
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

// Form 上請放一個 fileSystemWatcher 元件

fileSystemWatcher1.Path = "D:\\input\\";
//FileWatcher.Filter = "*.txt";
fileSystemWatcher1.Filter = "*.*";
fileSystemWatcher1.IncludeSubdirectories = true;
//fileSystemWatcher1.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
fileSystemWatcher1.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName ;

fileSystemWatcher1.Deleted += new FileSystemEventHandler(this.FileWatcher_Deleted);

fileSystemWatcher1.Renamed += new RenamedEventHandler(this.FileWatcher_Renamed);
fileSystemWatcher1.Created += new FileSystemEventHandler(this.FileWatcher_Created);
fileSystemWatcher1.Changed += new FileSystemEventHandler(this.FileWatcher_Changed);
}
private void FileWatcher_Created(object sender, System.IO.FileSystemEventArgs e)
{
//MessageBox.Show(string.Format("新增一個{0}檔案",e.Name), this.Text);
//listBox1.Items.Add(string.Format("新增一個{0}檔案", e.Name));
//if ((e.Name.IndexOf("xml\\") == -1) && (e.Name.IndexOf(".xml") == -1))
//if (e.Name.IndexOf("xml\\") == -1)
if (e.Name.IndexOf("xml") == -1)
{
listBox1.Items.Add(string.Format("新增一個{0}檔案", e.Name));
SendMailUseGmail(e.Name);
}
}
private void FileWatcher_Deleted(object sender, System.IO.FileSystemEventArgs e)
{
//MessageBox.Show(string.Format("刪除一個{0}檔案",e.Name), this.Text);
//listBox1.Items.Add(string.Format("刪除一個{0}檔案", e.Name));
//if ((e.Name.IndexOf("xml\\") == -1) && (e.Name.IndexOf(".xml") == -1))
//if (e.Name.IndexOf("xml\\") == -1)
if (e.Name.IndexOf("xml") == -1)
{
listBox1.Items.Add(string.Format("刪除一個{0}檔案", e.Name));
SendMailUseGmail(e.Name);
}
}
private void FileWatcher_Renamed(object sender, System.IO.RenamedEventArgs e)
{
//MessageBox.Show(string.Format("檔案{0}被改名為{1}",e.OldFullPath,e.FullPath), this.Text);
//listBox1.Items.Add(string.Format("改名一個{0}檔案", e.Name));
//if ((e.Name.IndexOf("xml\\") == -1) && (e.Name.IndexOf(".xml") == -1))
//if (e.Name.IndexOf("xml\\") == -1)
if (e.Name.IndexOf("xml") == -1)
{
listBox1.Items.Add(string.Format("改名一個{0}檔案", e.Name));
SendMailUseGmail(e.Name);
}
}
private void FileWatcher_Changed(object sender, System.IO.FileSystemEventArgs e)
{
//MessageBox.Show(string.Format("檔案{0}被改名為{1}",e.OldFullPath,e.FullPath), this.Text);
//listBox1.Items.Add(string.Format("更新一個{0}檔案", e.Name));
//if ((e.Name.IndexOf("xml\\") == -1) && (e.Name.IndexOf(".xml") == -1))
//if (e.Name.IndexOf("xml\\") == -1)
if (e.Name.IndexOf("xml") == -1)
{
listBox1.Items.Add(string.Format("更新一個{0}檔案", e.Name));
SendMailUseGmail(e.Name);
}
}

public void SendMailUseGmail(string eName)
{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.To.Add("abc@def.com.tw");
//msg.To.Add("b@b.com");
/*
* msg.To.Add("b@b.com");
* msg.To.Add("b@b.com");
* msg.To.Add("b@b.com");
*/
//msg.CC.Add("c@c.com");
/*
* msg.CC.Add("c@c.com");
* msg.CC.Add("c@c.com");
*/
msg.From = new MailAddress("kkk@gmail.com", "kkk@gmail.com", System.Text.Encoding.UTF8);

msg.Subject = "目錄中檔案發生異動" + eName;
msg.SubjectEncoding = System.Text.Encoding.UTF8;
msg.Body = "請檢查主機的 d:/input/" + eName;
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.IsBodyHtml = false;
msg.Priority = MailPriority.High;

SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("kkk@gmail.com", "Email的密碼");

//client.Port = 587;//Gmail Port, Success
//client.Port = 465;//Gmail Port, Failed
client.Port = 25;//Gmail Port, Success
client.Host = "smtp.gmail.com";
client.EnableSsl = true; // by SSL
object userState = msg;
try
{
// SendAsync Failed
//client.SendAsync(msg, userState);
client.Send(msg);
//MessageBox.Show("Send OK");
label1.Text = "Send Ok";
}
catch (System.Net.Mail.SmtpException ex)
{
//MessageBox.Show(ex.Message, "Failed");
label1.Text = ex.Message;
}
}

}
}

Win32 ASM, 曾經聽過某個 API, 功能是每次有新檔案加入就提供 SYSTEM NOTIFY, 這樣的構想就是從此來. 其實, 原理很簡單, 收到 FAX, 會產生一個 TIF格式的檔案, 配合 SMTP 發信功能就好. 沒選 YAHOO 或 ISP 的 SMTP, 雀屏中選的是 GMAIL, 因為實驗順利, 能用就一直用到至今, 沒有紀錄如何達成, 連原碼或如何解決BUG的注記都沒有時間寫, 今天真的要再從頭看一次, 那次修改了 C# 的原碼, 很快又忘了.

Problem :

SMTP send mail is good as long as read those redirected fax by uses PC and Linux or Windows, but some day found problem with Android device or tablet computer as they are required to download the fax to local storage before it can be view or open.

圖片最容易表達及幫助記憶遇到的問題

C# code snippet of SMTP mail attachment
result of SMTP attachment DSCN2277
result of SMTP attachment DSCN2277

Solution :

To revisit the code and perhaps include HTML capability are the most priority if we want to see the fax right after login to webmail.

embedded image of HTML encoded example DSCN2278
embedded image of HTML encoded example DSCN2278

Plan :
To learn about how to encode HTML and image in MAIL of SMTP.

GOOGLE search
http://www.google.com.tw/search?q=C%23+webmail+and+embedded+image&hl=zh-TW&client=firefox-a&hs=Hn&rls=org.mozilla:zh-TW:official&prmd=ivns&ei=mMOMTb_7CoLGvQOhxry-DQ&start=10&sa=N

看來這個會有幫助
http://www.codeproject.com/KB/IP/emailembeddedimages.aspx

http://blog.miniasp.com/post/2008/02/06/How-to-send-Email-with-embedded-picture-image.aspx

MAR-28-2011
上星期六的下午, 做完了小修改.
這是第二次碰 Visual C# express 2008 (Microsoft 可以免費下載的開發工具)
http://www.microsoft.com/express/Downloads/#2008-Visual-CS
webmail / iphone / ipad / android / PC / Maemo / Blackberry / Outlook, 測試結果都可用, 完畢.

廣告

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s