コード例 #1
0
    protected void btnSend_Click(object sender, EventArgs e)
    {
        #region 准备接口数据

        string    sEmailTemplateID = this.ddlEmailTemplateList.SelectedValue;
        string    sToIDs           = this.hdnToIDs.Value;
        LoginUser CurrentUser      = this.CurrUser;
        string    Token            = hidToken.Value;
        Dictionary <string, byte[]> Attachments = new Dictionary <string, byte[]>();

        #region Attachments

        //LPWeb.BLL.Template_Email_Attachments bllTempEmailattach = new Template_Email_Attachments();
        //var ds = bllTempEmailattach.GetList(" [Enabled] =1 AND TemplEmailId = " + sEmailTemplateID);

        LPWeb.BLL.Email_AttachmentsTemp bllEmailAttachTemp = new Email_AttachmentsTemp();

        var ds = bllEmailAttachTemp.GetListWithFileImage(Convert.ToInt32(sEmailTemplateID), Token);

        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow item in ds.Tables[0].Rows)
            {
                try
                {
                    Attachments.Add(item["Name"].ToString() + "." + item["FileType"].ToString(), (byte[])item["FileImage"]);
                }
                catch { }
            }
        }


        #endregion

        int[]    ToUserIDArray    = null;
        int[]    ToContactIDArray = null;
        string[] ToEmailAddrArray = null;

        int EmailIndex = 0;

        int[]    CCUserIDArray    = null;
        int[]    CCContactIDArray = null;
        string[] CCEmailAddrArray = null;

        if (sToIDs == string.Empty)  // 如果未添加收件人,则以email template的To和CC为准发邮件
        {
            #region use To and CC of email template

            #region 获取Email Template的Recipient(s)

            Template_Email EmailTemplateManager = new Template_Email();
            DataTable      RecipientList        = EmailTemplateManager.GetRecipientList(Convert.ToInt32(sEmailTemplateID));

            #endregion

            #region 获取Loan Team

            string    sSql         = "select * from LoanTeam where FileId = " + this.iLoanID;
            DataTable LoanTeamList = LPWeb.DAL.DbHelperSQL.ExecuteDataTable(sSql);

            #endregion

            #region 获取Contacts

            string    sSql2       = "select * from LoanContacts where FileId = " + this.iLoanID;
            DataTable ContactList = LPWeb.DAL.DbHelperSQL.ExecuteDataTable(sSql2);

            #endregion

            Collection <Int32>  ToUserIDs    = new Collection <int>();
            Collection <Int32>  ToContactIDs = new Collection <int>();
            Collection <String> ToEmailList  = new Collection <String>();

            Collection <Int32>  CCUserIDs    = new Collection <int>();
            Collection <Int32>  CCContactIDs = new Collection <int>();
            Collection <String> CCEmailList  = new Collection <String>();

            #region To

            DataRow[] ToRecipient = RecipientList.Select("RecipientType='To'");
            if (ToRecipient.Length > 0)
            {
                string sEmailList_To    = ToRecipient[0]["EmailAddr"].ToString();
                string sContactList_To  = ToRecipient[0]["ContactRoles"].ToString();
                string sUserRoleList_To = ToRecipient[0]["UserRoles"].ToString();

                #region Emails

                if (sEmailList_To != string.Empty)
                {
                    string[] EmailArray_To = sEmailList_To.Split(';');
                    foreach (string sEmailTo in EmailArray_To)
                    {
                        ToEmailList.Add(sEmailTo);
                    }
                }

                #endregion

                #region User IDs

                if (sUserRoleList_To != string.Empty)
                {
                    string[] UserRoleArray_To = sUserRoleList_To.Split(';');
                    foreach (string sUserRoleIDTo in UserRoleArray_To)
                    {
                        int iUserRoleIDTo = Convert.ToInt32(sUserRoleIDTo);

                        DataRow[] LoanTeamRows = LoanTeamList.Select("RoleId=" + iUserRoleIDTo);
                        foreach (DataRow LoanTeamRow in LoanTeamRows)
                        {
                            int iUserID = Convert.ToInt32(LoanTeamRow["UserId"]);
                            ToUserIDs.Add(iUserID);
                        }
                    }
                }

                #endregion

                #region Contact IDs

                if (sContactList_To != string.Empty)
                {
                    string[] ContactArray_To = sContactList_To.Split(';');
                    foreach (string sContactIDTo in ContactArray_To)
                    {
                        int iContactRoleIDTo = Convert.ToInt32(sContactIDTo);

                        DataRow[] ContactRows = ContactList.Select("ContactRoleId=" + iContactRoleIDTo);
                        foreach (DataRow ContactRow in ContactRows)
                        {
                            int iContactID = Convert.ToInt32(ContactRow["ContactId"]);
                            ToContactIDs.Add(iContactID);
                        }
                    }
                }

                #endregion
            }

            #endregion

            #region CC

            DataRow[] CCRecipient = RecipientList.Select("RecipientType='CC'");
            if (CCRecipient.Length > 0)
            {
                string sEmailList_CC    = CCRecipient[0]["EmailAddr"].ToString();
                string sContactList_CC  = CCRecipient[0]["ContactRoles"].ToString();
                string sUserRoleList_CC = CCRecipient[0]["UserRoles"].ToString();

                #region Emails

                if (sEmailList_CC != string.Empty)
                {
                    string[] EmailArray_CC = sEmailList_CC.Split(';');
                    foreach (string sEmailCC in EmailArray_CC)
                    {
                        CCEmailList.Add(sEmailCC);
                    }
                }

                #endregion

                #region User IDs

                if (sUserRoleList_CC != string.Empty)
                {
                    string[] UserRoleArray_CC = sUserRoleList_CC.Split(';');
                    foreach (string sUserRoleIDCC in UserRoleArray_CC)
                    {
                        int iUserRoleIDCC = Convert.ToInt32(sUserRoleIDCC);

                        DataRow[] LoanTeamRows = LoanTeamList.Select("RoleId=" + iUserRoleIDCC);
                        foreach (DataRow LoanTeamRow in LoanTeamRows)
                        {
                            int iUserID = Convert.ToInt32(LoanTeamRow["UserId"]);
                            CCUserIDs.Add(iUserID);
                        }
                    }
                }

                #endregion

                #region Contact IDs

                if (sContactList_CC != string.Empty)
                {
                    string[] ContactArray_CC = sContactList_CC.Split(';');
                    foreach (string sContactIDCC in ContactArray_CC)
                    {
                        int iContactRoleIDCC = Convert.ToInt32(sContactIDCC);

                        DataRow[] ContactRows = ContactList.Select("ContactRoleId=" + iContactRoleIDCC);
                        foreach (DataRow ContactRow in ContactRows)
                        {
                            int iContactID = Convert.ToInt32(ContactRow["ContactId"]);
                            CCContactIDs.Add(iContactID);
                        }
                    }
                }

                #endregion
            }

            // if check me, add user's email to CC list
            if (this.chkCCMe.Checked == true && CurrentUser.sEmail != string.Empty)
            {
                CCEmailList.Add(CurrentUser.sEmail);
            }

            #endregion

            ToUserIDArray    = new int[ToUserIDs.Count];
            ToContactIDArray = new int[ToContactIDs.Count];
            ToEmailAddrArray = new string[ToEmailList.Count];

            CCUserIDArray    = new int[CCUserIDs.Count];
            CCContactIDArray = new int[CCContactIDs.Count];
            CCEmailAddrArray = new string[CCEmailList.Count];

            ToUserIDs.CopyTo(ToUserIDArray, 0);
            ToContactIDs.CopyTo(ToContactIDArray, 0);
            ToEmailList.CopyTo(ToEmailAddrArray, 0);

            CCUserIDs.CopyTo(CCUserIDArray, 0);
            CCContactIDs.CopyTo(CCContactIDArray, 0);
            CCEmailList.CopyTo(CCEmailAddrArray, 0);

            #endregion
        }
        else // 如果添加收件人,则覆盖email template的To和CC
        {
            #region build ToUserIDArray and ToContactIDArray

            Collection <Int32> ToUserIDs    = new Collection <int>();
            Collection <Int32> ToContactIDs = new Collection <int>();

            string EmailAddress = "";
            string UserSql      = "";
            string ContactSql   = "";

            DataTable UserTable    = null;
            DataTable ContactTable = null;

            string[] ToIDArray = sToIDs.Split('$');
            ToEmailAddrArray = new string[ToIDArray.Length];
            foreach (string ToID in ToIDArray)
            {
                if (ToID.Contains("User") == true)
                {
                    int iToUserID = Convert.ToInt32(ToID.Replace("User", ""));
                    ToUserIDs.Add(iToUserID);
                    UserSql   = "select EmailAddress from Users where UserId = " + iToUserID;
                    UserTable = LPWeb.DAL.DbHelperSQL.ExecuteDataTable(UserSql);
                    foreach (DataRow dr in UserTable.Rows)
                    {
                        if (dr["EmailAddress"] == DBNull.Value)
                        {
                            EmailAddress = "";
                        }
                        else
                        {
                            EmailAddress = dr["EmailAddress"].ToString().Trim();
                        }
                    }
                    ToEmailAddrArray[EmailIndex] = EmailAddress;
                    EmailIndex = EmailIndex + 1;
                }
                else
                {
                    int iToContactID = Convert.ToInt32(ToID.Replace("Contact", ""));
                    ToContactIDs.Add(iToContactID);
                    ContactSql   = "select Email from Contacts where ContactId = " + iToContactID;
                    ContactTable = LPWeb.DAL.DbHelperSQL.ExecuteDataTable(ContactSql);

                    foreach (DataRow dr in ContactTable.Rows)
                    {
                        if (dr["Email"] == DBNull.Value)
                        {
                            EmailAddress = "";
                        }
                        else
                        {
                            EmailAddress = dr["Email"].ToString().Trim();
                        }
                    }
                    ToEmailAddrArray[EmailIndex] = EmailAddress;
                    EmailIndex = EmailIndex + 1;
                }
            }

            ToUserIDArray    = new int[ToUserIDs.Count];
            ToContactIDArray = new int[ToContactIDs.Count];

            if (ToUserIDs.Count > 0)
            {
                ToUserIDs.CopyTo(ToUserIDArray, 0);
            }

            if (ToContactIDs.Count > 0)
            {
                ToContactIDs.CopyTo(ToContactIDArray, 0);
            }

            #endregion

            CCUserIDArray    = new int[0];
            CCContactIDArray = new int[0];
            if (this.chkCCMe.Checked == true && CurrentUser.sEmail != string.Empty)
            {
                CCEmailAddrArray = new string[1];
                CCEmailAddrArray.SetValue(CurrentUser.sEmail, 0);
            }
            else
            {
                CCEmailAddrArray = new string[0];
            }
        }

        #endregion

        #region 调用API

        string sCloseDialogCodes = this.GetCloseDialogJs();

        SendEmailResponse response = null;

        try
        {
            ServiceManager sm = new ServiceManager();
            using (LP2ServiceClient service = sm.StartServiceClient())
            {
                #region SendEmailRequest

                SendEmailRequest req = new SendEmailRequest();
                if (this.chkUserEmailTemplate.Checked == true)
                {
                    req.EmailTemplId           = Convert.ToInt32(sEmailTemplateID);
                    req.EmailSubject           = string.Empty;
                    req.EmailBody              = null;
                    req.AppendPictureSignature = false;
                }
                else
                {
                    req.EmailTemplId           = 0;
                    req.EmailSubject           = this.txtSubject.Text.Trim();
                    req.EmailBody              = Encoding.UTF8.GetBytes(this.txtBody.Text.Trim());
                    req.AppendPictureSignature = this.chkAppendMyPic.Checked;
                }

                if (this.Request.QueryString["LoanID"] != null)
                {
                    req.FileId = this.iLoanID;
                }
                else if (this.Request.QueryString["ProspectID"] != null)
                {
                    req.ProspectId = this.iProspectID;
                }
                else if (this.Request.QueryString["ProspectAlertID"] != null)
                {
                    req.PropsectTaskId = this.iProspectAlertID;
                }

                if ((this.Request.QueryString["LoanID"] == null) &&
                    (this.Request.QueryString["ProspectID"] != null))
                {
                    string sSql = "select * from LoanContacts where ContactId=" + req.ProspectId + " and (ContactRoleId=dbo.lpfn_GetBorrowerRoleId() or ContactRoleId=dbo.lpfn_GetCoBorrowerRoleId())";

                    DataTable LoanList = null;
                    try
                    {
                        LoanList = LPWeb.DAL.DbHelperSQL.ExecuteDataTable(sSql);
                        foreach (DataRow LoanListRow in LoanList.Rows)
                        {
                            req.FileId = (int)LoanListRow["FileId"];
                        }
                    }
                    catch
                    {
                    }

                    if (LoanList == null || LoanList.Rows.Count == 0)
                    {
                    }
                }

                req.UserId       = CurrentUser.iUserID;
                req.ToEmails     = ToEmailAddrArray;
                req.ToUserIds    = ToUserIDArray;
                req.ToContactIds = ToContactIDArray;
                req.CCEmails     = CCEmailAddrArray;
                req.CCUserIds    = CCUserIDArray;
                req.CCContactIds = CCContactIDArray;
                req.hdr          = new ReqHdr();

                #region add Attachments

                req.Attachments = Attachments;


                #endregion


                #endregion

                response = service.SendEmail(req);
            }
        }
        catch (System.ServiceModel.EndpointNotFoundException)
        {
            string sExMsg = string.Format("Failed to send email, reason: Email Manager is not running.");
            LPLog.LogMessage(LogType.Logerror, sExMsg);
            PageCommon.WriteJsEnd(this, sExMsg, sCloseDialogCodes);
        }
        catch (Exception ex)
        {
            string sExMsg = string.Format("Failed to send email, error: {0}", ex.Message);
            LPLog.LogMessage(LogType.Logerror, sExMsg);
            PageCommon.WriteJsEnd(this, sExMsg, sCloseDialogCodes);
        }

        #endregion

        // 提示调用结果
        if (response.resp.Successful == true)
        {
            try
            {
                bllEmailAttachTemp.DeleteByToken(Token);
            }
            catch { }

            string RefreshParent = "window.parent.location.href=window.parent.location.href;";
            PageCommon.WriteJsEnd(this, "Sent email successfully.", RefreshParent + sCloseDialogCodes);
        }
        else
        {
            PageCommon.WriteJsEnd(this, "Failed to send email: " + response.resp.StatusInfo.Replace("'", "\'"), sCloseDialogCodes);
        }
    }
コード例 #2
0
    /// <summary>
    /// 发送邮件(旧发送方式)
    /// </summary>
    public void SendEmailOld()
    {
        LoginUser CurrentUser = new LoginUser();
        string    Token       = Request.QueryString["Token"] != null ? Request.QueryString["Token"].ToString() : "";

        LPWeb.BLL.Email_AttachmentsTemp bllEmailAttachTemp = new Email_AttachmentsTemp();
        var errMsg = string.Empty;

        //遍历所有模板 发送邮件
        foreach (var iEmailTemplateID in iEmailTemplateIDList)
        {
            string   sEmailTemplateName = string.Empty;
            int[]    ToUserIDArray      = null;
            int[]    ToContactIDArray   = null;
            string[] ToEmailAddrArray   = null;

            int[]    CCUserIDArray    = null;
            int[]    CCContactIDArray = null;
            string[] CCEmailAddrArray = null;


            Dictionary <string, byte[]> Attachments = new Dictionary <string, byte[]>();

            #region Attachments



            var ds = bllEmailAttachTemp.GetListWithFileImage(iEmailTemplateID, Token);

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow item in ds.Tables[0].Rows)
                {
                    try
                    {
                        Attachments.Add(item["Name"].ToString() + "." + item["FileType"].ToString(), (byte[])item["FileImage"]);
                    }
                    catch { }
                }
            }


            #endregion


            #region use To and CC of email template

            #region 获取Email Template的Recipient(s)

            Template_Email EmailTemplateManager = new Template_Email();
            DataTable      RecipientList        = EmailTemplateManager.GetRecipientList(iEmailTemplateID);
            var            emailTemplatemodel   = EmailTemplateManager.GetModel(iEmailTemplateID);
            if (emailTemplatemodel != null)
            {
                sEmailTemplateName = emailTemplatemodel.Name;
            }
            #endregion

            #region 获取Loan Team

            string    sSql         = "select * from LoanTeam where FileId = " + this.iLoanID;
            DataTable LoanTeamList = LPWeb.DAL.DbHelperSQL.ExecuteDataTable(sSql);

            #endregion

            #region 获取Contacts

            string    sSql2       = "select * from LoanContacts where FileId = " + this.iLoanID;
            DataTable ContactList = LPWeb.DAL.DbHelperSQL.ExecuteDataTable(sSql2);

            #endregion

            Collection <Int32>  ToUserIDs    = new Collection <int>();
            Collection <Int32>  ToContactIDs = new Collection <int>();
            Collection <String> ToEmailList  = new Collection <String>();

            Collection <Int32>  CCUserIDs    = new Collection <int>();
            Collection <Int32>  CCContactIDs = new Collection <int>();
            Collection <String> CCEmailList  = new Collection <String>();

            #region To

            DataRow[] ToRecipient = RecipientList.Select("RecipientType='To'");
            if (ToRecipient.Length > 0)
            {
                string sEmailList_To    = ToRecipient[0]["EmailAddr"].ToString();
                string sContactList_To  = ToRecipient[0]["ContactRoles"].ToString();
                string sUserRoleList_To = ToRecipient[0]["UserRoles"].ToString();
                string sTaskOwner       = ToRecipient[0]["TaskOwner"].ToString();

                #region Emails

                if (sEmailList_To != string.Empty)
                {
                    string[] EmailArray_To = sEmailList_To.Split(';');
                    foreach (string sEmailTo in EmailArray_To)
                    {
                        ToEmailList.Add(sEmailTo);
                    }
                }

                #endregion

                #region User IDs

                if (sUserRoleList_To != string.Empty)
                {
                    string[] UserRoleArray_To = sUserRoleList_To.Split(';');
                    foreach (string sUserRoleIDTo in UserRoleArray_To)
                    {
                        int iUserRoleIDTo = Convert.ToInt32(sUserRoleIDTo);

                        DataRow[] LoanTeamRows = LoanTeamList.Select("RoleId=" + iUserRoleIDTo);
                        foreach (DataRow LoanTeamRow in LoanTeamRows)
                        {
                            int iUserID = Convert.ToInt32(LoanTeamRow["UserId"]);
                            ToUserIDs.Add(iUserID);
                        }
                    }
                }

                #endregion

                #region Contact IDs

                if (sContactList_To != string.Empty)
                {
                    string[] ContactArray_To = sContactList_To.Split(';');
                    foreach (string sContactIDTo in ContactArray_To)
                    {
                        int iContactRoleIDTo = Convert.ToInt32(sContactIDTo);

                        DataRow[] ContactRows = ContactList.Select("ContactRoleId=" + iContactRoleIDTo);
                        foreach (DataRow ContactRow in ContactRows)
                        {
                            int iContactID = Convert.ToInt32(ContactRow["ContactId"]);
                            ToContactIDs.Add(iContactID);
                        }
                    }
                }

                #endregion

                #region TaskOwner

                if ((sTaskOwner != string.Empty) && (sTaskOwner != null))
                {
                    if (sTaskOwner == "True")
                    {
                        string    sSql_LoanTasks = "select Owner from LoanTasks where LoanTaskId=" + this.iTaskID;
                        DataTable LoanTasks_List = LPWeb.DAL.DbHelperSQL.ExecuteDataTable(sSql_LoanTasks);

                        string sOwnerId = LoanTasks_List.Rows[0]["Owner"].ToString();

                        if ((sOwnerId != string.Empty) && (sOwnerId != null))
                        {
                            int       iOwnerId           = Convert.ToInt32(sOwnerId);
                            string    sSql_Users         = "select EmailAddress, LastName, FirstName from Users where UserId=" + iOwnerId;
                            DataTable Users_List         = LPWeb.DAL.DbHelperSQL.ExecuteDataTable(sSql_Users);
                            string    Owner_EmailAddress = Users_List.Rows[0]["EmailAddress"].ToString();
                            if ((Owner_EmailAddress != string.Empty) && (Owner_EmailAddress != null))
                            {
                                ToUserIDs.Add(iOwnerId);
                                ToEmailList.Add(Owner_EmailAddress);
                            }
                        }
                    }
                }

                #endregion
            }

            #endregion

            #region CC

            DataRow[] CCRecipient = RecipientList.Select("RecipientType='CC'");
            if (CCRecipient.Length > 0)
            {
                string sEmailList_CC    = CCRecipient[0]["EmailAddr"].ToString();
                string sContactList_CC  = CCRecipient[0]["ContactRoles"].ToString();
                string sUserRoleList_CC = CCRecipient[0]["UserRoles"].ToString();

                #region Emails

                if (sEmailList_CC != string.Empty)
                {
                    string[] EmailArray_CC = sEmailList_CC.Split(';');
                    foreach (string sEmailCC in EmailArray_CC)
                    {
                        CCEmailList.Add(sEmailCC);
                    }
                }

                #endregion

                #region User IDs

                if (sUserRoleList_CC != string.Empty)
                {
                    string[] UserRoleArray_CC = sUserRoleList_CC.Split(';');
                    foreach (string sUserRoleIDCC in UserRoleArray_CC)
                    {
                        int iUserRoleIDCC = Convert.ToInt32(sUserRoleIDCC);

                        DataRow[] LoanTeamRows = LoanTeamList.Select("RoleId=" + iUserRoleIDCC);
                        foreach (DataRow LoanTeamRow in LoanTeamRows)
                        {
                            int iUserID = Convert.ToInt32(LoanTeamRow["UserId"]);
                            CCUserIDs.Add(iUserID);
                        }
                    }
                }

                #endregion

                #region Contact IDs

                if (sContactList_CC != string.Empty)
                {
                    string[] ContactArray_CC = sContactList_CC.Split(';');
                    foreach (string sContactIDCC in ContactArray_CC)
                    {
                        int iContactRoleIDCC = Convert.ToInt32(sContactIDCC);

                        DataRow[] ContactRows = ContactList.Select("ContactRoleId=" + iContactRoleIDCC);
                        foreach (DataRow ContactRow in ContactRows)
                        {
                            int iContactID = Convert.ToInt32(ContactRow["ContactId"]);
                            CCContactIDs.Add(iContactID);
                        }
                    }
                }

                #endregion
            }

            #endregion


            if (ToUserIDs.Count == 0 && ToEmailList.Count == 0 && ToContactIDs.Count == 0 &&
                CCUserIDs.Count == 0 && CCContactIDs.Count == 0 && CCEmailList.Count == 0)
            {
                errMsg = "No email recipient !   email template: " + sEmailTemplateName;
                break;
            }

            ToUserIDArray    = new int[ToUserIDs.Count];
            ToContactIDArray = new int[ToContactIDs.Count];
            ToEmailAddrArray = new string[ToEmailList.Count];

            CCUserIDArray    = new int[CCUserIDs.Count];
            CCContactIDArray = new int[CCContactIDs.Count];
            CCEmailAddrArray = new string[CCEmailList.Count];

            ToUserIDs.CopyTo(ToUserIDArray, 0);
            ToContactIDs.CopyTo(ToContactIDArray, 0);
            ToEmailList.CopyTo(ToEmailAddrArray, 0);

            CCUserIDs.CopyTo(CCUserIDArray, 0);
            CCContactIDs.CopyTo(CCContactIDArray, 0);
            CCEmailList.CopyTo(CCEmailAddrArray, 0);

            #endregion



            #region 调用API

            SendEmailResponse response = null;

            try
            {
                ServiceManager sm = new ServiceManager();
                using (LP2ServiceClient service = sm.StartServiceClient())
                {
                    SendEmailRequest req = new SendEmailRequest();
                    req.EmailTemplId = iEmailTemplateID;
                    req.FileId       = this.iLoanID;
                    //req.UserId = CurrentUser.iUserID;
                    req.UserId       = 0;
                    req.ToEmails     = ToEmailAddrArray;
                    req.ToUserIds    = ToUserIDArray;
                    req.ToContactIds = ToContactIDArray;
                    req.CCEmails     = CCEmailAddrArray;
                    req.CCUserIds    = CCUserIDArray;
                    req.CCContactIds = CCContactIDArray;
                    req.hdr          = new ReqHdr();
                    #region add Attachments

                    req.Attachments = Attachments;


                    #endregion
                    response = service.SendEmail(req);
                }
            }
            catch (System.ServiceModel.EndpointNotFoundException)
            {
                string sExMsg = string.Format("Failed to send the email, reason: Email Manager is not running.");
                LPLog.LogMessage(LogType.Logerror, sExMsg);
                //PageCommon.WriteJsEnd(this, sExMsg, "window.parent.CloseDialog_SendCompletionEmail();");
                response = new SendEmailResponse()
                {
                    resp = new RespHdr()
                    {
                        Successful = false, StatusInfo = sExMsg
                    }
                };
            }
            catch (Exception ex)
            {
                string sExMsg = string.Format("Failed to send email, error: {0}", ex.Message);
                LPLog.LogMessage(LogType.Logerror, sExMsg);
                //PageCommon.WriteJsEnd(this, sExMsg, "window.parent.CloseDialog_SendCompletionEmail();");
                response = new SendEmailResponse()
                {
                    resp = new RespHdr()
                    {
                        Successful = false, StatusInfo = sExMsg
                    }
                };
            }

            #endregion

            if (response.resp.Successful != true)
            {
                //PageCommon.WriteJsEnd(this, "Failed to send completion email: " + response.resp.StatusInfo.Replace("'", "\'"), "window.parent.CloseDialog_SendCompletionEmail();");
                errMsg = response.resp.StatusInfo.Replace("'", "\'");
                break;
            }
            //有一个邮件发送错误都报发送错误并退出发送
        }

        if (string.IsNullOrEmpty(errMsg))
        {
            try
            {
                bllEmailAttachTemp.DeleteByToken(Token);
            }
            catch { }
            this.Response.Write("{\"ExecResult\":\"Success\",\"ErrorMsg\":\"\"}");
            return;
        }
        else
        {
            this.Response.Write("{\"ExecResult\":\"Failed\",\"ErrorMsg\":\"" + errMsg + "\"}");
            return;
        }
    }
コード例 #3
0
    protected void btnSend_Click(object sender, EventArgs e)
    {
        ///modify to  EmailSendConmpletion_Background.aspx

        LoginUser CurrentUser = new LoginUser();

        int[]    ToUserIDArray    = null;
        int[]    ToContactIDArray = null;
        string[] ToEmailAddrArray = null;

        int[]    CCUserIDArray    = null;
        int[]    CCContactIDArray = null;
        string[] CCEmailAddrArray = null;

        string Token = hidToken.Value;
        Dictionary <string, byte[]> Attachments = new Dictionary <string, byte[]>();

        #region Attachments

        LPWeb.BLL.Email_AttachmentsTemp bllEmailAttachTemp = new Email_AttachmentsTemp();

        var ds = bllEmailAttachTemp.GetListWithFileImage(iEmailTemplateID, Token);

        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow item in ds.Tables[0].Rows)
            {
                try
                {
                    Attachments.Add(item["Name"].ToString() + "." + item["FileType"].ToString(), (byte[])item["FileImage"]);
                }
                catch { }
            }

            try
            {
                bllEmailAttachTemp.DeleteByToken(Token);
            }
            catch { }
        }


        #endregion


        #region use To and CC of email template

        #region 获取Email Template的Recipient(s)

        Template_Email EmailTemplateManager = new Template_Email();
        DataTable      RecipientList        = EmailTemplateManager.GetRecipientList(this.iEmailTemplateID);

        #endregion

        #region 获取Loan Team

        string    sSql         = "select * from LoanTeam where FileId = " + this.iLoanID;
        DataTable LoanTeamList = LPWeb.DAL.DbHelperSQL.ExecuteDataTable(sSql);

        #endregion

        #region 获取Contacts

        string    sSql2       = "select * from LoanContacts where FileId = " + this.iLoanID;
        DataTable ContactList = LPWeb.DAL.DbHelperSQL.ExecuteDataTable(sSql2);

        #endregion

        Collection <Int32>  ToUserIDs    = new Collection <int>();
        Collection <Int32>  ToContactIDs = new Collection <int>();
        Collection <String> ToEmailList  = new Collection <String>();

        Collection <Int32>  CCUserIDs    = new Collection <int>();
        Collection <Int32>  CCContactIDs = new Collection <int>();
        Collection <String> CCEmailList  = new Collection <String>();

        #region To

        DataRow[] ToRecipient = RecipientList.Select("RecipientType='To'");
        if (ToRecipient.Length > 0)
        {
            string sEmailList_To    = ToRecipient[0]["EmailAddr"].ToString();
            string sContactList_To  = ToRecipient[0]["ContactRoles"].ToString();
            string sUserRoleList_To = ToRecipient[0]["UserRoles"].ToString();
            string sTaskOwner       = ToRecipient[0]["TaskOwner"].ToString();

            #region Emails

            if (sEmailList_To != string.Empty)
            {
                string[] EmailArray_To = sEmailList_To.Split(';');
                foreach (string sEmailTo in EmailArray_To)
                {
                    ToEmailList.Add(sEmailTo);
                }
            }

            #endregion

            #region User IDs

            if (sUserRoleList_To != string.Empty)
            {
                string[] UserRoleArray_To = sUserRoleList_To.Split(';');
                foreach (string sUserRoleIDTo in UserRoleArray_To)
                {
                    int iUserRoleIDTo = Convert.ToInt32(sUserRoleIDTo);

                    DataRow[] LoanTeamRows = LoanTeamList.Select("RoleId=" + iUserRoleIDTo);
                    foreach (DataRow LoanTeamRow in LoanTeamRows)
                    {
                        int iUserID = Convert.ToInt32(LoanTeamRow["UserId"]);
                        ToUserIDs.Add(iUserID);
                    }
                }
            }

            #endregion

            #region Contact IDs

            if (sContactList_To != string.Empty)
            {
                string[] ContactArray_To = sContactList_To.Split(';');
                foreach (string sContactIDTo in ContactArray_To)
                {
                    int iContactRoleIDTo = Convert.ToInt32(sContactIDTo);

                    DataRow[] ContactRows = ContactList.Select("ContactRoleId=" + iContactRoleIDTo);
                    foreach (DataRow ContactRow in ContactRows)
                    {
                        int iContactID = Convert.ToInt32(ContactRow["ContactId"]);
                        ToContactIDs.Add(iContactID);
                    }
                }
            }

            #endregion

            #region TaskOwner

            if ((sTaskOwner != string.Empty) && (sTaskOwner != null))
            {
                if (sTaskOwner == "True")
                {
                    string    sSql_LoanTasks = "select Owner from LoanTasks where LoanTaskId=" + this.iTaskID;
                    DataTable LoanTasks_List = LPWeb.DAL.DbHelperSQL.ExecuteDataTable(sSql_LoanTasks);

                    string sOwnerId = LoanTasks_List.Rows[0]["Owner"].ToString();

                    if ((sTaskOwner != string.Empty) && (sTaskOwner != null))
                    {
                        int       iOwnerId           = Convert.ToInt32(sOwnerId);
                        string    sSql_Users         = "select EmailAddress, LastName, FirstName from Users where UserId=" + iOwnerId;
                        DataTable Users_List         = LPWeb.DAL.DbHelperSQL.ExecuteDataTable(sSql_Users);
                        string    Owner_EmailAddress = Users_List.Rows[0]["EmailAddress"].ToString();
                        if ((Owner_EmailAddress != string.Empty) && (Owner_EmailAddress != null))
                        {
                            ToUserIDs.Add(iOwnerId);
                            ToEmailList.Add(Owner_EmailAddress);
                        }
                    }
                }
            }

            #endregion
        }

        #endregion

        #region CC

        DataRow[] CCRecipient = RecipientList.Select("RecipientType='CC'");
        if (CCRecipient.Length > 0)
        {
            string sEmailList_CC    = CCRecipient[0]["EmailAddr"].ToString();
            string sContactList_CC  = CCRecipient[0]["ContactRoles"].ToString();
            string sUserRoleList_CC = CCRecipient[0]["UserRoles"].ToString();

            #region Emails

            if (sEmailList_CC != string.Empty)
            {
                string[] EmailArray_CC = sEmailList_CC.Split(';');
                foreach (string sEmailCC in EmailArray_CC)
                {
                    CCEmailList.Add(sEmailCC);
                }
            }

            #endregion

            #region User IDs

            if (sUserRoleList_CC != string.Empty)
            {
                string[] UserRoleArray_CC = sUserRoleList_CC.Split(';');
                foreach (string sUserRoleIDCC in UserRoleArray_CC)
                {
                    int iUserRoleIDCC = Convert.ToInt32(sUserRoleIDCC);

                    DataRow[] LoanTeamRows = LoanTeamList.Select("RoleId=" + iUserRoleIDCC);
                    foreach (DataRow LoanTeamRow in LoanTeamRows)
                    {
                        int iUserID = Convert.ToInt32(LoanTeamRow["UserId"]);
                        CCUserIDs.Add(iUserID);
                    }
                }
            }

            #endregion

            #region Contact IDs

            if (sContactList_CC != string.Empty)
            {
                string[] ContactArray_CC = sContactList_CC.Split(';');
                foreach (string sContactIDCC in ContactArray_CC)
                {
                    int iContactRoleIDCC = Convert.ToInt32(sContactIDCC);

                    DataRow[] ContactRows = ContactList.Select("ContactRoleId=" + iContactRoleIDCC);
                    foreach (DataRow ContactRow in ContactRows)
                    {
                        int iContactID = Convert.ToInt32(ContactRow["ContactId"]);
                        CCContactIDs.Add(iContactID);
                    }
                }
            }

            #endregion
        }

        #endregion

        ToUserIDArray    = new int[ToUserIDs.Count];
        ToContactIDArray = new int[ToContactIDs.Count];
        ToEmailAddrArray = new string[ToEmailList.Count];

        CCUserIDArray    = new int[CCUserIDs.Count];
        CCContactIDArray = new int[CCContactIDs.Count];
        CCEmailAddrArray = new string[CCEmailList.Count];

        ToUserIDs.CopyTo(ToUserIDArray, 0);
        ToContactIDs.CopyTo(ToContactIDArray, 0);
        ToEmailList.CopyTo(ToEmailAddrArray, 0);

        CCUserIDs.CopyTo(CCUserIDArray, 0);
        CCContactIDs.CopyTo(CCContactIDArray, 0);
        CCEmailList.CopyTo(CCEmailAddrArray, 0);

        #endregion

        #region 调用API

        SendEmailResponse response = null;

        try
        {
            ServiceManager sm = new ServiceManager();
            using (LP2ServiceClient service = sm.StartServiceClient())
            {
                SendEmailRequest req = new SendEmailRequest();
                req.EmailTemplId = this.iEmailTemplateID;
                req.FileId       = this.iLoanID;
                req.UserId       = CurrentUser.iUserID;
                req.ToEmails     = ToEmailAddrArray;
                req.ToUserIds    = ToUserIDArray;
                req.ToContactIds = ToContactIDArray;
                req.CCEmails     = CCEmailAddrArray;
                req.CCUserIds    = CCUserIDArray;
                req.CCContactIds = CCContactIDArray;
                req.hdr          = new ReqHdr();

                #region add Attachments

                req.Attachments = Attachments;


                #endregion

                response = service.SendEmail(req);
            }
        }
        catch (System.ServiceModel.EndpointNotFoundException)
        {
            string sExMsg = string.Format("Failed to send the email, reason: Email Manager is not running.");
            LPLog.LogMessage(LogType.Logerror, sExMsg);
            PageCommon.WriteJsEnd(this, sExMsg, "window.parent.CloseDialog_SendCompletionEmail();");
        }
        catch (Exception ex)
        {
            string sExMsg = string.Format("Failed to send email, error: {0}", ex.Message);
            LPLog.LogMessage(LogType.Logerror, sExMsg);
            PageCommon.WriteJsEnd(this, sExMsg, "window.parent.CloseDialog_SendCompletionEmail();");
        }

        #endregion

        if (response.resp.Successful == true)
        {
            PageCommon.WriteJsEnd(this, "Sent completion email successfully.", "window.parent.CloseDialog_SendCompletionEmail();");
        }
        else
        {
            PageCommon.WriteJsEnd(this, "Failed to send completion email: " + response.resp.StatusInfo.Replace("'", "\'"), "window.parent.CloseDialog_SendCompletionEmail();");
        }
    }
コード例 #4
0
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["CloseDialogCodes"] != null) // 如果有LoanID
        {
            CloseDialogCodes = Request.QueryString["CloseDialogCodes"].ToString();
        }

        if (Request.QueryString["TemplEmailId"] != null) // 如果有LoanID
        {
            string sTemplEmailId = Request.QueryString["TemplEmailId"];

            if (PageCommon.IsID(sTemplEmailId) == false)
            {
                PageCommon.WriteJsEnd(this, sErrorMsg, CloseDialogCodes);
            }

            TemplEmailId = Convert.ToInt32(sTemplEmailId);
        }

        Token = Request.QueryString["token"] != null ? Request.QueryString["token"].ToString() : "";
        isListTemplEmailAttach = Request.QueryString["isListTemplEmailAttach"] != null?Convert.ToBoolean(Request.QueryString["isListTemplEmailAttach"]) : false;

        if (!IsPostBack)
        {
            if (isListTemplEmailAttach)
            {
                LPWeb.BLL.Template_Email_Attachments bllTempEmailAttach = new Template_Email_Attachments();

                var           ds = bllTempEmailAttach.GetListWithOutFileImage(" Enabled =1 AND TemplEmailId = " + TemplEmailId);
                StringBuilder sb = new StringBuilder();

                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        sb.Append("<option value='s" + dr["TemplAttachId"].ToString() + "' >" + dr["Name"].ToString() + "." + dr["FileType"].ToString() + "</option>");
                    }
                }

                Default_lbxAttachments = sb.ToString();
            }
        }



        ///判定是不是 在执行发送
        if (Request.Form["hidIsSend"] != null)
        {
            sLocalTime = Request.Form["hfLocalTime"].ToString().Trim();
            //Attachment

            #region Attachments
            if (Request.Files.Count > 0)
            {
                foreach (var key in Request.Files.AllKeys)
                {
                    var file = Request.Files[key];

                    if (file.ContentLength > 0 && !string.IsNullOrEmpty(file.FileName))
                    {
                        if (file.ContentLength > 1024 * 1024 * 20)
                        {
                            ErrorMsg("The file you tried to upload has exceeded the allowed limit, 20MB.");
                            Response.End();
                        }

                        byte[] bytes = new byte[file.InputStream.Length];
                        file.InputStream.Read(bytes, 0, bytes.Length);
                        file.InputStream.Seek(0, SeekOrigin.Begin);

                        FileInfo info = new FileInfo(file.FileName);

                        Attachments.Add(info.Name, bytes);
                    }
                }
            }
            #endregion

            if (Attachments.Count == 0)
            {
                ErrorMsg("Attachments / Note is empty.");
                Response.End();
            }


            #region Save file

            try
            {
                LPWeb.BLL.Email_AttachmentsTemp bllEmailAttachTemp = new Email_AttachmentsTemp();
                foreach (var item in Attachments)
                {
                    LPWeb.Model.Email_AttachmentsTemp model = new LPWeb.Model.Email_AttachmentsTemp();

                    model.Token     = Token;
                    model.Name      = item.Key.ToString().Substring(0, item.Key.ToString().LastIndexOf('.'));
                    model.FileType  = item.Key.ToString().Substring(item.Key.ToString().LastIndexOf('.') + 1);
                    model.FileImage = item.Value;

                    bllEmailAttachTemp.Add(model);
                }

                ErrorMsg(true, "");
            }
            catch (Exception ex)
            {
                ErrorMsg(ex.Message.Replace("\"", "").Replace("\\n", ""));
            }

            #endregion


            Response.End();
        }
    }
コード例 #5
0
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["TemplEmailId"] == null)
        {
            return;
        }
        TemplEmailId = Convert.ToInt32(Request.QueryString["TemplEmailId"]);

        op       = Request.QueryString["op"] == null ? "list" : Request.QueryString["op"].ToString();
        AttachId = Request.QueryString["AttachId"] == null ? "" : Request.QueryString["AttachId"].ToString();
        Token    = Request.QueryString["Token"] == null ? "" : Request.QueryString["Token"].ToString();



        LPWeb.BLL.Email_AttachmentsTemp bllEmailAttachTemp = new Email_AttachmentsTemp();
        switch (op)
        {
        case "list":

            rpAttachmentsList.DataSource = bllEmailAttachTemp.GetList(TemplEmailId, Token);       //blltmpemalAttach.GetListWithOutFileImage("TemplEmailId = " + TemplEmailId + " and Enabled =1");
            rpAttachmentsList.DataBind();

            break;

        case "Remove":
            if (string.IsNullOrEmpty(AttachId))
            {
                Response.Clear();
                Response.Write("1");
                Response.End();
                return;
            }

            var idlist = AttachId.Split(',').ToList();
            foreach (string idstr in idlist)
            {
                try
                {
                    if (idstr.ToLower().StartsWith("s"))
                    {
                        int id = Convert.ToInt32(idstr.Replace("s", ""));
                        bllEmailAttachTemp.Add(
                            new LPWeb.Model.Email_AttachmentsTemp {
                            Token = Token, TemplAttachId = id
                        }
                            );
                    }
                    if (idstr.ToLower().StartsWith("c"))
                    {
                        int id = Convert.ToInt32(idstr.Replace("c", ""));
                        bllEmailAttachTemp.Delete(id);
                    }
                }
                catch { }
            }



            Response.Clear();
            Response.Write("1");
            Response.End();
            break;
        }
    }