2016-05-10 18 views
0

ここで私の問題です。サーバ上の一時フォルダに保存されているPDFファイルを生成するWebページ(既存のプロジェクトに属していない外部Webページ)を生成しました。指定されたパラメータでPDFが生成されます。 私はそのウェブページがどのようなものであれ(PDF)を保存し、それを電子メールに添付して、結果が---->電子メールにPDFを添付したいと思っています。PDFをダウンロードして電子メールに添付する

Dim FullReportUrl As String = Nothing 
 
      FullReportUrl = (String.Format("https://www.something.com/Reports/ReportManager.aspx?SessionID={0}&report=CaseFullReport&CompanyCode={1}&ExportType=pdf&ConnectionType=Main&GlobalVar=IncidentID^{2}|CompanyCode^{3}", "2c3fe87f-5c4e-4a7d-acda-970ed47f27eb", m_User.CompanyCode, m_IncidentCaseID.ToString(), m_User.CompanyCode)) 
 

 
      '# Create mail message 
 
      Dim msg As New MailMessage 
 
      msg.From = New MailAddress(Globals.gFromEmailAddr) 
 
      msg.To.Add(New MailAddress("[email protected]")) 
 
      msg.Subject = String.Concat("Full Report IR-", m_IncidentID.ToString()) 
 
      '# Attach the additional text to the E-Mail. 
 
      If Not String.IsNullOrEmpty(rtbAdditionalText.Text.ToString()) Then 
 
       msg.Body = "<html><body>Dear " + "Test" + ", (Please Do Not Replay to this email)<br><br>" + rtbAdditionalText.Text.ToString().Trim() + "<br><br> Sincerely,<br> Something." + "<br><img id=""Logo"" alt=""logo"" src=""https://www.something.com/gs/Images/Layout/logo.png""/></body></html>" 
 
      End If 
 

 
      msg.IsBodyHtml = True 
 
      msg.Priority = MailPriority.High 
 

 

 
      '# Attach the full report to the email. 
 

 
      Dim ms As MemoryStream = New MemoryStream 
 
      Dim writer As StreamWriter = New StreamWriter(ms) 
 
      With writer 
 
       .Write(GetFileStream(FullReportUrl)) 
 
       .Flush() 
 

 
       'ms.Position = 0 '== Reading the stream from the beginging and finializing it. 
 
       Dim attach As Attachment = New Attachment(ms, "test.pdf", System.Net.Mime.MediaTypeNames.Application.Pdf) 
 
       msg.Attachments.Add(attach) 
 
      End With 
 

 
      
 

 
      'Create client and send 
 
      Dim client As New SmtpClient(Globals.gMailServer) 
 
      client.Credentials = New System.Net.NetworkCredential("[email protected]", "555555$") 
 

 
      Dim userState As Object = msg 
 
      client.Send(msg) 
 
      msg = Nothing 
 
      client.Dispose()

破損出てくるPDF。なぜ誰かが指摘できますか?

+0

投稿されたコードはVB.NETですが、C#とVB.NETの両方であなたの質問にタグを付けました。あなたはどれが欲しいですか?あなたは 'FullReportUrl'で何をしていますか?レポートURLはPDFを返すものですか? – Tim

+0

FullReportURL - PDFファイルを生成するURL。 – user3188868

答えて

0

、その後、あなたのPDFファイルを生成 URLで

StreamReader 

オーバー
HttpWebRequest 

byte[] 

にPDFのデータを取得してくださいそれを電子メールに添付する方が簡単になります

0

サーバーを制御できるかどうかはわかりません。もしそうなら、私はあなたにファイルストリームの結果を与えるAPIを作成します。以下の擬似コード(C#のため申し訳ありません) 公共FileStreamResultインデックス(int型のID)

{ 
     MemoryStream mystream = new MemoryStream(); 
     mystream = {Generate pdf code} 
     return new FileStreamResult(mystream,"application/pdf"); 
    } 

あなたのクライアントは、この

public Sub GetPDFfromAPI(int id) 
     dim myfile as byte() = Nothing 
     dim handler as HttpClientHandler = new HttpClientHandler() 
     dim myclient as new HttpClient(handler) 
     dim client as new 
      client.BaseAddress = new Uri("http://myhosturl") 
      client.DefaultRequestHeaders.Accept.Clear() 
      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/octet-stream")) 

      Dim response as HttpResponseMessage = client.Get("api/ControllerName/"+id.ToString()) 
      if (response.IsSuccessStatusCode) then 


       myfile = response.Content.ReadAsByteArray() 


      else 

       return null 
      End if 
    Dim oFileStream As System.IO.FileStream 
    oFileStream = New System.IO.FileStream("myfile.pdf",System.IO.FileMode.Create) 
    oFileStream.Write(myfile, 0, myfle.Length) 
    oFileStream.Close() 

おそらくその

非同期(async)メソッドを作成することをお勧めのようになります。
+0

私は私の質問についてより明確にしようとします:Webページを呼び出すことによって、PDFが生成されます。私はそのPDFをつかみ、電子メールに添付したいと思います。だから、基本的に私はそのWebページにPDFを表示したくないの背後にあるコードでは、私は添付ファイルとして電子メールにそれをプッシュダウンロードしたい。私が投稿したコードを見てください。 – user3188868

関連する問題