Advertisement
Guest User

Help

a guest
Jul 15th, 2016
680
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.26 KB | None | 0 0
  1. using OpenPop.Mime;
  2. using OpenPop.Mime.Header;
  3. using OpenPop.Pop3;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10.  
  11. namespace bs_torrent_client
  12. {
  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             string username = "xxxxxxxxx";
  18.             string password = "xxxxxxxxx";
  19.             string hostname = "pop.gmail.com";
  20.  
  21.             HeadersFromAndSubject(hostname, 995, true, username, password, 5);
  22.         }
  23.  
  24.         public static void HeadersFromAndSubject(string hostname, int port, bool useSsl, string username, string password, int messageNumber)
  25.         {
  26.             // The client disconnects from the server when being disposed
  27.             using (Pop3Client client = new Pop3Client())
  28.             {
  29.                 // Connect to the server
  30.                 client.Connect(hostname, port, useSsl);
  31.  
  32.                 // Authenticate ourselves towards the server
  33.                 client.Authenticate(username, password, OpenPop.Pop3.AuthenticationMethod.UsernameAndPassword);
  34.  
  35.                 // We want to check the headers of the message before we download
  36.                 // the full message
  37.                 MessageHeader headers = client.GetMessageHeaders(messageNumber);
  38.  
  39.                 RfcMailAddress from = headers.From;
  40.                 string subject = headers.Subject;
  41.  
  42.              
  43.                 if (from.HasValidMailAddress && from.Address.Equals("XXXXXXXXX"))
  44.                 {
  45.                     // Download the full message
  46.                     Message message = client.GetMessage(messageNumber);
  47.  
  48.                     // We know the message contains an attachment with the name "useful.pdf".
  49.                     // We want to save this to a file with the same name
  50.                     foreach (MessagePart attachment in message.FindAllAttachments())
  51.                     {
  52.                         if (attachment.FileName.Equals("XXXXXXXX"))
  53.                         {
  54.                             // Save the raw bytes to a file
  55.                             File.WriteAllBytes(attachment.FileName, attachment.Body);
  56.                         }
  57.                     }
  58.                 }
  59.             }
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement