Esempio n. 1
0
    private string SendEmail(int ContactID, int UserID, byte[] EmailBody)
    {
        string         ReturnMessage = string.Empty;
        ServiceManager sm            = new ServiceManager();

        LPWeb.BLL.Loans _bLoan = new LPWeb.BLL.Loans();
        using (LP2ServiceClient service = sm.StartServiceClient())
        {
            SendEmailRequest req = new SendEmailRequest();
            req.hdr = new ReqHdr();
            req.hdr.SecurityToken = "SecurityToken"; //todo:check dummy data
            req.hdr.UserId        = this.CurrUser.iUserID;
            req.EmailBody         = EmailBody;
            req.EmailSubject      = string.Format("Loan Status Report for {0}'s loan", _bLoan.GetLoanBorrowerName(iLoanID));
            req.UserId            = this.CurrUser.iUserID;
            if (ContactID > 0)
            {
                req.ToContactIds = new int[1] {
                    ContactID
                };
            }
            if (UserID > 0)
            {
                req.ToUserIds = new int[1] {
                    UserID
                };
            }
            SendEmailResponse respone = null;
            try
            {
                respone = service.SendEmail(req);

                if (respone.resp.Successful)
                {
                    ReturnMessage = string.Empty;
                }
                else
                {
                    ReturnMessage = respone.resp.StatusInfo;
                }
            }
            catch (System.ServiceModel.EndpointNotFoundException)
            {
                string sExMsg = string.Format("Failed to send email, reason: Email Manager is not running.");
                LPLog.LogMessage(LogType.Logerror, sExMsg);
            }
            catch (Exception ex)
            {
                string sExMsg = string.Format("Failed to send email, error: {0}", ex.Message);
                LPLog.LogMessage(LogType.Logerror, sExMsg);
            }

            return(ReturnMessage);
        }
    }
Esempio n. 2
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);
        }
    }
Esempio n. 3
0
    protected void btnSend_Click(object sender, EventArgs e)
    {
        LoginUser CurrentUser = new LoginUser();

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

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

        #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

        #region 获取Prospect Loan Officer

        string    sSql3           = "SELECT dbo.Users.*  FROM dbo.Prospect INNER JOIN  dbo.ProspectTasks ON dbo.Prospect.Contactid = dbo.ProspectTasks.ContactId INNER JOIN dbo.Users ON dbo.Prospect.Loanofficer = dbo.Users.UserId WHERE  dbo.ProspectTasks.ProspectTaskId =" + this.iProspectID;
        DataTable LoanOfficerList = LPWeb.DAL.DbHelperSQL.ExecuteDataTable(sSql3);

        #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 Loan Officer User IDs

            if (sUserRoleList_To != string.Empty)
            {
                foreach (DataRow LoanOfficerRow in LoanOfficerList.Rows)
                {
                    int iLOUserID = Convert.ToInt32(LoanOfficerRow["UserId"]);
                    ToUserIDs.Add(iLOUserID);
                }
            }

            #endregion
            #region TaskOwner

            if ((sTaskOwner != string.Empty) && (sTaskOwner != null))
            {
                if (sTaskOwner == "True")
                {
                    string    sSql_LoanTasks = "select Owner from LoanTasks where LoanTaskId=" + this.iProspectID;
                    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

        bool successful_status = true;

        try
        {
            ServiceManager sm = new ServiceManager();
            using (LP2ServiceClient service = sm.StartServiceClient())
            {
                SendEmailRequest req = new SendEmailRequest();
                req.EmailTemplId = this.iEmailTemplateID;
                req.FileId       = this.iProspectID;
                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();

                SendEmailResponse response = service.SendEmail(req);

                if (response.resp.Successful == false)
                {
                    this.ClientScript.RegisterClientScriptBlock(this.GetType(), "_FailedSend5", "alert('Failed to send completion email.');window.parent.CloseDialog_SendCompletionEmail()();", true);
                    return;
                }
            }
        }
        catch (System.ServiceModel.EndpointNotFoundException ee)
        {
            successful_status = false;
            PageCommon.AlertMsg(this, "Failed to send completion email, reason: Email Manager is not running.");
        }

        #endregion
        if (successful_status == true)
        {
            PageCommon.WriteJs(this, "Send completion email successfully.", "window.parent.CloseDialog_SendCompletionEmail();");
        }
    }
    /// <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;
        }
    }
Esempio n. 5
0
    /// <summary>
    /// 完成 自定义task后 发送邮件 //gdc CR40
    /// </summary>
    /// <param name="modelcmpAlerts"></param>
    /// <param name="taskID"></param>
    public void SaveOKAndSendEmail(LPWeb.Model.Company_Alerts modelcmpAlerts, int taskID)
    {
        LoginUser CurrentUser = new LoginUser();

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

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

        #region use To and CC of email template

        #region 获取Email Template的Recipient(s)

        Template_Email EmailTemplateManager = new Template_Email();
        DataTable      RecipientList        = EmailTemplateManager.GetRecipientList(modelcmpAlerts.CustomTaskEmailTemplId);

        #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=" + taskID;
                    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 = modelcmpAlerts.CustomTaskEmailTemplId;
                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();

                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
    }
    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();");
        }
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        LoginUser CurrentUser = this.CurrUser;

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

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

        #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();

            #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
        }

        #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();

                response = service.SendEmail(req);
            }
        }
        catch (System.ServiceModel.EndpointNotFoundException)
        {
            string sExMsg = string.Format("Failed to send completion email, reason: Email Manager is not running.");
            LPLog.LogMessage(LogType.Logerror, sExMsg);
            PageCommon.WriteJsEnd(this, sExMsg, "window.close();");
        }
        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.close();");
        }

        #endregion

        if (response.resp.Successful == true)
        {
            PageCommon.WriteJsEnd(this, "Sent completion email successfully.", "window.close();");
        }
        else
        {
            PageCommon.WriteJsEnd(this, "Failed to send completion email: " + response.resp.StatusInfo.Replace("'", "\'"), "window.close();");
        }
    }
Esempio n. 8
0
        protected string SendEmail(int iLoanID, int iTaskID, int iEmailTemplateID)
        {
            string    ErorMsg     = string.Empty;
            LoginUser CurrentUser = new LoginUser();

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

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

            #region use To and CC of email template

            #region 获取Email Template的Recipient(s)

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

            #endregion

            #region 获取Loan Team

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

            #endregion

            #region 获取Contacts

            string    sSql2       = "select * from LoanContacts where FileId = " + iLoanID;
            DataTable ContactList = LPWEBDAL.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=" + iTaskID;
                        DataTable LoanTasks_List = LPWEBDAL.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         = LPWEBDAL.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 = iEmailTemplateID;
                    req.FileId       = 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();

                    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);
            }
            catch (Exception ex)
            {
                string sExMsg = string.Format("Failed to send email, error: {0}", ex.Message);
                LPLog.LogMessage(LogType.Logerror, sExMsg);
            }
            finally
            {
                if (response.resp.Successful == true)
                {
                    ErorMsg = "Sent completion email successfully.";
                }
                else
                {
                    ErorMsg = "Failed to send completion email: " + response.resp.StatusInfo.Replace("'", "\'");
                }
            }
            #endregion
            return(ErorMsg);
        }