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 7737869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:07:00+00:00 2026-06-01T08:07:00+00:00

I am attempting to do the following: Retrieve a generated pdf from a web

  • 0

I am attempting to do the following:

  1. Retrieve a generated pdf from a web page
  2. Create an email message
  3. Attach the pdf to the message
  4. Send the message

I am able to retrieve the pdf from the web page and send the message with it as an attachment. However, when I try to open the attachment, I get the dreaded “Adobe Reader could not open ‘filename.pdf’ because it is either not a supported file type or because the file has been damaged” message.

The pdf is generated by an MVC3 page using a custom ActionResult to return the pdf. It looks like this

public class EnrollmentExpectationsPdfResult : FileResult
{
    IList<AdminRepEnrollmentExpectationViewModel> adminreps;

    public EnrollmentExpectationsPdfResult(IList<AdminRepEnrollmentExpectationViewModel> adminrep)
        : this("application/pdf", adminrep)
    { }

    public EnrollmentExpectationsPdfResult(string contentType, IList<AdminRepEnrollmentExpectationViewModel> adminrep)
        : base(contentType)
    {
        adminreps = adminrep;

    }

    protected override void WriteFile(HttpResponseBase response)
    {
        var cd = new ContentDisposition
        {
            Inline = true,
            FileName = "MyPDF.pdf"
        };
        response.AppendHeader("Content-Disposition", cd.ToString());

        //Skip a bunch of boring font stuff
        ...

        var writer = PdfWriter.GetInstance(doc, response.OutputStream);
        writer.PageEvent = new EnrollmentExpectationPDFPageEvent();            
        doc.Open();

        //Skip the doc writing stuff
        ...

        doc.Close();
    }
}

The controller method is here

public ActionResult EnrollmentExpectationsPDF()
{
    //skip a bunch a database stuff
    ...
    return new EnrollmentExpectationsPdfResult(adminList);
}

Here is the code at the heart of the problem…

//Get PDF
CredentialCache cc = new CredentialCache();
cc.Add(
       new Uri("http://myserver/mypdfgeneratingpage"),
       "NTLM",
       new NetworkCredential("myusername", "mypassword"));

var webRequestObject = (HttpWebRequest)WebRequest.Create("http://iapps.national.edu/erp/Reports/EnrollmentExpectationsPDF");
webRequestObject.Credentials = cc;
var response = webRequestObject.GetResponse();
var webStream = response.GetResponseStream();

//Create Mail
System.Net.Mail.MailMessage eMail = ...

//Skipping to attachment stuff
ContentType ct = new ContentType()
{
     MediaType = MediaTypeNames.Application.Pdf,
     Name = "EnrollmentExpecations_2.pdf"
};

Attachment a = new Attachment(webStream, ct);
eMail.Attachments.Add(a);

//Send Message
....

As an experiment, I tried writing the downloaded file to disk

MemoryStream ms = new MemoryStream();
var fileStream = File.Create("C:\\MyPDF.pdf");
webStream.CopyTo(fileStream);

Viola, I am able to open the file from the diskwithout a problem.

What am I missing in order to make the attachment readable?

  • 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-01T08:07:01+00:00Added an answer on June 1, 2026 at 8:07 am

    Just to make it clear to users, thanks to @rlb.usa

    “Make sure you close and flush your stream after you send the object“

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

Sidebar

Related Questions

I'm attempting to use the following sql to retrieve the total amount received, and
I get the following error when attempting to connect to a web service via
I am attempting to convert the following HQL query to Criteria. from SubportfolioAudit as
I am attempting to retrieve a list of objects from Entity Framework via WCF,
I am attempting to retrieve jpeg and jpg files using the following statement: string[]
I'm attempting to create a recursive routine that will retrieve PropertyInfos for all members
All this originated from me poking at a compiler warning message (C4267) when attempting
I am attempting to create a keystore and truststore file from a self-signed certificate,
I am attempting to use coffeescript/jQuery to do the following: 1) Retrieve all the
In my page I make the following ajax call to load data from my

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.