static void WeatherMailJob() { OpenPop.Pop3.Pop3Client mclient = new OpenPop.Pop3.Pop3Client(); mclient.Connect("mail.cute.edu.tw", 110, false); mclient.Authenticate("weather", "0988163835"); int cnt = mclient.GetMessageCount(); Console.WriteLine("Message cnt:" + cnt); string bodytext; for (int i = 1; i <= cnt; i++) { try { bodytext = ""; Message msg = mclient.GetMessage(i); string from = msg.Headers.From.MailAddress.Address; Console.WriteLine("from:" + from); Console.WriteLine("subject:" + msg.Headers.Subject); if (from != "*****@*****.**" && from != "*****@*****.**" && from != "*****@*****.**") continue; // string b = msg.MessagePart.GetBodyAsText(); // string htmlString = @"<p>I'm HTML!</p>"; // b= Regex.Replace(b, @"(<(.|\n)*?>| )", ""); // Console.WriteLine(Program.StripHTML( b)); if (!msg.MessagePart.IsText) { MessagePart msgpart = msg.FindFirstPlainTextVersion(); if (msgpart != null && msgpart.IsText) { // Console.WriteLine("body:" + msgpart.GetBodyAsText()); bodytext = msgpart.GetBodyAsText(); } } else { bodytext = msg.MessagePart.GetBodyAsText(); } string txtbodytext = StripHTML(bodytext); txtbodytext = txtbodytext.Replace("\r", "<br>"); Console.WriteLine("body:" + txtbodytext); // txtbodytext = txtbodytext.Replace("\r", "<br>"); if (LogToDB(msg, txtbodytext) || from == "*****@*****.**" || from == "*****@*****.**") { if (from == "*****@*****.**" || from == "*****@*****.**" || msg.Headers.Subject.Contains("颱風")) { #if DEBUG txtbodytext = "<img src=\"http://192.192.161.4/sshmc/logo.png\"><p>致 貴客戶,依中央氣象局發布資料,提醒您以下防災預警資訊:<br>" + txtbodytext; SendMailToAllUser(msg.Headers.Subject.Replace("\n", ""), txtbodytext); #else SendMailToAllUser(msg.Headers.Subject.Replace("\n", ""), bodytext); #endif } else { txtbodytext = "<img src=\"http://192.192.161.4/sshmc/logo.png\"><p>致 貴客戶,依中央氣象局發布資料,提醒您以下防災預警資訊:<br>" + txtbodytext; SendMailToAllUser(msg.Headers.Subject.Replace("\n", ""), txtbodytext); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } } #if !DEBUG for (int i = 1; i <= cnt; i++) mclient.DeleteMessage(i); #endif mclient.Dispose(); // Console.ReadKey(); }
static void Pop3Test() { OpenPop.Pop3.Pop3Client mclient = new OpenPop.Pop3.Pop3Client(); mclient.Connect("mail.cute.edu.tw", 110, false); mclient.Authenticate("weather", "0988163835"); int cnt = mclient.GetMessageCount(); Console.WriteLine("Message cnt:" + cnt); string bodytext; for (int i = 1; i <= cnt; i++) { bodytext = ""; Message msg = mclient.GetMessage(i); Console.WriteLine("from:" + msg.Headers.From.MailAddress.Address); Console.WriteLine("subject:" + msg.Headers.Subject); if (!msg.MessagePart.IsText) { MessagePart msgpart = msg.FindFirstPlainTextVersion(); if (msgpart != null && msgpart.IsText) { Console.WriteLine("body:" + msgpart.GetBodyAsText()); bodytext = msgpart.GetBodyAsText(); } } else { Console.WriteLine("body:" + msg.MessagePart.GetBodyAsText()); bodytext = msg.MessagePart.GetBodyAsText(); } if (msg.Headers.Subject.Contains("大雨") || msg.Headers.Subject.Contains("強風") || msg.Headers.Subject.Contains("地震")) { System.Net.Mail.SmtpClient c = new SmtpClient("mail.cute.edu.tw", 25); c.DeliveryMethod = SmtpDeliveryMethod.Network; c.Credentials = new System.Net.NetworkCredential("weather", "0988163835"); MailMessage m_mesg = new MailMessage(new MailAddress("*****@*****.**"), new MailAddress("*****@*****.**")); m_mesg.Body = bodytext; m_mesg.Subject = msg.Headers.Subject.Replace("\n", ""); m_mesg.IsBodyHtml = true; if (bodytext != "") c.Send(m_mesg); } } for (int i = 1; i <= cnt; i++) mclient.DeleteMessage(i); mclient.Dispose(); }
public bool Execute() { // Get already processed messages list List <String> processedMessages = _context.ReferralEmails.Select(x => x.MessageID).ToList(); if (processedMessages == null) { processedMessages = new List <string>(); } // Get any un-processed messages OpenPop.Pop3.Pop3Client popClient = new OpenPop.Pop3.Pop3Client(); popClient.Connect(_popServer, _popPort, _popSSL); popClient.Authenticate(_popUser, _popPassword); var messageIDs = popClient.GetMessageUids(); for (int i = 0; i < messageIDs.Count; i++) { if (!processedMessages.Contains(messageIDs[i])) { OpenPop.Mime.Message msg = null; string msgStatus = ""; string msgSubject = ""; try { msg = popClient.GetMessage(i + 1); msgSubject = msg.Headers.Subject; if (!_seedMode) { msgStatus = ProcessMessage(msg); } } catch (System.Data.Entity.Validation.DbEntityValidationException ex) { if (ex.EntityValidationErrors != null && ex.EntityValidationErrors.Count() > 0 && ex.EntityValidationErrors.First().ValidationErrors != null && ex.EntityValidationErrors.First().ValidationErrors.Count > 0) { string errorMessage = "An error occurred validating the referral." + "\r\nID: " + messageIDs[i] + "\r\nSubject: " + msgSubject + "\r\nFirst Validation Error: " + ex.EntityValidationErrors.First().ValidationErrors.First().ErrorMessage; _logger.LogError(errorMessage); AABC.DomainServices.Email.SMTP.Send(_smtpAccount, "Referral Validation Error", errorMessage, _validationEmail); } else { string errorMessage = "An error occurred validating the referral." + "\r\nID: " + messageIDs[i] + "\r\nSubject: " + msgSubject + "\r\nError Message: " + ex.Message; _logger.LogError(errorMessage); AABC.DomainServices.Email.SMTP.Send(_smtpAccount, "Referral Validation Error", errorMessage, _validationEmail); } msgStatus = "Validation Error"; } catch (System.Exception ex) { string errorMessage = "An error occurred processing the referral." + "\r\nID: " + messageIDs[i] + "\r\nSubject: " + msgSubject + "\r\nError Message: " + ex.Message; _logger.LogError(errorMessage); AABC.DomainServices.Email.SMTP.Send(_smtpAccount, "Referral Error", errorMessage, _errorEmail); msgStatus = "Errored"; } _context.ReferralEmails.Add(new Data.Models.ReferralEmail() { MessageID = messageIDs[i], MessageSubject = msgSubject, MessageStatus = msgStatus, ProcessedTime = DateTime.Now }); _context.SaveChanges(); } } popClient.Disconnect(); popClient.Dispose(); return(true); }