0

当我尝试调试代码时,在进入“添加附件”部分时,Watch 2 屏幕中的 ContentDisposition 为我提供了附件的 -1 大小。

这使我的代码跳闸并给我错误消息:

邮件发送失败。

整个代码:

private Attachment createCardAttachment()
{
    //  Information string
    string FamilyName = "FamilyName";
    string FirstName = "FirstName";
    string MiddleName = "MiddleName";
    string Title = "Miss,Mr.";
    string Suffix = "suffix(s)";
    //  string Birthday = "YYYY-MM-DD";
    string WorkAddressStreet = "WorkAddressStreet";
    string WorkAddressCity = "WorkAddressCity";
    string WorkAddressStateProvince = "WorkAddressStateProvince";
    string WorkAddressZipPostalCode = "1234 AA";
    string WorkAddressCountry = "WorkAddressCountry";
    string HomeAddressStreet = "HomeAddressStreet";
    string HomeAddressCity = "HomeAddressCity";
    string HomeAddressStateProvince = "HomeAddressStateProvince";
    string HomeAddressZipPostalCode = "1234 AA";
    string HomeAddressCountry = "HomeAddressCountry";
    string EmailPersonal = "Person@personaldomain.com";
    string EmailWork = "Person@workdomain.com";
    string Organization = "Organization";
    string WorkPhone = "123-456-7890 ext 321";
    string WorkFax = "123-456-7890";
    string CellPhone = "123-456-7890";
    string HomePhone = "123-456-7890";
    string JobTitle = "Programmer";
    string WorkURL = "http://www.domain.com";
    string ID = Request.QueryString["ID"]; // Database QueryString

    //  Save vCard Generate Code as .vcf extension
    string path = ("C:\\local\\vCardGenerator.Website\\FirstName_LastName.vcf");

            // Test attachment send
    Attachment attach = null;

    MemoryStream ms = new MemoryStream();

    //  vCard C# Code
    using (StreamWriter sw = new StreamWriter(ms))
    {
        sw.Write("BEGIN:VCARD\r\n");
        sw.Write("VERSION:1.0\r\n");
        sw.Write("N;LANGUAGE=en-us:{0};{1};{2};{3};{4};;\r\n", FamilyName, FirstName, MiddleName, Title, Suffix);
        sw.Write("FN:{0} {1} {2} {3} {4}\r\n", Title, FirstName, MiddleName, Suffix, FamilyName);
        sw.Write("ORG:{0}\r\n", Organization, "\r\n");
        sw.Write("TEL;WORK;VOICE:{0}\r\n", WorkPhone, "\r\n");
        sw.Write("TEL;WORK;FAX:{0}\r\n", WorkFax, "\r\n");
        sw.Write("TEL;CELL;VOICE:{0}\r\n", CellPhone, "\r\n");
        sw.Write("TEL;HOME;VOICE:{0}\r\n", HomePhone, "\r\n");
        sw.Write("TITLE:{0}\r\n", JobTitle, "\r\n");
        sw.Write("ADR;INTL;PARCEL;WORK:;;{0}; {1}; {2}; {3}; {4};\r\n", WorkAddressStreet, WorkAddressCity, WorkAddressStateProvince, WorkAddressZipPostalCode, WorkAddressCountry);
        sw.Write("ADR;INTL;PARCEL;HOME:;;{0}; {1}; {2}; {3}; {4};\r\n", HomeAddressStreet, HomeAddressCity, HomeAddressStateProvince, HomeAddressZipPostalCode, HomeAddressCountry);
        sw.Write("URL;WORK:{0}\r\n", WorkURL);
        sw.Write("EMAIL;PREF;INTERNET:{0}\r\n", EmailPersonal);
        sw.Write("EMAIL;INTERNET:{0}\r\n", EmailWork);
        sw.Write("END:VCARD\r\n");


        ContentType ct = new ContentType(MediaTypeNames.Application.Octet);
        attach = new Attachment(ms, ct);
        // Goes well until here
        // Gives the attachment a size of -1
        attach.ContentDisposition.FileName = "FirstName_LastName.vcf";
        attach.Name = "FirstName_LastName.vcf";
        sw.Flush();
        sw.Close();
        ms.Close();

    }

    return attach;
}
    }
}

具体的:

// Goes well until here
// Gives the attachment a size of -1
attach.ContentDisposition.FileName = "FirstName_LastName.vcf";

任何人都可以向我提供任何信息为什么我的附件大小为-1?

如果需要,愿意提供更多信息/代码。
提前致谢,

4

1 回答 1

2

在使用附件之前,您正在关闭内存流。将其转储到一个字节数组中,然后加载到您的附件中,或者在完成之前不要关闭您的流。

即使您在 MemoryStream 关​​闭之前设置附件的数据,它仍然使用那个内存流,您需要将它复制到某个东西,这样它就不会认为它突然是一个关闭的流。

编辑:老实说,我很惊讶你没有因为尝试访问封闭的流而获得例外。您可以尝试取出 StreamWriter 上的使用并删除 sw.Close() 和 ms.Close()。您可能应该将内存流的位置重置为。

ms.Position = 0;

我只是建议您复制到字节数组,以便您可以正确处理流写入器和内存流。

EDIT2:试试这个:::

    private Attachment createCardAttachment()
    {
        // new variable for size
        long attSize = 0;

        //  Information string
        string FamilyName = "FamilyName";
        string FirstName = "FirstName";
        string MiddleName = "MiddleName";
        string Title = "Miss,Mr.";
        string Suffix = "suffix(s)";
        //  string Birthday = "YYYY-MM-DD";
        string WorkAddressStreet = "WorkAddressStreet";
        string WorkAddressCity = "WorkAddressCity";
        string WorkAddressStateProvince = "WorkAddressStateProvince";
        string WorkAddressZipPostalCode = "1234 AA";
        string WorkAddressCountry = "WorkAddressCountry";
        string HomeAddressStreet = "HomeAddressStreet";
        string HomeAddressCity = "HomeAddressCity";
        string HomeAddressStateProvince = "HomeAddressStateProvince";
        string HomeAddressZipPostalCode = "1234 AA";
        string HomeAddressCountry = "HomeAddressCountry";
        string EmailPersonal = "Person@personaldomain.com";
        string EmailWork = "Person@workdomain.com";
        string Organization = "Organization";
        string WorkPhone = "123-456-7890 ext 321";
        string WorkFax = "123-456-7890";
        string CellPhone = "123-456-7890";
        string HomePhone = "123-456-7890";
        string JobTitle = "Programmer";
        string WorkURL = "http://www.domain.com";
        string ID = Request.QueryString["ID"]; // Database QueryString

        //  Save vCard Generate Code as .vcf extension
        string path = ("C:\\local\\vCardGenerator.Website\\FirstName_LastName.vcf");

        // Test attachment send
        Attachment attach = null;

        MemoryStream actualAttachment = new MemoryStream();

        //  vCard C# Code
        using (MemoryStream ms = new MemoryStream())
        using (StreamWriter sw = new StreamWriter(ms))
        {
            sw.Write("BEGIN:VCARD\r\n");
            sw.Write("VERSION:1.0\r\n");
            sw.Write("N;LANGUAGE=en-us:{0};{1};{2};{3};{4};;\r\n", FamilyName, FirstName, MiddleName, Title, Suffix);
            sw.Write("FN:{0} {1} {2} {3} {4}\r\n", Title, FirstName, MiddleName, Suffix, FamilyName);
            sw.Write("ORG:{0}\r\n", Organization, "\r\n");
            sw.Write("TEL;WORK;VOICE:{0}\r\n", WorkPhone, "\r\n");
            sw.Write("TEL;WORK;FAX:{0}\r\n", WorkFax, "\r\n");
            sw.Write("TEL;CELL;VOICE:{0}\r\n", CellPhone, "\r\n");
            sw.Write("TEL;HOME;VOICE:{0}\r\n", HomePhone, "\r\n");
            sw.Write("TITLE:{0}\r\n", JobTitle, "\r\n");
            sw.Write("ADR;INTL;PARCEL;WORK:;;{0}; {1}; {2}; {3}; {4};\r\n", WorkAddressStreet, WorkAddressCity, WorkAddressStateProvince, WorkAddressZipPostalCode, WorkAddressCountry);
            sw.Write("ADR;INTL;PARCEL;HOME:;;{0}; {1}; {2}; {3}; {4};\r\n", HomeAddressStreet, HomeAddressCity, HomeAddressStateProvince, HomeAddressZipPostalCode, HomeAddressCountry);
            sw.Write("URL;WORK:{0}\r\n", WorkURL);
            sw.Write("EMAIL;PREF;INTERNET:{0}\r\n", EmailPersonal);
            sw.Write("EMAIL;INTERNET:{0}\r\n", EmailWork);
            sw.Write("END:VCARD\r\n");


            ContentType ct = new ContentType(MediaTypeNames.Application.Octet);
            sw.Flush();

            attSize = ms.Length;

            //Added position set to makesure copy works right...
            ms.Position = 0;

            //Copy to the stream you plan to use
            ms.CopyTo(actualAttachment);
            actualAttachment.Position = 0;
        }

        attach = new Attachment(actualAttachment, ct);

        //Try to set the attachment size now
        attach.ContentDisposition.Size = attSize;

        attach.ContentDisposition.FileName = "FirstName_LastName.vcf";
        attach.Name = "FirstName_LastName.vcf";

        return attach;
    }

EDIT3:我现在看到您解决了自己的问题。我为您未来的编码提供了另外一件事。这是一种扩展方法,您可以将其与任何版本的 .NET 一起使用,并且与框架版本无关

public static class StreamExtensions
{
    public static void CopyToEx(this Stream fromStream, Stream toStream)
    {
        int read = 0;
        byte[] buffer = new byte[1024];
        while((read = fromStream.Read(buffer, 0, 1024) != 0)
        {
            toStream.Write(buffer, 0, read);
        }

        toStream.Position = 0;
    }
}

这样,您可以将扩展方法添加到任何程序中,或将其放入“工具包”中以备将来使用。那么“CopyTo”不存在也没关系,你仍然可以用同样的方式在代码中调用“CopyToEx”。

于 2012-11-02T14:57:45.393 回答