csharp 致电SMO并发送电子邮件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 致电SMO并发送电子邮件相关的知识,希望对你有一定的参考价值。



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);
                                    }
                                }
                            }
                        }
                    }
                }

            }
        

以上是关于csharp 致电SMO并发送电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

csharp E10致电BOReader

csharp Generate_All_Table_Script_SMO

csharp:SMO run sql script

csharp 发邮件发送电子邮件

csharp C#中的电子邮件发送

csharp 发送电子邮件