2012-07-31 80 views
0

我使用Visual Studio 2010中 我想寫功能,打開Outlook窗口,當用戶點擊一個按鈕,發送電子郵件編寫項目在asp.net C#。打開Outlook窗口發送電子郵件asp.net

我嘗試這樣做:

using Outlook = Microsoft.Office.Interop.Outlook; 

Outlook.Application oApp = new Outlook.Application(); 
Outlook._MailItem oMailItem = (Outlook._MailItem)oApp.CreateItem (Outlook.OlItemType.olMailItem); 
oMailItem.To = address; 
// body, bcc etc... 
oMailItem.Display (true); 

但是編譯器說有命名空間內微軟沒有命名空間局。 其實Microsoft Office包括Outlook完全安裝在我的電腦中。

我應該包括辦公室庫到Visual Studio? 問題如何解決?

+0

您是否試圖從網頁打開Outlook? – yogi 2012-07-31 09:06:59

回答

1

如果使用Microsoft.Office.Interop.Outlook,Outlook必須在服務器上安裝(並在服務器上運行,而不是在用戶計算機上)。

您是否嘗試過使用SmtpClient

System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage(); 
     using (m) 
     { 
      //sender is set in web.config: <smtp from="my alias &lt;[email protected]&gt;"> 
      m.To.Add(to); 
      if (!string.IsNullOrEmpty(cc)) 
       m.CC.Add(cc); 
      m.Subject = subject; 
      m.Body = body; 
      m.IsBodyHtml = isBodyHtml; 
      if (!string.IsNullOrEmpty(attachmentName)) 
       m.Attachments.Add(new System.Net.Mail.Attachment(attachmentFile, attachmentName)); 

      System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(); 
      try 
      { client.Send(m); } 
      catch (System.Net.Mail.SmtpException) {/*errors can happen*/ } 
     } 
+0

目前我在本地主機上運行項目,所以我的電腦不是服務器?我試過SMTP,但我想打開Outlook正如我上面 – Nurlan 2012-07-31 09:15:35

+0

是說,其實你的計算機是服務器;請記住,您必須在將部署應用程序的服務器上安裝Outlook。 – 2012-07-31 09:17:18

+0

在這種情況下,用戶如何看到服務器上運行的Outlook窗口? – Nurlan 2012-07-31 09:19:09

1

這個使用Outlook來發送電子郵件與收件人,主題和身體預裝。

<A HREF="mailto:[email protected]?subject=this is the subject&body=Hi, This is the message body">send outlook email</A> 
1

而是你試試這樣,添加使用Microsoft.Office.Interop.Outlook;參考

 Application app = new Application(); 
     NameSpace ns = app.GetNamespace("mapi"); 
     ns.Logon("Email-Id", "Password", false, true); 
     MailItem message = (MailItem)app.CreateItem(OlItemType.olMailItem); 
     message.To = "To-Email_ID"; 
     message.Subject = "A simple test message"; 
     message.Body = "This is a test. It should work"; 

     message.Attachments.Add(@"File_Path", Type.Missing, Type.Missing, Type.Missing); 

     message.Send(); 
     ns.Logoff(); 
相關問題