public static List<MailMessageEx> Receive(string hostname, int port, bool useSsl, string username, string password) { using (Pop3Client client = new Pop3Client(hostname, port, useSsl, username, password)) { client.Trace += new Action<string>(Console.WriteLine); client.Authenticate(); client.Stat(); List<MailMessageEx> maillist = new List<MailMessageEx>(); MailMessageEx message = null; foreach (Pop3ListItem item in client.List()) { message = client.RetrMailMessageEx(item); if (message != null) { client.Dele(item); maillist.Add(message); } } client.Noop(); client.Rset(); client.Quit(); return maillist; } }
public static List <MailMessageEx> Receive(string hostname, int port, bool useSsl, string username, string password) { using (Pop3Client client = new Pop3Client(hostname, port, useSsl, username, password)) { client.Trace += new Action <string>(Console.WriteLine); client.Authenticate(); client.Stat(); List <MailMessageEx> maillist = new List <MailMessageEx>(); MailMessageEx message = null; foreach (Pop3ListItem item in client.List()) { message = client.RetrMailMessageEx(item); if (message != null) { client.Dele(item); maillist.Add(message); } } client.Noop(); client.Rset(); client.Quit(); return(maillist); } }
static void Main(string[] args) { try { string host = ConfigurationManager.AppSettings.Get("popHost"); int port = Convert.ToInt32(ConfigurationManager.AppSettings.Get("popPort")); bool useSSL = Convert.ToBoolean(ConfigurationManager.AppSettings.Get("useSSL")); string username = ConfigurationManager.AppSettings.Get("popUser"); string password = ConfigurationManager.AppSettings.Get("popPassword"); string dropPath = ConfigurationManager.AppSettings.Get("dropFolderPath"); bool traceToConsole = Convert.ToBoolean(ConfigurationManager.AppSettings.Get("traceToConsole")); bool deleteAfterRetrieve = Convert.ToBoolean(ConfigurationManager.AppSettings.Get("deleteAfterRetrieve")); TraceProvider.RegisterTraceProvider(); using (Pop3Client popClient = new Pop3Client(host, port, useSSL, username, password)) { if (traceToConsole) { popClient.Trace += new Action <string>(Console.WriteLine); } //connects to Pop3 Server, Executes POP3 USER and PASS popClient.Authenticate(); popClient.Stat(); System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(); smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.SpecifiedPickupDirectory; smtpClient.PickupDirectoryLocation = dropPath; StringBuilder traceMessage = new StringBuilder("Messages To Retrieve: " + popClient.List().Count); Console.WriteLine(traceMessage.ToString()); ULS.LogMessage("Pop Client", traceMessage.ToString(), "E-Mail", TraceProvider.TraceSeverity.InformationEvent); foreach (Pop3ListItem item in popClient.List()) { using (MailMessageEx popMessage = popClient.RetrMailMessageEx(item)) { using (System.Net.Mail.MailMessage smtpMessage = new System.Net.Mail.MailMessage()) { if (traceToConsole) { foreach (System.Net.Mail.MailAddress toAddress in popMessage.To) { Console.WriteLine("To: " + toAddress.Address); } Console.WriteLine("From: " + popMessage.From.Address); Console.WriteLine("Subject: " + popMessage.Subject); Console.WriteLine("Attachments: " + popMessage.Attachments.Count); foreach (System.Net.Mail.Attachment attachment in popMessage.Attachments) { Console.WriteLine("Attachment: " + attachment.Name); } Console.Write("Body: " + popMessage.Body + "\n"); } smtpMessage.From = popMessage.From; smtpMessage.Subject = popMessage.Subject; smtpMessage.SubjectEncoding = popMessage.SubjectEncoding; smtpMessage.Body = popMessage.Body; smtpMessage.BodyEncoding = popMessage.BodyEncoding; smtpMessage.Sender = popMessage.Sender; traceMessage = new StringBuilder("Message Processed:<br/>\n"); foreach (System.Net.Mail.MailAddress toAddress in popMessage.To) { traceMessage.Append("To: " + toAddress + "<br/>\n"); smtpMessage.To.Add(toAddress); } foreach (System.Net.Mail.MailAddress ccAddress in popMessage.CC) { traceMessage.Append("CC: " + ccAddress + "<br/>\n"); smtpMessage.CC.Add(ccAddress); } traceMessage.Append("From: " + popMessage.From.Address + "<br/>\n"); traceMessage.Append("Subject: " + popMessage.Subject + "<br/>\n"); foreach (System.Net.Mail.Attachment attachment in popMessage.Attachments) { smtpMessage.Attachments.Add(attachment); traceMessage.Append("Attachment: " + attachment.Name + "<br/>\n"); } smtpMessage.IsBodyHtml = popMessage.IsBodyHtml; foreach (string key in popMessage.Headers.AllKeys) { smtpMessage.Headers.Add(key, popMessage.Headers[key]); } ULS.LogMessage("Pop Client", traceMessage.ToString(), "E-Mail", TraceProvider.TraceSeverity.InformationEvent); smtpClient.Send(smtpMessage); if (deleteAfterRetrieve) { popClient.Dele(item); } } } } popClient.Noop(); if (!deleteAfterRetrieve) { popClient.Rset(); } popClient.Quit(); } } catch (Exception ex) { ULS.LogError("Pop Client", ex.ToString(), "E-Mail"); } TraceProvider.UnregisterTraceProvider(); }
/// <summary> /// 获取邮件 /// </summary> public void MailPop3Client() { //Logger.Log.Info("邮件审批开始..."); //Logger.Log.Info("************************---PopServer=" + PopServer); //Logger.Log.Debug("************************---PopPort=" + PopPort); //Logger.Log.Debug("************************---User="******"************************---Pass="******"************************---UseSSL=" + UseSSL); using (Pop3Client client = new Pop3Client(PopServer, PopPort, UseSSL, User, Pass)) { //显示程序与POP3服务器的通信过程 //client.Trace += new Action<string>(Console.WriteLine); //连接POP3服务器并验证用户身份 Logger.Log.Info("Connect POP3 Server and verify..."); client.Authenticate(); Logger.Log.Info("Verify Success..."); client.Stat(); //枚举所有未读邮件 Logger.Log.Info("Mail Exist..." + client.List()); SpreadtrumLHDEntities Db = new SpreadtrumLHDEntities(); foreach (Pop3ListItem item in client.List()) { MailMessageEx message = null; try { LOTSImportSDLogs lotlogs = new LOTSImportSDLogs(); Logger.Log.Info("RetrMailMessageEx item=" + item.MessageId); message = client.RetrMailMessageEx(item); if (message.Subject.ToLower().StartsWith("[lot judgement]") //&& message.Sender.Address.ToLower() == "*****@*****.**" ) { Logger.Log.Info("RetrMailMessageEx message subject=" + message.Subject); string[] data = getLotData(message.Subject); Logger.Log.Info("RetrMailMessageEx message osat=" + data[2]); Logger.Log.Info("RetrMailMessageEx message Device=" + data[3]); Logger.Log.Info("RetrMailMessageEx message LotNO=" + data[4]); Logger.Log.Info("RetrMailMessageEx message Stage=" + data[5]); Logger.Log.Info("RetrMailMessageEx message DeliveryDate=" + message.DeliveryDate.ToString()); lotlogs.OSAT = data[2]; lotlogs.Device = data[3]; lotlogs.LotNO = data[4]; lotlogs.Stage = data[5]; lotlogs.DeliveryDate = message.DeliveryDate; lotlogs.CreateTime = DateTime.Now; lotlogs.LHDFormatStatus = "Pending"; lotlogs.LHDImportStatus = "Pending"; Db.LOTSImportSDLogs.Add(lotlogs); //Db.Set<LOTSImportSDLogs>().Attach(lotlogs); //Db.Entry(lotlogs).State = EntityState.Added; } client.Dele(item); } catch (Exception ex) { Logger.Log.Error("RetrMailMessageEx error=" + ex); //client.Dele(item); continue; } } Db.SaveChanges(); Logger.Log.Info("邮件处理完毕。"); client.Quit(); } }
/// <summary> /// 获取邮件 /// </summary> public void MailPop3Client() { //Logger.Log.Info("邮件审批开始..."); //Logger.Log.Info("************************---PopServer=" + PopServer); //Logger.Log.Debug("************************---PopPort=" + PopPort); //Logger.Log.Debug("************************---User="******"************************---Pass="******"************************---UseSSL=" + UseSSL); using (Pop3Client client = new Pop3Client(PopServer, PopPort, UseSSL, User, Pass)) { //显示程序与POP3服务器的通信过程 //client.Trace += new Action<string>(Console.WriteLine); //连接POP3服务器并验证用户身份 Logger.Log.Info("Connect POP3 Server and verify..."); client.Authenticate(); Logger.Log.Info("Verify Success..."); client.Stat(); //枚举所有未读邮件 Logger.Log.Info("Mail Exist..." + client.List()); //SpreadtrumLHDEntities Db = new SpreadtrumLHDEntities(); DbOperation dbo = new DbOperation(); foreach (Pop3ListItem item in client.List()) { MailMessageEx message = null; try { LOTSImportSDLogs lotlogs = new LOTSImportSDLogs(); Logger.Log.Info("RetrMailMessageEx item=" + item.MessageId); message = client.RetrMailMessageEx(item); if (message.Subject.ToLower().StartsWith("[lot judgement]") //&& message.Sender.Address.ToLower() == "*****@*****.**" ) { Logger.Log.Info("RetrMailMessageEx message subject=" + message.Subject); string[] data = getLotData(message.Subject); Logger.Log.Info("RetrMailMessageEx message osat=" + data[2]); Logger.Log.Info("RetrMailMessageEx message Device=" + data[3]); Logger.Log.Info("RetrMailMessageEx message LotNO=" + data[4]); Logger.Log.Info("RetrMailMessageEx message Stage=" + data[5]); Logger.Log.Info("RetrMailMessageEx message DeliveryDate=" + message.DeliveryDate.ToString()); //lotlogs.OSAT = data[2]; //lotlogs.Device = data[3]; //lotlogs.LotNO = data[4]; //lotlogs.Stage = data[5]; //lotlogs.DeliveryDate = message.DeliveryDate; //lotlogs.CreateTime = DateTime.Now; //lotlogs.LHDFormatStatus = "Pending"; //lotlogs.LHDImportStatus = "Pending"; //Db.LOTSImportSDLogs.Add(lotlogs); string insertSQL = @"INSERT INTO [dbo].[LOTSImportSDLogs] ([OSAT] ,[Device] ,[LotNO] ,[Stage] ,[DeliveryDate] ,[CreateTime] ,[LHDFormatStatus] ,[LHDImportStatus]) VALUES ('{0}' ,'{1}' ,'{2}' ,'{3}' ,'{4}' ,GETDATE() ,'Pending' ,'Pending');"; //Db.Set<LOTSImportSDLogs>().Attach(lotlogs); //Db.Entry(lotlogs).State = EntityState.Added; dbo.ExecuteNonQuery(string.Format(insertSQL, data[2], data[3], data[4], data[5], message.DeliveryDate.ToString("yyyy-MM-dd HH:mm:ss"))); } client.Dele(item); } catch (Exception ex) { Logger.Log.Error("RetrMailMessageEx error=" + ex); //client.Dele(item); continue; } } //Db.SaveChanges(); Logger.Log.Info("邮件处理完毕。"); client.Quit(); } }