private void Repeatermsg_ItemCommand(object source, RepeaterCommandEventArgs e) { switch (e.CommandName) { case "DeleteMail": try { DeleteClick(((Label)e.Item.FindControl("msgid")).Text, ((Label)e.Item.FindControl("msgMessageID")).Text); }catch {} break; case "OpenBody": this.dgmessages.Visible = true; Repeatermsg.Visible = false; long msgid = long.Parse(((Label)e.Item.FindControl("msgid")).Text); string MessageID = String.Empty; Message[] msgs = new Message[1]; ArrayList msgatt = new ArrayList(); string msgMessageID = ((Label)e.Item.FindControl("msgMessageID")).Text; string PathTemplate; PathTemplate = Path.Combine(ConfigSettings.DataStoragePath, String.Format("webmail{1}{0}{1}mails", UC.UserId, Path.DirectorySeparatorChar)); FileFunctions.CheckDir(PathTemplate, true); string NameOfFile = PathTemplate + Path.DirectorySeparatorChar + msgMessageID + ".tmsg"; if (!File.Exists(NameOfFile)) { DataRow dtPop3 = DatabaseConnection.CreateDataset("SELECT MAILSERVER,MAILUSER,MAILPASSWORD FROM ACCOUNT WHERE UID=" + UC.UserId).Tables[0].Rows[0]; string pop3 = dtPop3[0].ToString(); bool secure = false; if (pop3.StartsWith("!")) { pop3 = pop3.Substring(1); secure = true; } using (Pop3Client email = new Pop3Client(dtPop3[1].ToString(), dtPop3[2].ToString(), pop3, secure)) { try { email.OpenInbox(); email.AttachmentsPath = Path.Combine(ConfigSettings.DataStoragePath, String.Format("webmail{1}{0}{1}", UC.UserId, Path.DirectorySeparatorChar)); email.NextEmail(msgid); try { MessageID = (email.MessageID != null) ? Regex.Replace(email.MessageID.Trim('<', '>'), @"[^a-zA-Z0-9_]", "_") : ""; } catch { MessageID = String.Empty; } Message msg = new Message(); msg.From = (email.From != null && email.From.Length > 0) ? email.From : Root.rm.GetString("WebMLtxt11"); msg.To = (email.To != null && email.To.Length > 0) ? email.To : Root.rm.GetString("WebMLtxt11"); msg.Subject = (email.Subject != null && email.Subject.Length > 0) ? email.Subject : Root.rm.GetString("WebMLtxt11"); msg.MsgID = msgid; if (email.IsMultipart) { IEnumerator enumerator = email.MultipartEnumerator; Queue attach = new Queue(); string bodyhtml = null; string bodyplain = null; while (enumerator.MoveNext()) { Pop3Component multipart = (Pop3Component)enumerator.Current; if (multipart.IsBody) { if (multipart != null && multipart.ContentType.ToLower().IndexOf("text/html") > -1) { bodyhtml = multipart.Data; } else if (multipart != null && multipart.ContentType.ToLower().IndexOf("text/plain") > -1) { bodyplain = multipart.Data; //multipart.Data.Replace("\r","").Replace("\n","<br>"); } } else { if (multipart.ContentID != null && multipart.FilePath != null) { attach.Enqueue(multipart.ContentID); attach.Enqueue("/mailinglist/webmail/mailredir.aspx?render=no&att=1&img=" + multipart.FilePath.Replace(email.AttachmentsPath, "")); } else if (multipart.FilePath != null) { msgatt.Add(multipart.FilePath); } } } if (bodyhtml != null) { while (attach.Count > 0) { bodyhtml = Regex.Replace(bodyhtml, @"(?<=src=[""']?)cid:" + ((string)attach.Dequeue()).Trim('<', '>') + @"(?=[""']?)", (string)attach.Dequeue(), RegexOptions.IgnoreCase | RegexOptions.Multiline); } bodyhtml = Regex.Replace(bodyhtml, @"</?(html|body|link|meta)[\s\S]*?>", "", RegexOptions.IgnoreCase | RegexOptions.Multiline); bodyhtml = Regex.Replace(bodyhtml, @"<title[\s\S]*?</title[\s\S]*?>", "<body_not_allowed", RegexOptions.IgnoreCase | RegexOptions.Multiline); bodyhtml = Regex.Replace(bodyhtml, @"<(style|script|head)[\s\S]*?>", "<!--", RegexOptions.IgnoreCase | RegexOptions.Multiline); bodyhtml = Regex.Replace(bodyhtml, @"</(style|script|head)[\s\S]*?>", "-->", RegexOptions.IgnoreCase | RegexOptions.Multiline); bodyhtml = Regex.Replace(bodyhtml, @"(?<=<a)[ ](?=[\s\S]*?>)", " target=_blank ", RegexOptions.IgnoreCase | RegexOptions.Multiline); } if (bodyhtml != null) { msg.Body = bodyhtml; } else if (bodyplain != null) { msg.Body = bodyplain.Replace("\r", "").Replace("\n", "<br>"); } } else { if (email.IsHTML) { if (email.ContentTransferEncoding != null && email.ContentTransferEncoding.ToLower().Equals("quoted-printable")) { msg.Body = Pop3Utils.FromQuotedPrintable(email.Body); } else { msg.Body = email.Body; } msg.ContentType = "H"; } else { msg.ContentType = "P"; try { msg.Body = email.Body.Replace("\r", "").Replace("\n", "<br>"); } catch { msg.Body = String.Empty; } } } email.CloseConnection(); if (msgatt.Count > 0) { MessageAttach[] attach = new MessageAttach[msgatt.Count]; for (int i = 0; i < msgatt.Count; i++) { MessageAttach ma = new MessageAttach(); ma.Link = msgatt[i].ToString(); ma.Filename = Path.GetFileName(msgatt[i].ToString()); attach[i] = ma; } msg.Attachment = attach; } msgs[0] = msg; dgmessages.DataSource = msgs; dgmessages.DataBind(); tblpaging.Visible = false; if (MessageID.Length > 1) { PathTemplate = Path.Combine(ConfigSettings.DataStoragePath, String.Format("webmail{1}{0}{1}mails", UC.UserId, Path.DirectorySeparatorChar)); NameOfFile = PathTemplate + Path.DirectorySeparatorChar + MessageID + ".tmsg"; FileStream newfile = new FileStream(NameOfFile, FileMode.Create); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(newfile, msg); newfile.Close(); } } catch (Pop3PasswordException) { Context.Items.Add("warning", Root.rm.GetString("Mailtxt16")); } catch (Pop3LoginException) { Context.Items.Add("warning", Root.rm.GetString("Mailtxt17")); } catch (Pop3ConnectException) { Context.Items.Add("warning", Root.rm.GetString("Mailtxt16")); } catch (Pop3DecodeException) { Context.Items.Add("warning", Root.rm.GetString("Mailtxt25")); } catch (Pop3MissingBoundaryException) { Context.Items.Add("warning", Root.rm.GetString("Mailtxt25")); } } } else { Message msg = new Message(); FileStream newfile = new FileStream(NameOfFile, FileMode.Open); BinaryFormatter bf = new BinaryFormatter(); msg = (Message)bf.Deserialize(newfile); newfile.Close(); msgs[0] = msg; dgmessages.DataSource = msgs; dgmessages.DataBind(); tblpaging.Visible = false; } break; } }
public static string FindAndRecordEmailBounces() { Pop3Client email = null; string message = string.Empty; try { email = new Pop3Client("*****@*****.**", "deadFax186", "mail.thebirthdayregister.com"); email.OpenInbox(); // only process x amount of messages at a time because deleting is not really done until the session is closed const long nMaxProcess = 2; long nTotalCount = Math.Min(nMaxProcess, email.MessageCount); long nMaxEmail = email.MessageCount; long nMinEmail = Math.Max(0, email.MessageCount - nMaxProcess); for (long i = nMaxEmail - 1; i >= nMinEmail; i--) { email.NextEmail(i); if (0 != string.Compare(email.To, "*****@*****.**", true)) { continue; } // check the subject, delete items as appropriate if (email.Subject.ToLower().Contains("delay")) { email.DeleteEmail(); } string szRecipient = "", szAction = "", szStatus = "", szTo = "", szFrom = "", szSubject = "", szDate = "", szReply_to = ""; if (email.Subject.ToLower().Contains("failure notice") || email.Subject.ToLower().Contains("Delivery Status Notification (Failure)") || email.Subject.ToLower().Contains("Mail Delivery Failure") || email.Subject.ToLower().Contains("Mail System Error - Returned Mail")) { if (!email.IsMultipart) { szTo = GetEmailFromValue(GetValueFromKey(email.Body, "To:")); szFrom = GetEmailFromValue(GetValueFromKey(email.Body, "From:")); szSubject = CleanSubject(GetValueFromKey(email.Body, "Subject:")); szDate = GetValueFromKey(email.Body, "Date:"); szRecipient = GetEmailFromValue(GetValueFromKey(email.Body, "Final-Recipient:")); szAction = GetValueFromKey(email.Body, "Action:"); szStatus = GetValueFromKey(email.Body, "Status:"); szReply_to = GetEmailFromValue(GetValueFromKey(email.Body, "Reply-To:")); } else { System.Collections.IEnumerator enumerator = email.MultipartEnumerator; while (enumerator.MoveNext()) { Pop3Component multipart = (Pop3Component)enumerator.Current; if (string.IsNullOrEmpty(szTo)) { szTo = GetEmailFromValue(GetValueFromKey(multipart.Data, "To:")); } if (string.IsNullOrEmpty(szTo)) { szTo = GetEmailFromValue(GetValueFromKey(multipart.Data, "Original-Recipient:")); } if (string.IsNullOrEmpty(szFrom)) { szFrom = GetEmailFromValue(GetValueFromKey(multipart.Data, "From:")); } if (string.IsNullOrEmpty(szSubject)) { szSubject = CleanSubject(GetValueFromKey(multipart.Data, "Subject:")); } if (string.IsNullOrEmpty(szDate)) { szDate = GetValueFromKey(multipart.Data, "Date:"); } if (string.IsNullOrEmpty(szDate)) { szDate = GetValueFromKey(multipart.Data, "Arrival-Date:"); } if (string.IsNullOrEmpty(szRecipient)) { szRecipient = GetEmailFromValue(GetValueFromKey(multipart.Data, "Final-Recipient:")); } if (string.IsNullOrEmpty(szAction)) { szAction = GetValueFromKey(multipart.Data, "Action:"); } if (string.IsNullOrEmpty(szStatus)) { szStatus = GetValueFromKey(multipart.Data, "Status:"); } if (string.IsNullOrEmpty(szReply_to)) { szReply_to = GetEmailFromValue(GetValueFromKey(multipart.Data, "Reply-To:")); } } } message = message + szTo + " | " + szFrom + " | " + szReply_to + " | " + szSubject + " | " + szDate + " <br/>"; email.DeleteEmail(); } } message = message + string.Format("Total message count: {0}, Process message count: {1}", email.MessageCount.ToString(), nTotalCount.ToString()); email.CloseConnection(); } catch (Exception ex) { if (null != email) { try { email.CloseConnection(); } catch { } } message = "Exception thrown in FindAndRecordEmailBounces. Message: " + ex.Message; } return(message); }