pcmaker

download.ashx

Nov 29th, 2012
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.93 KB | None | 0 0
  1. //download.ashx
  2.  
  3. using LumiSoft.Net.Mail;
  4. using LumiSoft.Net.MIME;
  5. using LumiSoft.Net.POP3.Client;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Web;
  10.  
  11. namespace Mojna.WebApplication
  12. {
  13.     public class downloader : IHttpHandler
  14.     {
  15.         public void ProcessRequest(HttpContext context)
  16.         {
  17.             HttpResponse Response = context.Response;
  18.             HttpRequest Request = context.Request;
  19.             Response.Clear();
  20.  
  21.             //dosya isteği gelmişse işle
  22.             if (!string.IsNullOrEmpty(Request["DosyaAdi"]))
  23.             {
  24.                 byte[] data = null;
  25.  
  26.                 popaBaglan(Request, ref data);
  27.  
  28.                 Response.AddHeader("content-disposition", "attachment; filename=" + Request["DosyaAdi"]);
  29.                 Response.OutputStream.Write(data, 0, data.Length);
  30.             }
  31.  
  32.             //her şekilde responsu bitir.
  33.             Response.End();
  34.         }
  35.  
  36.         void popaBaglan(HttpRequest Request, ref byte[] data)
  37.         {
  38.             POP3_Client clt = new POP3_Client();
  39.             clt.Connect("sunucu", 110, false);
  40.             clt.Authenticate("mail", "parola", true);
  41.             clt.Timeout = int.MaxValue;
  42.  
  43.             //mesajı bul
  44.             var mesaj = clt.Messages.OfType<POP3_ClientMessage>().First(p => p.UID == Request["MesajID"]);
  45.  
  46.             //mesajı parsele
  47.             Mail_Message mime = Mail_Message.ParseFromByte(mesaj.MessageToByte());
  48.  
  49.             //dosyayı bul
  50.             MIME_Entity entry = mime.Attachments.First(p => p.ContentType.Param_Name == Request["DosyaAdi"]);
  51.  
  52.             //dosyayı data olarak al ve ref ile geri dön.
  53.             string FileName = entry.ContentType.Param_Name;
  54.             data = ((MIME_b_SinglepartBase)entry.Body).Data;
  55.  
  56.         }
  57.  
  58.         public bool IsReusable
  59.         {
  60.             get
  61.             {
  62.                 return false;
  63.             }
  64.         }
  65.     }
  66. }
Add Comment
Please, Sign In to add comment