电子邮件附件中的C#Base64

时间:2018-08-06 12:35:05

标签: c#

我知道我陷入了一个愚蠢的问题...现在我充满希望,希望您能为我提供帮助。简单情况:我需要从数据库发送带有附件的电子邮件。我自己完成的,但是...我不知道为什么,但是电子邮件中发送的附件不正确。宝贵的附件内容代码,即我从数据库中获得的内容。这是代码:

static void IncomeMessage()
        {   
            SqlConnection conn = new SqlConnection("Data Source=xxx;Initial Catalog=xxx;User ID=xxx;Password=xxx");
            SqlCommand cmd = new SqlCommand("Here is my SQL SELECT", conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd); 
            DataTable dt = new DataTable();
            da.Fill(dt);

            foreach (DataRow dr in dt.Rows)
            {
                try
                {
                    var AID = dr["ID"].ToString();
                    var Ids = dr["MessageID"].ToString();
                    var SuperOrganizationTitle = dr["SuperOrganizationTitle"].ToString();
                    var Date = dr["Date"].ToString();
                    var Title = dr["Title"].ToString();
                    var Description = dr["Description"].ToString();
                    var DBFile = dr["File"].ToString();
                    var Filename = dr["FileName"].ToString();

                    MailMessage oMail = new MailMessage("test@test.com", "guntisvindels@gmail.com", "New message with theme: " + Title, "Message Income: " + Date + " From " + SuperOrganizationTitle + ". Message Content: " + Description);

                     DBFile = GetBase64(DBFile);

                     var bytes = Encoding.ASCII.GetBytes(DBFile);
                     MemoryStream strm = new MemoryStream(bytes);        
                     Attachment data = new Attachment(strm, Filename);
                     ContentDisposition disposition = data.ContentDisposition;
                     data.ContentId = Filename;
                     data.ContentDisposition.Inline = true;
                     oMail.Attachments.Add(data);

                    SmtpClient oSmtp = new SmtpClient();
                    SmtpClient oServer = new SmtpClient("test.test.com");

                    oServer.Send(oMail);
                }
                catch (Exception ex)
                {
                    // TraceAdd("Error=" + ex.Message.ToString());
                }

            }
        }

要从数据库“文件”字段中删除不必要的数据(标签),请使用此代码

public static string GetBase64(string str)
{
    var index1 = str.IndexOf("<content>");
    index1 = index1 + "<content>".Length;
    var index2 = str.IndexOf("</content>");
    var kek = Slice(str, index1, index2);
    return kek;
}

public static string Slice(string source, int start, int end)
{
    if (end < 0)
    {
        end = source.Length + end;
    }
    int len = end - start;
    return source.Substring(start, len);
}

当我启动代码时,我可以向我发送电子邮件,并且我收到了。但是邮件内容带有数据库文件字段中我的Base64代码的附件。这是一个问题-我在哪里弄错了,我该如何接收带有已写入数据库的正确附件的电子邮件。谢谢您的关注

由于P2S集成平台可以正确显示数据库中的附件,因此附件正确写入

以下是数据库在“文件”列(DBFile)中包含的示例

<file><name>zinojums.html</name><content>PGh0bWw+PGhlYWQ+PG1ldGEgaHR0cC1lcXVpdj0iQ29udGVudC1UeXBlIiBjb250ZW50PSJ0ZXh0L2h0bWw7IGNoYXJzZXQ9dXRmLTgiIC8+PHN0eWxlPmh0bWwsYm9keXtmb250LWZhbWlseTpBcmlhbCxIZWx2ZXRpY2Esc2Fucy1zZXJpZjtmb250LXNpemU6MTBwdDt9aDF7Zm9udC1zaXplOjE0cHQ7fWgye2ZvbnQtc2l6ZToxMnB0O31we2JhY2tncm91bmQtY29sb3I6I0Y3RUVEQTttYXJnaW46MDtwYWRkaW5nOjhweDt9aHJ7aGVpZ2h0OjFweDsgYm9yZGVyOjAgbm9uZTsgY29sb3I6ICM1RjlCRDQ7IGJhY2tncm91bmQtY29sb3I6ICM1RjlCRDQ7fTwvc3R5bGU+PC9oZWFkPjxib2R5PjxoMT5JbmZvcm3EgWNpamEgbm8gVmFsc3RzIGllxYbEk211bXUgZGllbmVzdGE8L2gxPjxoMj5Eb2t1bWVudHMgcGllxYZlbXRzPC9oMj48cD5Ob2Rva8S8dSBtYWtzxIF0xIFqYSBOci4gPGI+TEFUVklKQVMgVkFMU1RTIFJBRElPIFVOIFRFTEVWxKpaSUpBUyBDRU5UUlMgQVMgKDQwMDAzMDExMjAzKTwvYj4gaWVzbmllZ3RhaXMgZG9rdW1lbnRzICI8Yj5aacWGYXMgcGFyIGRhcmJhIMWGxJNtxJNqaWVtPC9iPiIgTnIuIDxiPjU1NDYyNTY5PC9iPiBwaWXFhmVtdHMgdW4gaWVrxLxhdXRzIFZJRCBkYXR1YsSBesSTLjxiciAvPjwvcD48IS0tY29udGVudC0tPjxociAvPsWgaXMgZS1wYXN0cyBpciBpenZlaWRvdHMgYXV0b23EgXRpc2tpLCBsxatkemFtIHV6IHRvIG5lYXRiaWxkxJN0LjxiciAvPlBpZXNsxJNndGllcyBWSUQgRWxla3Ryb25pc2vEgXMgZGVrbGFyxJPFoWFuYXMgc2lzdMSTbWFpOiA8YSBocmVmPSJodHRwczovL2Vkcy52aWQuZ292Lmx2Ij5lZHMudmlkLmdvdi5sdjwvYT4uPC9ib2R5PjwvaHRtbD4=</content></file>

1 个答案:

答案 0 :(得分:1)

现在,您用于从字符串创建二进制数据的方法没有考虑到字符串是经过base64编码的事实。

您需要更换

var bytes = Encoding.ASCII.GetBytes(DBFile);

var bytes = Convert.FromBase64String(DBFile);