private string transformFormats(string s) { Match m; if ((m = Regex.Match(s, "=\\?([\\d\\w\\-]+)\\?([QB])\\?(.*?)\\?=")).Success) { Encoding enc = Encoding.GetEncoding(m.Groups[1].Value); switch (m.Groups[2].Value) { case "Q": //Quoted printable s = enc.GetString(Encoding.UTF8.GetBytes(Pop3Utils.FromQuotedPrintable((m.Groups[3].Value)))); break; case "B": //base 64 s = enc.GetString(Convert.FromBase64String(m.Groups[3].Value)); break; } } return(s); }
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; } }