Created
June 19, 2018 01:04
-
-
Save jinan-kordab/deb2de86cb7c90c73c7719ee354ee0e9 to your computer and use it in GitHub Desktop.
Console app that reads all your email from Gmail that is sent by remote printer.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using OpenPop.Mime; | |
using OpenPop.Pop3; | |
namespace ReadEmailsFromPrinterAndStoreThem | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
bool f = FetchAllMessages(); | |
} | |
public static bool FetchAllMessages() | |
{ | |
bool done = false; | |
EF.PRINTERSTATSFROMEMAILEntities dbps = new EF.PRINTERSTATSFROMEMAILEntities(); | |
// The client disconnects from the server when being disposed | |
using (Pop3Client client = new Pop3Client()) | |
{ | |
client.Connect("pop.gmail.com", 995, true); | |
client.Authenticate("recent:yournickname@gmail.com", "PASSWORD"); | |
int messageCount = client.GetMessageCount(); | |
for (int i = messageCount; i > 0; i--) | |
{ | |
Message eMsg = client.GetMessage(i); | |
string result = System.Text.Encoding.UTF8.GetString(eMsg.RawMessage); | |
string[] lines = result.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None); | |
for (int ij = 0; ij < lines.Length; ij++) | |
{ | |
if (lines[ij].ToString().Contains("Content-Type: text/plain")) | |
{ | |
string PRINTER_ID = lines[ij + 2].ToString().Split(':').Skip(1).FirstOrDefault(); | |
string PRINTER_NAME = lines[ij + 3].ToString().Split(':').Skip(1).FirstOrDefault(); | |
string PRINTERTYPE = lines[ij + 4].ToString().Split(':').Skip(1).FirstOrDefault(); | |
string TIMEOFDAY = lines[ij + 5].ToString().Split(':').Skip(1).FirstOrDefault(); | |
string PAPERSPRINTED = lines[ij + 6].ToString().Split(':').Skip(1).FirstOrDefault(); | |
string DOUBLESIDED = lines[ij + 7].ToString().Split(':').Skip(1).FirstOrDefault(); | |
string USERS = lines[ij + 8].ToString().Split(':').Skip(1).FirstOrDefault(); | |
EF.PRINTERSUNSHINE psunshine = new EF.PRINTERSUNSHINE | |
{ | |
PRINTERID = PRINTER_ID, | |
PRINTERNAME = PRINTER_NAME, | |
PRINTERTYPE = PRINTERTYPE, | |
TIMEOFDAY = TIMEOFDAY, | |
PAPERSPRINTED = PAPERSPRINTED, | |
DOUBLESIDED = DOUBLESIDED, | |
USERS = USERS | |
}; | |
dbps.PRINTERSUNSHINE.Add(psunshine); | |
dbps.SaveChanges(); | |
} | |
} | |
client.DeleteMessage(i); | |
} | |
client.Disconnect(); | |
client.Dispose(); | |
done = true; | |
return done; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment