當我試圖調試代碼時,在進入'添加附件'部分時,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大小的任何信息?
願意提供更多信息/代碼,如果需要的話。
在此先感謝,
該文件是否真的存在?我會想象它不存在或您的應用程序沒有權限訪問該文件。 –
文件excist,在使用的streamwriter代碼中自動生成vCard。所以它被髮送之前已經作出 – Lobato
您是否嘗試發送沒有「attach.ContentDisposition.FileName」行的郵件? –