【问题标题】:Error :Could not load file or assembly 'DocumentFormat.OpenXml,错误:无法加载文件或程序集'DocumentFormat.OpenXml,
【发布时间】:2016-12-23 19:23:17
【问题描述】:

我得到这个错误是 VS2015

附加信息:无法加载文件或程序集 'DocumentFormat.OpenXml,版本=2.5.5631.0,文化=中性, PublicKeyToken=31bf3856ad364e35' 或其依赖项之一。这 定位程序集的清单定义与程序集不匹配 参考。 (HRESULT 异常:0x80131040)

我正在尝试在 C# 中发送带有数据库表附件的邮件

这里是我的代码 id

using ClosedXML.Excel;
using MySql.Data.MySqlClient;
using System;
using System.Data;
using System.IO;
using System.Net.Mail;
using System.Windows.Forms;

namespace Email
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        String MyConString = "SERVER=10.0.30.125;" + "DATABASE=test;" + "UID=root;" + "PASSWORD=asterisk;" + "Convert Zero Datetime = True";        
        private void button1_Click(object sender, EventArgs e)
        {
            DataTable dt = GetData();
            dt.TableName = "data";
            using (XLWorkbook wb = new XLWorkbook())
            {
                wb.Worksheets.Add(dt);
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    wb.SaveAs(memoryStream);
                    byte[] bytes = memoryStream.ToArray();
                    memoryStream.Close();
                    using (MailMessage Msg = new MailMessage())
                    {
                        Msg.From = new MailAddress("my@mail.com");
                        Msg.To.Add("to@mail.com");
                        Msg.Subject = "DATA";
                        Msg.Body = "Excel Attachment";
                        Msg.Attachments.Add(new Attachment(new MemoryStream(bytes), "DATA.xlsx"));
                        Msg.IsBodyHtml = true;
                        SmtpClient smtp = new SmtpClient();
                        smtp.Host = "smtp.gmail.com";
                        smtp.EnableSsl = true;
                        System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();
                        credentials.UserName = "my@mail.com";
                        credentials.Password = "password";
                        smtp.UseDefaultCredentials = true;
                        smtp.Credentials = credentials;
                        smtp.Port = 587;
                        smtp.Send(Msg);
                        MessageBox.Show("MAIL SENT SUCCESSFULLY TO " + txtTo.Text);
                        txtTo.Text = "";
                    }
                }
            }
        }
        private DataTable GetData()
        {
            using (MySqlConnection conn = new MySqlConnection(MyConString))
            {
                using (MySqlCommand cmd = new MySqlCommand("SELECT * from table;", conn))
                using (MySqlDataAdapter sda = new MySqlDataAdapter())
                {
                    cmd.Connection = conn;
                    sda.SelectCommand = cmd;
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        return dt;
                    }
                }
            }
        }
    }
}

我将它添加到我的 AddReference 中

ClosedXml.dll
DocumentFormat.OpenXml.dll
MySql.Data.dll

帮助解决我的问题。为什么会出现这个错误?

【问题讨论】:

    标签: c# mysql email


    【解决方案1】:

    下载并安装 DocumentFormat.OpenXml 2.5 版https://www.microsoft.com/en-us/download/details.aspx?id=30425

    【讨论】:

    猜你喜欢
    • 2014-12-01
    • 2014-10-08
    • 1970-01-01
    • 2010-10-07
    • 2023-03-05
    • 1970-01-01
    • 2018-09-14
    • 2013-02-23
    相关资源
    最近更新 更多