你好我已經創建了一個應用程序,將查看和看看一個工廠已運行多少小時,並旨在發送電子郵件負責跟蹤這些工廠的所有污染物的人。我的問題是,一旦我部署到機器,它在Visual Studio中工作,它給我一個錯誤。發送電子郵件與Outlook C#
如果有人可以查看代碼,並告訴我在哪裏,我有一個錯誤。非常感謝。
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();
}
}
}
這不是一個調試服務,http://stackoverflow.com/help/how-to-ask。 – Mathemats
請參閱http://stackoverflow.com/questions/16168027/how-can-i-supress-the-outlook-warning-while-sending-mail-using-macro-in-excel –
另一種選擇是SmtpClient https:// msdn.microsoft.com/en-us/library/system.net.mail.smtpclient(v=vs.110).aspx – hatchet