使用 C# 将电子邮件附件以二进制形式保存到数据库中

发布于 2024-11-27 18:02:55 字数 3171 浏览 1 评论 0原文

我可以从 Exchange Server 2003 (MAPI.Attachment) 获取电子邮件附件。如何将 pdf 附件文件以二进制形式保存到数据库中?

这里有一些代码可以更好地理解。出于测试目的,我将附件 pdf 文件保存到文件系统中。我如何将其保存到数据库中?或者如何将其转换为字节数组?另外,我怎样才能获得文件的大小,因为当我声明字节数组“fileData”时我需要它......

byte[] fileData = new byte[10000];
                string[] fileTokens = new string[2];
                string[] result = new string[3];
                message.Unread = false;
                emailSubject = message.Subject.ToString();
                emailBody = message.Text.ToString();
                MAPI.Attachments test = null;
                test = (MAPI.Attachments)message.Attachments;
                int attachmentCount = (int)test.Count;
                for (int loopCounter = 1; loopCounter <= attachmentCount; loopCounter++)
                {
                    MAPI.Attachment test2 = (MAPI.Attachment)test.get_Item(loopCounter);
                    bool temp = (test2.Name.ToString().Contains(".pdf") && test2.Name.ToString().IndexOf('Q') == 0);
                    if (test2.Name.ToString().Contains(".pdf") && test2.Name.ToString().IndexOf('Q') == 0)
                    {
                        //test2.ReadFromFile(fileData);
                        test2.WriteToFile("d:\\data\\" + test2.Name);
                        PDFParser pdfParser = new PDFParser();
                        pdfParser.ReadPdfFile("d:\\data\\" + test2.Name, result);
                        sentTime = (DateTime)message.TimeSent;
                        string fileName = (string)test2.Name;
                        fileTokens = fileName.Split('.');
                    }
                    RequestHistorySet historySet = new RequestHistorySet(1, sentTime, fileData, fileTokens[1]);
                    bool res = historySet.Update();

                    message.Unread = false;
                    message.Update();

这是来自historySet类的更新函数

public bool Update()
{
    using (SqlConnection mySqlConnection = ...))
    {
        // Set up the Command object
        SqlCommand myCommand = new SqlCommand("CONNECTION STRING..", mySqlConnection);


        // Set up the OriginalName parameter
        SqlParameter prmId = new SqlParameter("@id", SqlDbType.Int);
        prmId.Value = id;
        myCommand.Parameters.Add(prmId);

        SqlParameter prmRequsetDate = new SqlParameter("@requestDate", SqlDbType.DateTime);
        prmRequsetDate.Value = requestDate;
        myCommand.Parameters.Add(prmRequsetDate);

        // Set up the FileData parameter
        SqlParameter prmFileData = new SqlParameter("@uplodedQuote_File ", SqlDbType.VarBinary);
        prmFileData.Value = fileData;
        prmFileData.Size = fileData.Length;
        myCommand.Parameters.Add(prmFileData);


        SqlParameter prmFileExtension = new SqlParameter("@uplodedQuote_Ext", SqlDbType.NVarChar);
        prmFileExtension.Value = fileExtension;
        myCommand.Parameters.Add(prmFileExtension);

        // Execute the command, and clean up.
        mySqlConnection.Open();
        bool result = myCommand.ExecuteNonQuery() > 0;
        mySqlConnection.Close();

        return result;
    }
}

I can get email attachments from exchange server 2003 (MAPI.Attachment). How can I save the pdf attachment file as binary into the database?

Here's some code to understand better. For testing purposes I am saving the attachment pdf file into a filesystem. How ca I go about saving that into a database? Or how can I convert that into a byte array? Also, how can I get the size of the file since I need that when I declare the byte array "fileData"...

byte[] fileData = new byte[10000];
                string[] fileTokens = new string[2];
                string[] result = new string[3];
                message.Unread = false;
                emailSubject = message.Subject.ToString();
                emailBody = message.Text.ToString();
                MAPI.Attachments test = null;
                test = (MAPI.Attachments)message.Attachments;
                int attachmentCount = (int)test.Count;
                for (int loopCounter = 1; loopCounter <= attachmentCount; loopCounter++)
                {
                    MAPI.Attachment test2 = (MAPI.Attachment)test.get_Item(loopCounter);
                    bool temp = (test2.Name.ToString().Contains(".pdf") && test2.Name.ToString().IndexOf('Q') == 0);
                    if (test2.Name.ToString().Contains(".pdf") && test2.Name.ToString().IndexOf('Q') == 0)
                    {
                        //test2.ReadFromFile(fileData);
                        test2.WriteToFile("d:\\data\\" + test2.Name);
                        PDFParser pdfParser = new PDFParser();
                        pdfParser.ReadPdfFile("d:\\data\\" + test2.Name, result);
                        sentTime = (DateTime)message.TimeSent;
                        string fileName = (string)test2.Name;
                        fileTokens = fileName.Split('.');
                    }
                    RequestHistorySet historySet = new RequestHistorySet(1, sentTime, fileData, fileTokens[1]);
                    bool res = historySet.Update();

                    message.Unread = false;
                    message.Update();

And here's the Update function from historySet Class

public bool Update()
{
    using (SqlConnection mySqlConnection = ...))
    {
        // Set up the Command object
        SqlCommand myCommand = new SqlCommand("CONNECTION STRING..", mySqlConnection);


        // Set up the OriginalName parameter
        SqlParameter prmId = new SqlParameter("@id", SqlDbType.Int);
        prmId.Value = id;
        myCommand.Parameters.Add(prmId);

        SqlParameter prmRequsetDate = new SqlParameter("@requestDate", SqlDbType.DateTime);
        prmRequsetDate.Value = requestDate;
        myCommand.Parameters.Add(prmRequsetDate);

        // Set up the FileData parameter
        SqlParameter prmFileData = new SqlParameter("@uplodedQuote_File ", SqlDbType.VarBinary);
        prmFileData.Value = fileData;
        prmFileData.Size = fileData.Length;
        myCommand.Parameters.Add(prmFileData);


        SqlParameter prmFileExtension = new SqlParameter("@uplodedQuote_Ext", SqlDbType.NVarChar);
        prmFileExtension.Value = fileExtension;
        myCommand.Parameters.Add(prmFileExtension);

        // Execute the command, and clean up.
        mySqlConnection.Open();
        bool result = myCommand.ExecuteNonQuery() > 0;
        mySqlConnection.Close();

        return result;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南薇 2024-12-04 18:02:55

到目前为止,这就是我所做的。我将其保存到文件系统,然后从那里读取。

test2.WriteToFile("d:\\data\\" + test2.Name);
fileData = FileToByteArray("d:\\data\\" + test2.Name);
PDFParser pdfParser = new PDFParser();
pdfParser.ReadPdfFile("d:\\data\\" + test2.Name, result);
TryToDelete("d:\\data\\" + test2.Name);
sentTime = (DateTime)message.TimeSent;
string fileName = (string)test2.Name;
fileTokens = fileName.Split('.');
historySet = new RequestHistorySet(1, sentTime, fileData, fileTokens[1]);

As of now this is what I have done. I save it to a filesystem and then read from there.

test2.WriteToFile("d:\\data\\" + test2.Name);
fileData = FileToByteArray("d:\\data\\" + test2.Name);
PDFParser pdfParser = new PDFParser();
pdfParser.ReadPdfFile("d:\\data\\" + test2.Name, result);
TryToDelete("d:\\data\\" + test2.Name);
sentTime = (DateTime)message.TimeSent;
string fileName = (string)test2.Name;
fileTokens = fileName.Split('.');
historySet = new RequestHistorySet(1, sentTime, fileData, fileTokens[1]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文