When I am trying to debug the code, while stepping into the ‘add Attachment’ part, the ContentDisposition from the Watch 2 screen gives me the size of -1 of the attachment.
This makes my code trip and give me the error message:
Failure sending mail.
Whole code:
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;
}
}
}
Specific:
// Goes well until here
// Gives the attachment a size of -1
attach.ContentDisposition.FileName = "FirstName_LastName.vcf";
Can anybody provide me with any information Why my attachment has the size of -1??
Willing to provide more information/code if needed.
Thanks in Advance,
You are closing your memory stream before using your attachement. Dump it to a byte array and then load into your attachment or don’t close your stream until you are done.
Even though you are setting the data of the attachement before the MemoryStream is closed, it is still using THAT memory stream, you will need to copy it to something so it doesn’t think that it is suddenly a closed stream.
EDIT: I am honestly kind of surprised that you are not getting an exception for trying to access a closed stream. You could try to take out the using on the StreamWriter and remove the sw.Close() and ms.Close(). You should probably reset the position of the memory stream to.
I was only suggesting that you copy to byte array so that you can properly dispose of your stream writer and memory stream.
EDIT2: Try this:::
EDIT3: I see now that you solved your own issue. I offer you one more thing for your future coding. Here is an extension method that you can use with any version of .NET and doesn’t matter what framework version
That way you can add your extension method to any program or put it into a “toolkit” for future use. Then it doesn’t matter if “CopyTo” doesn’t exist, you can still call “CopyToEx” in code the same way.