2012-11-02 65 views
0

當我試圖調試代碼時,在進入'添加附件'部分時,Watch 2屏幕上的ContentDisposition會給我附件的大小-1。爲什麼我的附件大小爲-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 = "[email protected]"; 
    string EmailWork = "[email protected]"; 
    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大小的任何信息?

願意提供更多信息/代碼,如果需要的話。
在此先感謝,

+0

該文件是否真的存在?我會想象它不存在或您的應用程序沒有權限訪問該文件。 –

+0

文件excist,在使用的streamwriter代碼中自動生成vCard。所以它被髮送之前已經作出 – Lobato

+0

您是否嘗試發送沒有「attach.ContentDisposition.FileName」行的郵件? –

回答

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 = "[email protected]"; 
     string EmailWork = "[email protected]"; 
     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」。

+0

因此,我必須將所有內容轉儲到一個字節數組中,並將其複製到某個內容中,以免它突然變成封閉的流? – Lobato

+0

是爲什麼它給我-1大小的解決方案? – Lobato

+0

由於流關閉了,並且某處出現異常,所以無法看到它,或者流位置關閉(設置爲最後一個字節)。我會冒險猜測,在Attachement對象中有一些異常處理,以防止「ObjectDisposedException」,這通常表明您的流已被關閉 – iMortalitySX

相關問題