当我尝试调试代码时,在进入“添加附件”部分时,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 ??
如果需要,愿意提供更多信息/代码 在此先感谢,
答案 0 :(得分:2)
在使用附件之前,您正在关闭内存流。将其转储到字节数组,然后加载到附件中,或者在完成之前不要关闭流。
即使您在关闭MemoryStream之前设置了附件的数据,它仍然使用THAT内存流,您需要将其复制到某些内容,因此它不会认为它突然是一个封闭的流。 / p> 编辑:老实说,如果您没有尝试访问封闭的流,我会感到惊讶。您可以尝试取消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”。