星期二, 6月 23, 2009

透過C#寄發mail

1.直接寄發
透過System.Net.Mail,舊的System.Web.Mail不用了
reference
MailMessage 類別


2.呼叫outlook
需加入參考「 Microsoft Outlook 11.0 Object Library」
Outlook.ApplicationClass app = new Outlook.ApplicationClass();
Outlook.MailItemClass mi = (Outlook.MailItemClass)app.CreateItem(Outlook.OlItemType.olMailItem);
mi.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
mi.To = "yurenju@gmail.com";
mi.Display(new object());


references
C# 呼叫 Outlook

3.呼叫預設email程式
private void StartDefaultClient()
{
string filename = "mailto:jismenon@yahoo.com?subject=Hello&body=Hello&cc=meera@yahoo.com&bcc=jany@yahoo.com&Attach=@C:\\test.txt";
Process myProcess = new Process();
myProcess.StartInfo.FileName = filename;
myProcess.StartInfo.UseShellExecute = true;
myProcess.StartInfo.RedirectStandardOutput = false;
myProcess.Start();
}

references
Opening default Email client from a C# Application


p.s 斷行問題
body裡有斷行時,要改用escape("\n") 或 %0A
否則會沒效果

沒有留言: