使用outlook C#发送电子邮件

时间:2015-04-06 22:53:33

标签: c# email outlook

你好我已经创建了一个应用程序,它将查看并查看工厂运行了多少小时,并且旨在向负责跟踪这些工厂的所有污染物的人发送电子邮件。我的问题是,虽然它部署到机器后它在Visual Studio中工作,但它给了我一个错误。

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 = "whoever@whatever.com";
                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 个答案:

答案 0 :(得分:2)

首先,我注意到以下几行代码:

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

您需要使用Outlook Application类的现有实例来调用Quit方法,而不是创建一个新方法。

  

我的问题是,当我在Visual Studio中部署到机器时,它会发生错误。

您会收到标准的安全提示。在此上下文中的“安全性”是指所谓的“对象模型防护”,它触发安全提示并阻止对某些功能的访问,以防止恶意程序从Outlook数据中收集电子邮件地址并使用Outlook传播病毒和垃圾邮件。不能简单地关闭这些提示。有三种主要方法可以避免这种提示:

  1. Security Manager for Outlook组件允许在运行时关闭/打开提示。

  2. 使用不生成安全提示的低级代码。或围绕该API的任何其他第三方包装(例如,Redemption)。

  3. 运行最新的防病毒软件。

  4. 您可以在Outlook "Object Model Guard" Security Issues for Developers文章中详细了解相关内容。