Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8870365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:42:32+00:00 2026-06-14T17:42:32+00:00

When I am trying to debug the code, while stepping into the ‘add Attachment’

  • 0

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,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:42:33+00:00Added an answer on June 14, 2026 at 5:42 pm

    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.

    ms.Position = 0;
    

    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:::

        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: 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

    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;
        }
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to debug my code which is being executed from a unit test
Ive got the following code: Trying to debug it for a while. Can't figure
I was getting the following error message while trying to debug my code: Not
I am trying to debug a kernel code, because of a scheduling while in
While I am trying to debug this code (in C# WinForms), it shows an
I'm trying to debug a problem with some legacy code. While trying to understand
I simplified the code a little while trying to debug: [HttpPost] public ActionResult Register(User
I am having real problems trying debug my code to malfunctioning print statements. I
I'm trying to debug my code. I haven't really used a debugger before. I
I'm trying to debug some code in the .NET Framework. Unfortunately, many of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.