2015-04-06 234 views
0

你好我已經創建了一個應用程序,將查看和看看一個工廠已運行多少小時,並旨在發送電子郵件負責跟蹤這些工廠的所有污染物的人。我的問題是,一旦我部署到機器,它在Visual Studio中工作,它給我一個錯誤。發送電子郵件與Outlook C#

enter image description here

如果有人可以查看代碼,並告訴我在哪裏,我有一個錯誤。非常感謝。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Data.OleDb; 
using Outlook = Microsoft.Office.Interop.Outlook; 
using Microsoft.Office.Core; 
using System.Net; 
using System.Net.Mail; 
using System.Diagnostics; 

namespace RRHoursMgmt 
{ 
    public partial class PlantHoursLookup : Form 
    { 

     string conn_String = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Y:\\ NotTHISNAME.accdb; Persist Security Info= False"; 
     string error_msg = ""; 
     string q = ""; 

     OleDbConnection conn = null; 
     public PlantHoursLookup() 
     { 
      InitializeComponent(); 
     } 

     private void exitToolStripMenuItem_Click(object sender, EventArgs e) 
     { 
      Exitt(); 
     } 

     private void connectToolStripMenuItem_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       conn = new OleDbConnection(conn_String); 
       conn.Open(); 
       connectToolStripMenuItem.Text = "\u221A Connected"; 


       //disToolStripMenuItem.Enabled = true; 
       //connectToolStripMenuItem.Enabled = false; 
      } 
      catch (System.Exception ex) 
      { 


      } 
      conn.Close(); 
     } 

     private void disToolStripMenuItem_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       conn.Close(); 
       connectToolStripMenuItem.Text = "Connect"; 

      } 
      catch (System.Exception ex) 
      { 
       error_msg = ex.Message; 
       MessageBox.Show(error_msg);    

      } 

     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      connectToolStripMenuItem.PerformClick(); 
      bool isOpen = isOutlookOpen(); // asks if outlook is open returns true or false 
      bool shouldWeCloseOutlook = false; // changes to true if we open outlook 
      if (isOpen != true) 
      { 
       openOutlook(); 
       shouldWeCloseOutlook = true; 
      } 
      run_Query(); 
      if (shouldWeCloseOutlook) 
      { 
       System.Threading.Thread.Sleep(10000); 
       closeOutlook(); 
      } 
      Exitt(); 
     } 

     private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
     { 
      disToolStripMenuItem.PerformClick(); 
     } 
     private void run_Query() 
     { 
      error_msg = ""; 
      q = QueryBox.Text; 
      try 
      { 
       OleDbCommand cmd = new OleDbCommand(q, conn); 
       OleDbDataAdapter da = new OleDbDataAdapter(cmd); 
       int i = 0; 
       DataTable dt = new DataTable(); 
       da.SelectCommand = cmd; 
       da.Fill(dt); 
       Results.DataSource = dt; 
       Results.AutoResizeColumns(); 
       int rowCount = dt.Rows.Count; 
       Outlook.Application app = new Outlook.Application(); 
       Outlook.MailItem mi = app.CreateItem(Outlook.OlItemType.olMailItem); 

       string body = ""; 
       mi.Subject = "Weekly Plant Hours"; 
       mi.To = "[email protected]"; 
       if (rowCount!= 0) 
       { 
        //building body string 
        body = "this person, These Plants are over 400 hours:" + Environment.NewLine; 
        for (i = 0; i < rowCount-1; i++) 
        { 
         body = body + dt.Rows[i][0] + " " + dt.Rows[i][1] + " Hours"; 
         body = body + Environment.NewLine;      

        } 

       } 
       else 
       { 
        body = body + "this person, There Are no Plants over 400 hours!"; 
       } 
       mi.Body = body.ToString(); 

       mi.Display(false); 
       mi.Send(); 
      } 
      catch (System.Exception ex) 
      { 
       error_msg = ex.Message; 
       MessageBox.Show(error_msg);     
      } 
     } 
     private void runQueryToolStripMenuItem_Click(object sender, EventArgs e) 
     { 
      this.Cursor = Cursors.WaitCursor; 
      run_Query(); 
      this.Cursor = Cursors.Default; 
     } 
     private void Exitt() 
     { 
      System.Windows.Forms.Application.Exit(); 
     } 
     private bool isOutlookOpen() 
     { 
      Process[] pName = Process.GetProcessesByName("OUTLOOK"); 
      if (pName.Length == 0) 
      { 
       return false; 
      } 
      return true; 

     } 
     private void openOutlook() 
     { 
      Outlook.Application olook = new Outlook.Application(); 

     } 

     private void closeOutlook() 
     { 
      Outlook.Application oLook = new Outlook.Application(); 
      oLook.Quit(); 

     } 
    } 

} 
+1

這不是一個調試服務,http://stackoverflow.com/help/how-to-ask。 – Mathemats

+0

請參閱http://stackoverflow.com/questions/16168027/how-can-i-supress-the-outlook-warning-while-sending-mail-using-macro-in-excel –

+2

另一種選擇是SmtpClient https:// msdn.microsoft.com/en-us/library/system.net.mail.smtpclient(v=vs.110).aspx – hatchet

回答

2

首先,我注意到下面的代碼行:

Outlook.Application oLook = new Outlook.Application(); 
oLook.Quit(); 

您需要使用Outlook應用程序類的現有實例調用Quit方法,而不是創建一個新的。

我的問題是,雖然它在Visual Studio中的工作,一旦我部署到機器,它給了我一個錯誤。

您將得到標準的安全提示。在這種情況下,「安全」是指觸發安全提示並阻止訪問某些功能的所謂「對象模型後衛」,以防止惡意程序從Outlook數據中收集電子郵件地址並使用Outlook傳播病毒和垃圾郵件。這些提示不能簡單地關閉。有三種主要的方法來避免這樣的提示:

  1. Security Manager for Outlook組件允許把在運行時提示斷開/接通。

  2. 使用不生成安全提示的低級代碼。或者圍繞該API的任何其他第三方包裝(例如,贖回)。

  3. 運行最新的防病毒軟件。

您可以在Outlook "Object Model Guard" Security Issues for Developers文章中閱讀更多。

相關問題