2015-04-06 175 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文章中阅读更多。

相关问题