Andyloveprogram
9/13/2017 - 8:00 AM

Call SMO and Send Email

Call SMO and Send Email



public static void SendAutomatedEmail(string currentuser, DataSet dsuserpendinglist, string mailserver, string port, string mailfrom, string cnstring, string smohost, string smoport, string smoname, string smomethod, string password)
        {
            using (MailMessage mail = new MailMessage())
            {
                //connect to smart object
                // build up a connection string with the SCConnectionStringBuilder
                SourceCode.Hosting.Client.BaseAPI.SCConnectionStringBuilder hostServerConnectionString = new SCConnectionStringBuilder();
                hostServerConnectionString.Host = smohost; // name of the K2 host server, or the name of the DNS entry pointing to the K2 Farm
                hostServerConnectionString.Port = Convert.ToUInt32(smoport); // use port 5555 for all non-workflow client connections
                hostServerConnectionString.IsPrimaryLogin = true; // true = re-authenticate user, false = use cached security credentials
                hostServerConnectionString.Integrated = true; // true = use the logged on user, false = use the specified user

                string ExceptionEmailUsers = System.Configuration.ConfigurationManager.AppSettings["ExceptionEmailUsers"];

                //Open the connection to K2
                SourceCode.SmartObjects.Client.SmartObjectClientServer soServer = new SmartObjectClientServer();
                soServer.CreateConnection();
                soServer.Connection.Open(hostServerConnectionString.ToString());

                string PortalURL = System.Configuration.ConfigurationManager.AppSettings["PortalURL"];
               

                mail.From = new MailAddress(mailfrom);

                SmartObject mySmartObject = soServer.GetSmartObject(smoname);

                // set up the Method, Input Properties and Filters, if required
                mySmartObject.MethodToExecute = smomethod;

                bool err = false;
                if (currentuser.ToUpper().Substring(0, 3).Contains("K2:"))
                {
                    currentuser = "" + currentuser.Substring(3);
                }
                if (ExceptionEmailUsers.Trim().ToUpper().Contains(currentuser.Trim().ToUpper()))
                {

                }
                else
                {
                    if (currentuser != "K2Server")
                    {
                        mySmartObject.Methods[smomethod].InputProperties["UserName"].Value = currentuser;
                        SmartObject so = null;

                        try
                        {
                            so = soServer.ExecuteScalar(mySmartObject);
                        }
                        catch (Exception ex)
                        {
                            if (ex.Message.Contains("could not resolve"))
                            {
                                err = true;
                                return;
                            }
                        }

                        //read a property from the SmartObject
                        string propertyValue = null;
                        string dispName = "Admin";
                        if (err)
                            propertyValue = System.Configuration.ConfigurationManager.AppSettings["SupportUserEmail"];
                        else
                        {
                            if (so != null)
                            {
                                propertyValue = so.Properties["Email"].Value.ToString();
                                dispName = so.Properties["DisplayName"].Value.ToString();
                            }
                        }
                        //close the K2 connection
                        soServer.Connection.Close();


                        if (!string.IsNullOrEmpty(propertyValue))
                        {
                            //mail.To.Add("andyz@k2.com");
                            string mailBody = getHtml(dispName, dsuserpendinglist, PortalURL);
                            mail.To.Add(propertyValue);
                            mail.Subject = "Workflow Approval - Pending Task Notification";
                            mail.IsBodyHtml = true;
                            mail.Body = mailBody;                       
                            using (SmtpClient smtp = new SmtpClient(mailserver, Int32.Parse(port)))
                            {
                                smtp.Credentials = new NetworkCredential(mailfrom, password);
                                smtp.EnableSsl = false; //[2017.04.21 Kavidha]:For Hayco this should be false
                                try
                                {
                                    smtp.Send(mail);
                                }
                                catch (Exception ex)
                                {
                                    if (ex.Message.Contains("could not resolve"))
                                    {
                                        mail.To.Add(System.Configuration.ConfigurationManager.AppSettings["SupportUserEmail"]);
                                        smtp.Send(mail);
                                    }
                                }
                            }
                        }
                    }
                }

            }