public JsonResult DownloadAttachment(int ID, string Name) { Ctx ct = Session["ctx"] as Ctx; Name = Server.HtmlDecode(Name); MMailAttachment1 _mAttachment = new MMailAttachment1(ct, ID, null); string path = ""; var fName = ""; foreach (MAttachmentEntry oMAttachEntry in _mAttachment.GetEntries()) { if (Name.ToUpper() == oMAttachEntry.GetName().ToUpper()) { fName = DateTime.Now.Ticks.ToString() + Name; path = Path.Combine(Server.MapPath("~/TempDownload"), fName); byte[] bytes = oMAttachEntry.GetData(); using (FileStream fs = new FileStream(path, FileMode.Append, System.IO.FileAccess.Write)) { fs.Write(bytes, 0, bytes.Length); } break; } } return(Json(JsonConvert.SerializeObject(fName), JsonRequestBehavior.AllowGet)); }
//Used to save letters as Attachment.... User can see saved letteres from History Form....... public string SaveAttachment(string subject, int AD_Table_ID, string html, Dictionary <string, Dictionary <string, string> > values) { StringBuilder strb = new StringBuilder(); StringBuilder sHtml = new StringBuilder(); //for (int i = 0; i < values.Count; i++) //{ List <string> keysss = values.Keys.ToList(); for (int q = 0; q < keysss.Count; q++) { string copy = html; Dictionary <string, string> val = values[keysss[q]]; List <string> valKeys = val.Keys.ToList(); for (int c = 0; c < valKeys.Count; c++) { copy = copy.Replace(valKeys[c], val[valKeys[c]]); } byte[] arrays = HtmlToPdfbytes(copy, true); int AD_Client_Id = ctx.GetAD_Client_ID(); int iOrgid = ctx.GetAD_Org_ID(); MMailAttachment1 _mAttachment = new MMailAttachment1(ctx, 0, null); _mAttachment.AddEntry(subject + ".pdf", arrays); _mAttachment.SetAD_Client_ID(AD_Client_Id); _mAttachment.SetAD_Org_ID(iOrgid); _mAttachment.SetAD_Table_ID(AD_Table_ID); _mAttachment.IsActive(); _mAttachment.SetAttachmentType("L"); //get first key coloumn _mAttachment.SetRecord_ID(Util.GetValueOfInt(keysss[q])); _mAttachment.SetTextMsg(copy); _mAttachment.SetTitle(subject); _mAttachment.NewRecord(); if (_mAttachment.Save()) { } } //} return(strb.ToString()); }
private void GetMails(UserInformation user, int AD_User_ID, int AD_Client_ID, int AD_Org_ID) { string login = Login(user); if (login.Equals("")) { imapMail.SelectInbox(); List <long> uidList = imapMail.SearchFlag(Flag.All); uidList.Reverse(); //DocumentService ser = new DocumentService(); byte[] bytes = null; string tableName = "AD_User"; int _tableID = -1; int existRec = -1; StringBuilder attachmentID = new StringBuilder(); foreach (long uid in uidList) { try { Envelope structure = imapMail.GetEnvelopeByUID(uid); string from = structure.From[0].Address; try { string sql = "SELECT " + tableName + "_ID " + " , C_BPartner_ID " + "FROM " + tableName + " WHERE lower(Email) =" + "'" + from.Trim().ToLower() + "'"; sql += " AND AD_Client_ID=" + GetCtx().GetAD_Client_ID(); string finalSql = MRole.GetDefault(GetCtx(), false).AddAccessSQL(sql, tableName.ToString(), MRole.SQL_NOTQUALIFIED, MRole.SQL_RO); IDataReader idr = DB.ExecuteReader(sql);//+ " order by ad_texttemplate_id"); DataTable dt = new DataTable(); dt.Load(idr); idr.Close(); if (dt.Rows.Count <= 0) { retVal.Append("Either proper access is not there or Email not found in database"); continue; } if (sender == "contacts") { _tableID = PO.Get_Table_ID("AD_User"); existRec = GetAttachedRecord(_tableID, Convert.ToInt32(dt.Rows[0][0]), Convert.ToInt32(uid), folderName); } if (sender == "businessPartner") { _tableID = PO.Get_Table_ID("C_BPartner"); existRec = GetAttachedRecord(_tableID, Convert.ToInt32(dt.Rows[0][1]), Convert.ToInt32(uid), folderName); } if (existRec > 0)// Is mail already attached { retVal.Append("MailAlreadyAttachedWithParticularRecord"); continue; } if (dt.Rows.Count == 1) { MMailAttachment1 mAttachment = new MMailAttachment1(GetCtx(), 0, null); IMail message; String eml = imapMail.GetMessageByUID(uid); message = new MailBuilder().CreateFromEml(eml); string textmsg = message.Html; bool isAttachment = false; for (int i = 0; i < message.Attachments.Count; i++) { isAttachment = true; mAttachment.SetBinaryData(message.Attachments[i].Data); mAttachment.AddEntry(message.Attachments[i].FileName, message.Attachments[i].Data); } string cc = "";// mailBody.Cc; for (int i = 0; i < message.Cc.Count; i++) { cc += ((Limilabs.Mail.Headers.MailBox)message.Cc[i]).Address + ";"; } string bcc = "";// mailBody.Bcc; for (int i = 0; i < message.Bcc.Count; i++) { bcc += ((Limilabs.Mail.Headers.MailBox)message.Bcc[i]).Address + ";"; } string title = message.Subject; string mailAddress = ""; for (int i = 0; i < message.To.Count; i++) { mailAddress += ((Limilabs.Mail.Headers.MailBox)message.To[i]).Address + ";"; } string mailFrom = ""; for (int i = 0; i < message.From.Count; i++) { mailFrom += ((Limilabs.Mail.Headers.MailBox)message.From[i]).Address + ";"; } string date = ((DateTime)message.Date).ToShortDateString(); mAttachment.SetAD_Client_ID(GetCtx().GetAD_Client_ID()); mAttachment.SetAD_Org_ID(GetCtx().GetAD_Org_ID()); mAttachment.SetAD_Table_ID(_tableID); mAttachment.SetAttachmentType("I"); mAttachment.SetDateMailReceived(message.Date); mAttachment.SetFolderName(folderName); mAttachment.SetIsActive(true); mAttachment.SetIsAttachment(isAttachment); mAttachment.SetMailAddress(mailAddress); mAttachment.SetMailAddressBcc(bcc); mAttachment.SetMailAddressCc(cc); mAttachment.SetMailAddressFrom(mailFrom); if (sender == "contacts") { mAttachment.SetRecord_ID(Convert.ToInt32(dt.Rows[0][0])); } if (sender == "businessPartner") { mAttachment.SetRecord_ID(Convert.ToInt32(dt.Rows[0][1])); } mAttachment.SetMailUID(Convert.ToInt32(uid)); mAttachment.SetMailUserName(mailAddress); mAttachment.SetTextMsg(textmsg); mAttachment.SetTitle(message.Subject); if (!mAttachment.Save())//save into database { retVal.Append("SaveError"); } } else if (dt.Rows.Count == 0) { retVal.Append("NoRecordFound"); } else { retVal.Append("MultipleRecordFound"); } } catch { } } catch (Exception ex) { Logout(); } } Logout(); } else { log.Log(Level.SEVERE, login); } }
private void GetMails(UserInformation user, int AD_User_ID, int AD_Client_ID, int AD_Org_ID) { DataSet dsUser = DB.ExecuteDataset("select isemail,notificationtype from ad_user where ad_user_id=" + AD_User_ID); string login = Login(user); if (login.Equals("")) { imapMail.SelectInbox(); List <long> uidList = imapMail.SearchFlag(Flag.All); uidList.Reverse(); //DocumentService ser = new DocumentService(); byte[] bytes = null; string tableName = "AD_User"; int _tableID = -1; int existRec = -1; StringBuilder attachmentID = new StringBuilder(); string userOrBp = string.Empty; int record_ID = 0; foreach (long uid in uidList) { try { Envelope structure = imapMail.GetEnvelopeByUID(uid); string from = structure.From[0].Address; string subJect = structure.Subject; if (!String.IsNullOrEmpty(subJect) && subJect.IndexOf("(●") > -1) { string documentNO = subJect.Substring(subJect.IndexOf(":") + 1, subJect.IndexOf("(●") - (subJect.IndexOf(":") + 1)); subJect = subJect.Substring(subJect.IndexOf("(●") + 2); subJect = subJect.Substring(0, subJect.LastIndexOf("●)")); string TableID = subJect.Split('-')[0]; // subJect.Substring(subJect.IndexOf("(") + 1, subJect.LastIndexOf("_") - subJect.IndexOf("(") - 1); string recordID = subJect.Split('-')[1]; // subJect.Substring(subJect.IndexOf("_") + 1, subJect.LastIndexOf(")") - subJect.IndexOf("_") - 1); existRec = GetAttachedRecord(Util.GetValueOfInt(TableID), Convert.ToInt32(recordID), Convert.ToInt32(uid), folderName); if (existRec > 0)// Is mail already attached { retVal.Append("MailAlreadyAttachedWithParticularRecord"); continue; } MMailAttachment1 mAttachment = new MMailAttachment1(GetCtx(), 0, null); IMail message; String eml = imapMail.GetMessageByUID(uid); message = new MailBuilder().CreateFromEml(eml); string textmsg = message.Html; bool isAttachment = false; for (int i = 0; i < message.Attachments.Count; i++) { isAttachment = true; mAttachment.SetBinaryData(message.Attachments[i].Data); mAttachment.AddEntry(message.Attachments[i].FileName, message.Attachments[i].Data); } string cc = "";// mailBody.Cc; for (int i = 0; i < message.Cc.Count; i++) { cc += ((Limilabs.Mail.Headers.MailBox)message.Cc[i]).Address + ";"; } string bcc = "";// mailBody.Bcc; for (int i = 0; i < message.Bcc.Count; i++) { bcc += ((Limilabs.Mail.Headers.MailBox)message.Bcc[i]).Address + ";"; } string title = message.Subject; string mailAddress = ""; for (int i = 0; i < message.To.Count; i++) { mailAddress += ((Limilabs.Mail.Headers.MailBox)message.To[i]).Address + ";"; } string mailFrom = ""; for (int i = 0; i < message.From.Count; i++) { mailFrom += ((Limilabs.Mail.Headers.MailBox)message.From[i]).Address + ";"; } mAttachment.SetAD_Client_ID(GetCtx().GetAD_Client_ID()); mAttachment.SetAD_Org_ID(GetCtx().GetAD_Org_ID()); mAttachment.SetAD_Table_ID(Convert.ToInt32(TableID)); mAttachment.SetAttachmentType("I"); mAttachment.SetDateMailReceived(message.Date); mAttachment.SetFolderName(folderName); mAttachment.SetIsActive(true); mAttachment.SetIsAttachment(isAttachment); mAttachment.SetMailAddress(mailAddress); mAttachment.SetMailAddressBcc(bcc); mAttachment.SetMailAddressCc(cc); mAttachment.SetMailAddressFrom(mailFrom); mAttachment.SetRecord_ID(Convert.ToInt32(recordID)); //if (sender == "AD_User") //{ // mAttachment.SetRecord_ID(Convert.ToInt32(dt.Rows[j][0])); // record_ID = Convert.ToInt32(dt.Rows[j][0]); //} //if (sender == "C_BPartner") //{ // mAttachment.SetRecord_ID(Convert.ToInt32(dt.Rows[j][1])); // record_ID = Convert.ToInt32(dt.Rows[j][1]); //} mAttachment.SetMailUID(Convert.ToInt32(uid)); mAttachment.SetMailUserName(mailAddress); mAttachment.SetTextMsg(textmsg); mAttachment.SetTitle(message.Subject); if (!mAttachment.Save())//save into database { retVal.Append("SaveError"); } else { SendMailOrNotification(dsUser, GetCtx(), Msg.GetMsg(GetCtx(), " Emailrecievedwithsubject") + " = " + message.Subject + " " + Msg.GetMsg(GetCtx(), "ANDAttachto") + " " + Msg.GetMsg(GetCtx(), "RequestID") + " = " + recordID, Convert.ToInt32(TableID), Convert.ToInt32(recordID), Convert.ToString(documentNO)); } } // else // { try { string sql = "SELECT " + tableName + "_ID " + " , C_BPartner_ID,Name,value " + "FROM " + tableName + " WHERE lower(Email) like " + "'%" + from.Trim().ToLower() + "%'"; sql += " AND AD_Client_ID=" + AD_Client_ID; //sql += " AND AD_Client_ID=" + GetCtx().GetAD_Client_ID(); //string finalSql = MRole.GetDefault(GetCtx(), false).AddAccessSQL(sql, tableName.ToString(), MRole.SQL_NOTQUALIFIED, MRole.SQL_RO); IDataReader idr = DB.ExecuteReader(sql);//+ " order by ad_texttemplate_id"); DataTable dt = new DataTable(); dt.Load(idr); idr.Close(); if (dt.Rows.Count <= 0) { retVal.Append("Either proper access is not there or Email not found in database"); continue; } // if (sender == "contacts") if (dt.Rows.Count > 0) { for (int j = 0; j < dt.Rows.Count; j++) { string sqlQuery = "SELECT IsEmployee From C_BPartner WHERE C_BPartner_ID=" + Convert.ToInt32(dt.Rows[j][1]); sql += " AND AD_Client_ID=" + AD_Client_ID; //sqlQuery += " AND AD_Client_ID=" + GetCtx().GetAD_Client_ID(); //string finalQuery = MRole.GetDefault(GetCtx(), false).AddAccessSQL(sqlQuery, "C_BPartner", MRole.SQL_NOTQUALIFIED, MRole.SQL_RO); DataSet ds = DB.ExecuteDataset(sqlQuery); if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) { if (isExcludeEmployee == "Y" && Convert.ToString(ds.Tables[0].Rows[0]["IsEmployee"]).Trim() == "Y") { continue; } } if (sender == "AD_User") { _tableID = PO.Get_Table_ID("AD_User"); existRec = GetAttachedRecord(_tableID, Convert.ToInt32(dt.Rows[j][0]), Convert.ToInt32(uid), folderName); userOrBp = Msg.GetMsg(GetCtx(), "User"); } //if (sender == "businessPartner") if (sender == "C_BPartner") { _tableID = PO.Get_Table_ID("C_BPartner"); existRec = GetAttachedRecord(_tableID, Convert.ToInt32(dt.Rows[j][1]), Convert.ToInt32(uid), folderName); userOrBp = Msg.GetMsg(GetCtx(), "BusinessPartner"); } if (existRec > 0)// Is mail already attached { retVal.Append("MailAlreadyAttachedWithParticularRecord"); continue; } MMailAttachment1 mAttachment = new MMailAttachment1(GetCtx(), 0, null); IMail message; String eml = imapMail.GetMessageByUID(uid); message = new MailBuilder().CreateFromEml(eml); string textmsg = message.Html; bool isAttachment = false; for (int i = 0; i < message.Attachments.Count; i++) { isAttachment = true; mAttachment.SetBinaryData(message.Attachments[i].Data); mAttachment.AddEntry(message.Attachments[i].FileName, message.Attachments[i].Data); } string cc = "";// mailBody.Cc; for (int i = 0; i < message.Cc.Count; i++) { cc += ((Limilabs.Mail.Headers.MailBox)message.Cc[i]).Address + ";"; } string bcc = "";// mailBody.Bcc; for (int i = 0; i < message.Bcc.Count; i++) { bcc += ((Limilabs.Mail.Headers.MailBox)message.Bcc[i]).Address + ";"; } string title = message.Subject; string mailAddress = ""; for (int i = 0; i < message.To.Count; i++) { mailAddress += ((Limilabs.Mail.Headers.MailBox)message.To[i]).Address + ";"; } string mailFrom = ""; string subject = ""; for (int i = 0; i < message.From.Count; i++) { mailFrom += ((Limilabs.Mail.Headers.MailBox)message.From[i]).Address + ";"; } string date = ((DateTime)message.Date).ToShortDateString(); mAttachment.SetAD_Client_ID(GetCtx().GetAD_Client_ID()); mAttachment.SetAD_Org_ID(GetCtx().GetAD_Org_ID()); mAttachment.SetAD_Table_ID(_tableID); mAttachment.SetAttachmentType("I"); mAttachment.SetDateMailReceived(message.Date); mAttachment.SetFolderName(folderName); mAttachment.SetIsActive(true); mAttachment.SetIsAttachment(isAttachment); mAttachment.SetMailAddress(mailAddress); mAttachment.SetMailAddressBcc(bcc); mAttachment.SetMailAddressCc(cc); mAttachment.SetMailAddressFrom(mailFrom); if (sender == "AD_User") { mAttachment.SetRecord_ID(Convert.ToInt32(dt.Rows[j][0])); record_ID = Convert.ToInt32(dt.Rows[j][0]); } if (sender == "C_BPartner") { mAttachment.SetRecord_ID(Convert.ToInt32(dt.Rows[j][1])); record_ID = Convert.ToInt32(dt.Rows[j][1]); } mAttachment.SetMailUID(Convert.ToInt32(uid)); mAttachment.SetMailUserName(mailAddress); mAttachment.SetTextMsg(textmsg); mAttachment.SetTitle(message.Subject); if (!mAttachment.Save())//save into database { retVal.Append("SaveError"); } else { SendMailOrNotification(dsUser, GetCtx(), Msg.GetMsg(GetCtx(), " Emailrecievedwithsubject") + " = " + message.Subject + Msg.GetMsg(GetCtx(), "ANDAttachto") + userOrBp + " = " + Convert.ToString(dt.Rows[j]["Name"]), _tableID, record_ID, Convert.ToString(dt.Rows[j]["Value"])); } } } else if (dt.Rows.Count == 0) { retVal.Append("NoRecordFound"); } //else //{ // retVal.Append("MultipleRecordFound"); //} } catch { } //} } catch (Exception ex) { Logout(); } } Logout(); } else { log.Log(Level.SEVERE, login); } }