1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Microsoft.VisualBasic;
using System.Diagnostics;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Security.Principal;
using Microsoft.Windows.Design.Interaction;
using Microsoft.CSharp;

using System.IO;
using System.Reflection;
using System.Configuration;
using MySql.Data.MySqlClient;
using System.Data.OleDb;
using System.Collections;
using System.Net.Mail;
using System.Net;
using Excel = Microsoft.Office.Interop.Excel;
using OutLook = Microsoft.Office.Interop.Outlook;
using Microsoft.Office.Interop.Outlook;
using EAGetMail;

namespace Email_ALerts
{
    public partial class frm_EmailAlert : Form
    {
        static string EmailConnStr = ConfigurationManager.ConnectionStrings["EmailAlerts"].ToString();
        static MySqlConnection objConn;
        static MySqlCommand objCmd;
        static MySqlDataAdapter objDa;
        static MySqlDataReader objDr;
        DataSet ds;
        DataTable dt;

        const string FOLDER_NAME = "spec";
        const string SAVE_FOLDER = "c:\\temp";
        private string m_strSaveFullPath = "";


        public frm_EmailAlert()
        {
            InitializeComponent();
        }

        private void ThisAddIn_Startup(object sender, EventArgs e)
        {

        }

        private void Application_ItemSend(object Item, ref bool Cancel)
        {
            OutLook.MailItem mailItem = Item as OutLook.MailItem;
            if ((mailItem != null))
            {
                if ((mailItem.Subject).ToLower().Contains(FOLDER_NAME))
                {
                    OutLook.MailItem mailItemCopy = mailItem.Copy() as OutLook.MailItem;
                    Microsoft.Office.Interop.Outlook.ApplicationClass oApp = new Microsoft.Office.Interop.Outlook.ApplicationClass();
                    Microsoft.Office.Interop.Outlook.MAPIFolder inBox = (Microsoft.Office.Interop.Outlook.MAPIFolder)oApp.GetNamespace("MAPI").GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
                    OutLook.MAPIFolder folder = null;

                    try
                    {
                        folder = inBox.Folders.Add("My Test Folder",Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
                        inBox.Folders["My Test Folder"].Display();
                    }
                    catch (System.Runtime.InteropServices.COMException ex)
                    {

                    }

                    catch (System.Exception ex)
                    {
                        Microsoft.Windows.Design.Interaction.MsgBox(ex.Message);
                    }

                    if ((folder == null))
                    {
                        folder = inBox.Folders.Add(FOLDER_NAME, OutLook.OlDefaultFolders.olFolderInbox);
                    }
                    mailItemCopy.Move(folder);
                    string strFromAddress = WindowsIdentity.GetCurrent().Name;
                    string strToAddress = mailItemCopy.To;
                    string strCCAddress = mailItemCopy.CC;
                    string strBCCAddress = mailItemCopy.BCC;
                    string strEmailFileSize = Convert.ToString(mailItemCopy.Size);
                    if (string.IsNullOrEmpty(strFromAddress))
                        strFromAddress = "";
                    if (string.IsNullOrEmpty(strToAddress))
                        strToAddress = "";
                    if (string.IsNullOrEmpty(strCCAddress))
                        strCCAddress = "";
                    if (string.IsNullOrEmpty(strBCCAddress))
                        strBCCAddress = "";
                    string strFileName = Regex.Replace(mailItemCopy.Subject, "[\\/\\\\\\:\\?\\*\\<\\>\\|]", "") + ".msg";
                    m_strSaveFullPath = SAVE_FOLDER + "\\" + strFileName;
                    SaveEmailToFile(ref mailItemCopy);
                    SaveEmailToDb(ref mailItemCopy.Subject, ref mailItemCopy.Body, ref strFileName, ref strEmailFileSize, ref strFromAddress, ref strToAddress, ref strCCAddress, ref strBCCAddress);
                    File.Delete(m_strSaveFullPath);
                }
            }

        }

        private void SaveEmailToDb(ref string strSubject, ref string strBody, ref string strMailName, ref string strEmailFileSize, ref string strFromAddress, ref string strToAddress, ref string strCCAddress, ref string strBCCAddress)
        {
            FileStream tmpFile = new FileStream(m_strSaveFullPath, FileMode.Open, FileAccess.Read);
            byte[] btSaveFile = new byte[tmpFile.Length + 1];
            tmpFile.Read(btSaveFile, 0, 0);
            tmpFile.Close();

            objConn = new MySqlConnection(EmailConnStr);
            string strSQLInsert = "INSERT INTO MailStore " + " (MailText,MailSubject,MailSavePath,MailSentDate,MailFileName,MailSentFrom,MailSentTo,MailSentCC,MailSentBCC,FileObject,FileObjectSize) " + " VALUES " + " (@Subject, @Body, @SavePath,@MailSentDate,@MailFileName,@MailSentFrom,@MailSentTo,@MailSentCC,@MailSentBCC,@File,@strEmailFileSize)";

            objCmd = new MySqlCommand(strSQLInsert, objConn);
            objCmd.Parameters.Add(new MySqlParameter("@Subject", SqlDbType.VarChar)).Value = strSubject;
            objCmd.Parameters.Add(new MySqlParameter("@Body", SqlDbType.VarChar)).Value = strBody;
            objCmd.Parameters.Add(new MySqlParameter("@SavePath", SqlDbType.VarChar)).Value = m_strSaveFullPath;
            objCmd.Parameters.Add(new MySqlParameter("@MailSentDate", SqlDbType.DateTime)).Value = DateTime.Now;
            objCmd.Parameters.Add(new MySqlParameter("@MailFileName", SqlDbType.VarChar)).Value = strMailName;
            objCmd.Parameters.Add(new MySqlParameter("@MailSentFrom", SqlDbType.VarChar)).Value = strFromAddress;
            objCmd.Parameters.Add(new MySqlParameter("@MailSentTo", SqlDbType.VarChar)).Value = strToAddress;
            objCmd.Parameters.Add(new MySqlParameter("@MailSentCC", SqlDbType.VarChar)).Value = strCCAddress;
            objCmd.Parameters.Add(new MySqlParameter("@MailSentBCC", SqlDbType.VarChar)).Value = strBCCAddress;
            objCmd.Parameters.Add(new MySqlParameter("@File", SqlDbType.VarBinary)).Value = btSaveFile;
            objCmd.Parameters.Add(new MySqlParameter("@strEmailFileSize", SqlDbType.VarChar)).Value = strEmailFileSize;

            objConn.Open();
            objCmd.ExecuteNonQuery();
            objConn.Close();
            objConn.Close(); objCmd.Dispose();
        }

        private void SaveEmailToFile(ref Microsoft.Office.Interop.Outlook.MailItem miMailItem)
        {
            if (!Directory.Exists(SAVE_FOLDER))
            {
                Directory.CreateDirectory(SAVE_FOLDER);
            }
            miMailItem.SaveAs(m_strSaveFullPath, Microsoft.Office.Interop.Outlook.OlSaveAsType.olMSG);
        }
    }
}

谁能帮助我如何使用 C#.net 将 OutLook 电子邮件保存到 MySql 数据库中?我想将我的 OutLook 邮件消息保存到 MySql 数据库中。我写了这段代码,但这不起作用,其中有一些错误。请更正此代码或将实际代码发送给我。谢谢

问候:艾哈迈德

4

0 回答 0