public void FetchMessages(NameValueCollection param) { Pop3 client = new Pop3(); bool connected = false; try { string outdir = RequireParam("outdir", param); if (!Directory.Exists(outdir)) Directory.CreateDirectory(outdir); string server = RequireParam("server", param); int port = Convert.ToInt32(RequireParam("port", param)); string ssl = param["ssl"]; if (ssl.Length == 0) ssl = port == 995 ? "1" : "0"; TlsParameters par = new TlsParameters(); par.CommonName = server; par.CertificateVerifier = CertificateVerifier.AcceptAll; Pop3Security sec = (Pop3Security)Enum.Parse(typeof(Pop3Security), RequireParam("security", param)); log.Info("Connecting to {0}:{1}. SSL: {2}", server, port, ssl); string sess = client.Connect(server, port, par, sec); connected = true; log.Info("Connected: {0}", sess); Pop3Authentication auth = (Pop3Authentication)Enum.Parse(typeof(Pop3Authentication), RequireParam("auth", param)); log.Info("Logging in: {0}", RequireParam("user", param)); client.Login(RequireParam("user", param), RequireParam("pwd", param), auth); log.Info("Logged in."); Pop3MessageCollection messages = client.GetMessageList(); log.Info("There are {0} messages", messages.Count); int maxcount = Convert.ToInt32(RequireParam("maxcount", param)); if (maxcount <= 0 || maxcount > messages.Count) maxcount = messages.Count; for (int i = 0; i < maxcount; i++) { Pop3MessageInfo mi = messages[i]; log.Info("Downloading message {0}", mi.SequenceNumber); MailMessage mm = client.GetMailMessage(mi.SequenceNumber); log.Info("Message from: {0}, to: {1}, subject: {2}", mm.From, mm.To, mm.Subject); string g = Guid.NewGuid().ToString(); string file = Path.Combine(outdir, g + ".eml"); log.Info("Saving message to {0}", file); mm.Save(file); client.Delete(mi.SequenceNumber); } bool rollback = !"0".Equals(RequireParam("noremove", param)); client.Disconnect(rollback); } catch (Exception ex) { if (connected) client.Disconnect(true); throw; } }